Illia56 commited on
Commit
9946d9a
·
1 Parent(s): 3ca7351

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
app.py CHANGED
@@ -4,7 +4,6 @@ from llama_index.llms import Replicate
4
  from llama_index.embeddings import LangchainEmbedding
5
  from langchain.embeddings.huggingface import HuggingFaceEmbeddings
6
  from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, SimpleDirectoryReader
7
- import os
8
 
9
  # Ensure the environment variable is set
10
  if "REPLICATE_API_TOKEN" not in os.environ:
@@ -12,8 +11,6 @@ if "REPLICATE_API_TOKEN" not in os.environ:
12
  else:
13
  os.environ["REPLICATE_API_TOKEN"] = os.environ["REPLICATE_API_TOKEN"]
14
 
15
- PATH = '/Data'
16
-
17
  llm = Replicate(
18
  model="replicate/vicuna-13b:6282abe6a492de4145d7bb601023762212f9ddbbe78278bd6771c8b3b2f2a13b"
19
  )
@@ -29,8 +26,8 @@ service_context = ServiceContext.from_defaults(
29
  )
30
  set_global_service_context(service_context)
31
 
32
- # Transcribe and Query function
33
- def transcribe_and_query(youtube_url, message):
34
  client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
35
  result = client.predict(youtube_url, "transcribe", True, fn_index=7)
36
  with open(f'{PATH}/docs.txt','w') as f:
@@ -38,11 +35,7 @@ def transcribe_and_query(youtube_url, message):
38
 
39
  documents = SimpleDirectoryReader(PATH).load_data()
40
  index = VectorStoreIndex.from_documents(documents)
41
- query_engine = index.as_query_engine()
42
- response = query_engine.query(message)
43
-
44
- # Assuming the response has a 'response' attribute with the answer
45
- return response.response
46
 
47
  # Streamlit UI
48
  st.title("YouTube Video Chatbot")
@@ -50,6 +43,10 @@ st.title("YouTube Video Chatbot")
50
  # Input for YouTube URL
51
  youtube_url = st.text_input("Enter YouTube Video URL:")
52
 
 
 
 
 
53
  # Chatbot UI
54
  if "messages" not in st.session_state:
55
  st.session_state.messages = []
@@ -65,15 +62,16 @@ for message in st.session_state.messages:
65
  prompt = st.text_input("Ask something about the video:")
66
 
67
  # React to user input
68
- if prompt:
69
  # Add user message to chat history
70
  st.session_state.messages.append({"role": "human", "content": prompt})
71
 
72
  # Get response from the chatbot
73
- response = transcribe_and_query(youtube_url, prompt)
 
74
 
75
  # Add assistant response to chat history
76
- st.session_state.messages.append({"role": "assistant", "content": response})
77
 
78
  # Refresh the page to show the updated chat history
79
  if prompt:
 
4
  from llama_index.embeddings import LangchainEmbedding
5
  from langchain.embeddings.huggingface import HuggingFaceEmbeddings
6
  from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, SimpleDirectoryReader
 
7
 
8
  # Ensure the environment variable is set
9
  if "REPLICATE_API_TOKEN" not in os.environ:
 
11
  else:
12
  os.environ["REPLICATE_API_TOKEN"] = os.environ["REPLICATE_API_TOKEN"]
13
 
 
 
14
  llm = Replicate(
15
  model="replicate/vicuna-13b:6282abe6a492de4145d7bb601023762212f9ddbbe78278bd6771c8b3b2f2a13b"
16
  )
 
26
  )
27
  set_global_service_context(service_context)
28
 
29
+ # Transcribe function
30
+ def transcribe_video(youtube_url):
31
  client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
32
  result = client.predict(youtube_url, "transcribe", True, fn_index=7)
33
  with open(f'{PATH}/docs.txt','w') as f:
 
35
 
36
  documents = SimpleDirectoryReader(PATH).load_data()
37
  index = VectorStoreIndex.from_documents(documents)
38
+ return index.as_query_engine()
 
 
 
 
39
 
40
  # Streamlit UI
41
  st.title("YouTube Video Chatbot")
 
43
  # Input for YouTube URL
44
  youtube_url = st.text_input("Enter YouTube Video URL:")
45
 
46
+ if youtube_url and "query_engine" not in st.session_state:
47
+ st.write("Transcribing video... Please wait.")
48
+ st.session_state.query_engine = transcribe_video(youtube_url)
49
+
50
  # Chatbot UI
51
  if "messages" not in st.session_state:
52
  st.session_state.messages = []
 
62
  prompt = st.text_input("Ask something about the video:")
63
 
64
  # React to user input
65
+ if prompt and "query_engine" in st.session_state:
66
  # Add user message to chat history
67
  st.session_state.messages.append({"role": "human", "content": prompt})
68
 
69
  # Get response from the chatbot
70
+ response = st.session_state.query_engine.query(prompt)
71
+ response_text = response.response # Assuming the response has a 'response' attribute with the answer
72
 
73
  # Add assistant response to chat history
74
+ st.session_state.messages.append({"role": "assistant", "content": response_text})
75
 
76
  # Refresh the page to show the updated chat history
77
  if prompt: