Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Temporary fix for pydantic issue @see https://github.com/streamlit/streamlit/issues/3218
|
2 |
+
import pydantic
|
3 |
+
pydantic.class_validators._FUNCS.clear()
|
4 |
+
import streamlit as st
|
5 |
+
from streamlit_elements import elements, media
|
6 |
+
from chatbot import YouTubeChatbot
|
7 |
+
from components.sidebar import sidebar
|
8 |
+
from langchain.memory import ConversationBufferWindowMemory
|
9 |
+
from streamlit_chat import message
|
10 |
+
|
11 |
+
def index():
|
12 |
+
def put_media_player():
|
13 |
+
with elements("media_player"):
|
14 |
+
video_url = st.session_state.get('video_url')
|
15 |
+
media.Player(url=video_url, controls=True)
|
16 |
+
def clear_submit():
|
17 |
+
st.session_state["submit"] = False
|
18 |
+
|
19 |
+
st.set_page_config(page_title="YoutuberGPT", page_icon="🤖", layout="wide")
|
20 |
+
st.header("🤖YoutuberGPT")
|
21 |
+
sidebar()
|
22 |
+
|
23 |
+
if 'responses' not in st.session_state:
|
24 |
+
st.session_state['responses'] = ["Ask any question related to the video"]
|
25 |
+
|
26 |
+
if 'requests' not in st.session_state:
|
27 |
+
st.session_state['requests'] = []
|
28 |
+
|
29 |
+
if 'buffer_memory' not in st.session_state:
|
30 |
+
st.session_state.buffer_memory=ConversationBufferWindowMemory(k=3,return_messages=True)
|
31 |
+
|
32 |
+
if 'video_url' not in st.session_state:
|
33 |
+
st.session_state['video_url']= []
|
34 |
+
|
35 |
+
videocontainer = st.container()
|
36 |
+
# container for chat history
|
37 |
+
response_container = st.container()
|
38 |
+
# container for text box
|
39 |
+
textcontainer = st.container()
|
40 |
+
|
41 |
+
|
42 |
+
with videocontainer:
|
43 |
+
video_url = st.text_input("YouTube Video Url:", on_change=clear_submit)
|
44 |
+
st.session_state['video_url'] = video_url
|
45 |
+
if video_url:
|
46 |
+
put_media_player()
|
47 |
+
|
48 |
+
with textcontainer:
|
49 |
+
question = st.text_area("Question:", key="question")
|
50 |
+
if st.button("Run") or st.session_state.get("submit"):
|
51 |
+
try:
|
52 |
+
with st.spinner('preparing answer'):
|
53 |
+
chatbot = YouTubeChatbot()
|
54 |
+
db = chatbot.create_db_from_youtube_video_url(video_url)
|
55 |
+
if db is None:
|
56 |
+
return st.error("There is no transcript")
|
57 |
+
|
58 |
+
response = chatbot.get_response_from_query(db, question)
|
59 |
+
if response is None:
|
60 |
+
return st.error("There is no answer or something went wrong")
|
61 |
+
else:
|
62 |
+
st.session_state.requests.append(question)
|
63 |
+
st.session_state.responses.append(response)
|
64 |
+
st.session_state["submit"] = True
|
65 |
+
except:
|
66 |
+
return st.error("There is no answer or something went wrong")
|
67 |
+
|
68 |
+
with response_container:
|
69 |
+
if st.session_state['responses']:
|
70 |
+
|
71 |
+
for i in range(len(st.session_state['responses'])):
|
72 |
+
message(st.session_state['responses'][i],key=str(i))
|
73 |
+
if i < len(st.session_state['requests']):
|
74 |
+
message(st.session_state["requests"][i], is_user=True,key=str(i)+ '_user')
|
75 |
+
|
76 |
+
index()
|