Spaces:
Runtime error
Runtime error
Commit
·
dda0b8c
1
Parent(s):
6867599
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
6 |
from langchain.embeddings import AwaEmbeddings
|
7 |
import os
|
8 |
import openai
|
9 |
-
import
|
10 |
|
11 |
|
12 |
def get_transcript(url):
|
@@ -37,36 +37,72 @@ text_splitter = RecursiveCharacterTextSplitter(
|
|
37 |
is_separator_regex = False,
|
38 |
)
|
39 |
|
40 |
-
def chat(
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from langchain.embeddings import AwaEmbeddings
|
7 |
import os
|
8 |
import openai
|
9 |
+
import streamlit as st
|
10 |
|
11 |
|
12 |
def get_transcript(url):
|
|
|
37 |
is_separator_regex = False,
|
38 |
)
|
39 |
|
40 |
+
def chat(question):
|
41 |
+
if 'database' in st.session_state:
|
42 |
+
docs = st.session_state.database.similarity_search(question)
|
43 |
+
|
44 |
+
prompt = [
|
45 |
+
{"role": "system", "content": """You are my Youtube Asisstant. I will pass you texts from a Youtube Video Transcrip and I need you to use them to answer my question from the Youtube Video.
|
46 |
+
Please do not invent any information, and I am asking about information in the Youtube Video."""},
|
47 |
+
{"role":"user", "content": f"Context:{docs}"},
|
48 |
+
{"role":"user", "content": f"Question:{question}"},
|
49 |
+
]
|
50 |
+
response = openai.ChatCompletion.create(
|
51 |
+
model="gpt-3.5-turbo-0613",
|
52 |
+
messages=prompt,
|
53 |
+
temperature = 0
|
54 |
+
)
|
55 |
+
|
56 |
+
return response["choices"][0]["message"]["content"]
|
57 |
+
else:
|
58 |
+
return "Error, not generated database"
|
59 |
+
|
60 |
+
|
61 |
+
#------------------------------------------------------------- APP STREAMLIT--------------------------------------------------------------------
|
62 |
+
|
63 |
+
st.title("Ask Question Youtube Videos")
|
64 |
+
|
65 |
+
with st.sidebar:
|
66 |
+
if "api" not in st.session_state:
|
67 |
+
api_key= st.text_input(label="api", placeholder="API Key from OpenAI", label_visibility="hidden")
|
68 |
+
if st.button(label="Save"):
|
69 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
70 |
+
openai.api_key = api_key
|
71 |
+
st.session_state['api'] = api_key
|
72 |
+
else:
|
73 |
+
url = st.text_input(label="url", placeholder="Youtube Video URL", label_visibility="hidden")
|
74 |
+
if st.button(label="Save"):
|
75 |
+
st.session_state['url'] = url
|
76 |
+
info = get_transcript(url)
|
77 |
+
texts = text_splitter.create_documents([info])
|
78 |
+
st.session_state['database'] = FAISS.from_documents(texts, embeddings)
|
79 |
+
|
80 |
+
if "api" not in st.session_state:
|
81 |
+
st.write("Please, introduce your OpenAI API key to ask questions to any YouTube Video")
|
82 |
+
|
83 |
+
elif 'url' not in st.session_state:
|
84 |
+
st.write("Please, introduce link URL from the YouTube Video")
|
85 |
+
|
86 |
+
else:
|
87 |
+
# Initialize chat history
|
88 |
+
if "messages" not in st.session_state:
|
89 |
+
st.session_state.messages = []
|
90 |
+
|
91 |
+
# Display chat messages from history on app rerun
|
92 |
+
for message in st.session_state.messages:
|
93 |
+
with st.chat_message(message["role"]):
|
94 |
+
st.markdown(message["content"])
|
95 |
|
96 |
+
# Accept user input
|
97 |
+
if prompt := st.chat_input("What is up?"):
|
98 |
+
# Add user message to chat history
|
99 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
100 |
+
# Display user message in chat message container
|
101 |
+
with st.chat_message("user"):
|
102 |
+
st.markdown(prompt)
|
103 |
+
# Get response from your custom chat function
|
104 |
+
response = chat(prompt)
|
105 |
+
# Display assistant response in chat message container
|
106 |
+
with st.chat_message("assistant"):
|
107 |
+
st.markdown(response)
|
108 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|