Spaces:
Runtime error
Runtime error
a
commited on
Commit
•
49d09fc
1
Parent(s):
12b0fae
Update app.py
Browse files
app.py
CHANGED
@@ -7,92 +7,7 @@ hide_streamlit_style = """
|
|
7 |
</style>
|
8 |
"""
|
9 |
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
10 |
-
def paid_version():
|
11 |
-
import os
|
12 |
-
import argparse
|
13 |
-
import shutil
|
14 |
-
from langchain.document_loaders import YoutubeLoader
|
15 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
16 |
-
from langchain.vectorstores import Chroma
|
17 |
-
from langchain.embeddings import OpenAIEmbeddings
|
18 |
-
from langchain.chains import RetrievalQA
|
19 |
-
from langchain.llms import OpenAI
|
20 |
-
import streamlit as st
|
21 |
-
from langchain.chat_models import ChatOpenAI
|
22 |
-
from urllib.parse import urlparse, parse_qs
|
23 |
-
def extract_video_id(youtube_url):
|
24 |
-
try:
|
25 |
-
parsed_url = urlparse(youtube_url)
|
26 |
-
query_params = parse_qs(parsed_url.query)
|
27 |
-
video_id = query_params.get('v', [None])[0]
|
28 |
-
|
29 |
-
return video_id
|
30 |
-
except Exception as e:
|
31 |
-
print(f"Error extracting video ID: {e}")
|
32 |
-
return None
|
33 |
-
def set_openAi_api_key(api_key: str):
|
34 |
-
st.session_state["OPENAI_API_KEY"] = api_key
|
35 |
-
os.environ['OPENAI_API_KEY'] = api_key
|
36 |
-
def openai_api_insert_component():
|
37 |
-
with st.sidebar:
|
38 |
-
st.markdown(
|
39 |
-
"""
|
40 |
-
## Quick Guide 🚀
|
41 |
-
1. Get started by adding your [OpenAI API key](https://platform.openai.com/account/api-keys) below🔑
|
42 |
-
2. Easily input the video url
|
43 |
-
3. Engage with the content - ask questions, seek answers💬
|
44 |
-
"""
|
45 |
-
)
|
46 |
-
|
47 |
-
api_key_input = st.text_input("Input your OpenAI API Key",
|
48 |
-
type="password",
|
49 |
-
placeholder="Format: sk-...",
|
50 |
-
help="You can get your API key from https://platform.openai.com/account/api-keys.")
|
51 |
-
|
52 |
-
|
53 |
-
if api_key_input == "" or api_key_input is None:
|
54 |
-
st.sidebar.caption("👆 :red[Please set your OpenAI API Key here]")
|
55 |
-
|
56 |
-
|
57 |
-
st.caption(":green[Your API is not stored anywhere. It is only used to generate answers to your questions.]")
|
58 |
-
|
59 |
-
set_openAi_api_key(api_key_input)
|
60 |
|
61 |
-
def launchpaidversion():
|
62 |
-
openai_api_insert_component()
|
63 |
-
os.environ['OPENAI_API_KEY'] = st.session_state['OPENAI_API_KEY']
|
64 |
-
st.title('MKG: Your Chat with Youtube Assistant')
|
65 |
-
|
66 |
-
|
67 |
-
videourl = st.text_input("Insert The video URL")
|
68 |
-
query = st.text_input("Ask any question about the video")
|
69 |
-
if st.button("Submit Question", type="primary"):
|
70 |
-
video_id = extract_video_id(videourl)
|
71 |
-
loader = YoutubeLoader(video_id)
|
72 |
-
documents = loader.load()
|
73 |
-
|
74 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
75 |
-
documents = text_splitter.split_documents(documents)
|
76 |
-
|
77 |
-
shutil.rmtree('./data')
|
78 |
-
vectordb = Chroma.from_documents(
|
79 |
-
documents,
|
80 |
-
embedding=OpenAIEmbeddings(),
|
81 |
-
persist_directory='./data'
|
82 |
-
)
|
83 |
-
vectordb.persist()
|
84 |
-
|
85 |
-
qa_chain = RetrievalQA.from_chain_type(
|
86 |
-
llm=ChatOpenAI(model_name='gpt-3.5-turbo'),
|
87 |
-
retriever=vectordb.as_retriever(),
|
88 |
-
return_source_documents=True,
|
89 |
-
verbose=False
|
90 |
-
)
|
91 |
-
response = qa_chain(query)
|
92 |
-
st.write(response)
|
93 |
-
|
94 |
-
|
95 |
-
launchpaidversion()
|
96 |
def free_version():
|
97 |
import torch
|
98 |
import os
|
@@ -249,9 +164,7 @@ def intro():
|
|
249 |
""")
|
250 |
page_names_to_funcs = {
|
251 |
"Main Page": intro,
|
252 |
-
"Open Source Edition (Free version)": free_version
|
253 |
-
"Premium edition (Requires Open AI API Key )": paid_version
|
254 |
-
|
255 |
}
|
256 |
|
257 |
|
|
|
7 |
</style>
|
8 |
"""
|
9 |
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def free_version():
|
12 |
import torch
|
13 |
import os
|
|
|
164 |
""")
|
165 |
page_names_to_funcs = {
|
166 |
"Main Page": intro,
|
167 |
+
"Open Source Edition (Free version)": free_version
|
|
|
|
|
168 |
}
|
169 |
|
170 |
|