Illia56 commited on
Commit
5d9583a
Β·
1 Parent(s): 85e634d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -28,14 +28,19 @@ 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:
34
- f.write(result[1])
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")
@@ -47,32 +52,26 @@ 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 = []
53
 
54
- # Display chat history
55
  for message in st.session_state.messages:
56
- if message["role"] == "human":
57
- st.write(f"You: {message['content']}")
58
- else:
59
- st.write(f"Chatbot: {message['content']}")
60
 
61
  # User input
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:
78
- st.experimental_rerun()
 
28
 
29
  # Transcribe function
30
  def transcribe_video(youtube_url):
31
+ with st.status("Starting client"):
32
+ client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
33
+ st.write("Requesting client")
34
+ with st.status("Requesting Whisper"):
35
+ result = client.predict(youtube_url, "transcribe", True, fn_index=7)
36
+ st.write("Requesting API...")
37
+ with open(f'{PATH}/docs.txt','w') as f:
38
+ f.write(result[1])
39
+ st.write('Writing File...')
40
+ with st.status("Requesting Embeddings"):
41
+ documents = SimpleDirectoryReader(PATH).load_data()
42
+ index = VectorStoreIndex.from_documents(documents)
43
+ return index.as_query_engine()
44
 
45
  # Streamlit UI
46
  st.title("YouTube Video Chatbot")
 
52
  st.write("Transcribing video... Please wait.")
53
  st.session_state.query_engine = transcribe_video(youtube_url)
54
 
 
55
  if "messages" not in st.session_state:
56
  st.session_state.messages = []
57
 
58
+ # Display chat messages from history on app rerun
59
  for message in st.session_state.messages:
60
+ with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else 'πŸ¦™')):
61
+ st.markdown(message["content"])
 
 
62
 
63
  # User input
64
+ prompt = st.chat_input("Ask something about the video:")
65
 
66
+ if prompt := textinput and "query_engine" in st.session_state:
67
+ # Display user message in chat message container
68
+ st.chat_message("human",avatar = "πŸ§‘β€πŸ’»").markdown(prompt)
69
  # Add user message to chat history
70
  st.session_state.messages.append({"role": "human", "content": prompt})
71
 
 
72
  response = st.session_state.query_engine.query(prompt)
73
+ response_text = response.response
74
+ with st.chat_message("assistant", avatar='πŸ¦™'):
75
+ st.markdown(response_text)
76
  # Add assistant response to chat history
77
+ st.session_state.messages.append({"role": "assistant", "content": response})