forked from gtech-mulearn/Top100-OpenAi-Challenge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.py
114 lines (91 loc) · 3.91 KB
/
chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from openai import OpenAI
import streamlit as st
from youtube_transcript_api import YouTubeTranscriptApi
import validators
from urllib.parse import urlparse, parse_qs
st.set_page_config(page_title="Chat with video", page_icon="🏖", layout="centered")
st.title("Chat with video")
st.write(
"Seamlessly converse with videos, receive text responses, and skip watching the lengthy video content."
)
client = OpenAI()
st.session_state.setdefault("openai_model", "gpt-3.5-turbo")
st.session_state.setdefault("messages", [])
st.session_state.setdefault("transcription", None)
st.session_state.setdefault("useDefault", False)
def get_video_id(youtube_url):
# Parse the URL to get the query parameters
parsed_url = urlparse(youtube_url)
query_params = parse_qs(parsed_url.query)
# Extract the video ID from the 'v' parameter
video_id = query_params.get("v", [""])[0]
return video_id
def process_video(url):
with st.spinner("Processing your video.."):
video_id = get_video_id(url)
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
print(transcript_list)
transcription = " ".join([entry["text"] for entry in transcript_list])
st.session_state.transcription = transcription
st.session_state.messages.clear()
st.session_state.messages.append(
{
"role": "user",
"content": st.session_state.transcription
+ "This is the transcript of a youtube video. Answer me the following questions based on the video transcript. In your ansewers, use the word video instead of video transcript.",
}
)
def get_assistant_response():
message_placeholder = st.empty()
full_response = ""
for response in client.chat.completions.create(
model=st.session_state["openai_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
):
full_response += response.choices[0].delta.content or ""
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
return full_response
def discoverTopics(text: str, model_name = "gpt-3.5-turbo"):
print("Points Summarizing...")
print("Using model:", model_name)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant designed to summarize text using bullet points and appropriate topics."},
{"role": "user", "content": text}
],
temperature=0,
max_tokens=1024
)
return response.choices[0].message.content
# User input: YouTube URL
defaultValue = "https://www.youtube.com/watch?v=V5GKwuzKQV0"
url = st.text_input(label="Enter YouTube URL:", value = defaultValue if st.session_state.useDefault else "")
st.session_state.useDefault = st.checkbox("Use sample Youtube URL")
if st.button("Process Video"):
if validators.url(url):
process_video(url)
else:
st.warning("Please enter a valid YouTube URL.")
# st.subheader("Topics Covered")
# with st.spinner("Gettings topics"):
# st.session_state.topics = discoverTopics(st.session_state.transcription)
# st.markdown(st.session_state.topics)
if st.session_state.transcription:
for message in st.session_state.messages[1:]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Ask anything about the video. Eg. Say Summarize"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
full_response = get_assistant_response()
st.session_state.messages.append(
{"role": "assistant", "content": full_response}
)