awacke1 commited on
Commit
4be1d74
β€’
1 Parent(s): 82cc38a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -14
app.py CHANGED
@@ -17,6 +17,8 @@ from xml.etree import ElementTree as ET
17
  from openai import OpenAI
18
  import extra_streamlit_components as stx
19
  from streamlit.runtime.scriptrunner import get_script_run_ctx
 
 
20
 
21
  # πŸ”§ Config & Setup
22
  st.set_page_config(
@@ -71,6 +73,7 @@ def get_download_link(file):
71
 
72
  @st.cache_resource
73
  def speech_synthesis_html(result):
 
74
  html_code = f"""
75
  <html><body>
76
  <script>
@@ -81,6 +84,37 @@ def speech_synthesis_html(result):
81
  """
82
  components.html(html_code, height=0)
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  def process_image(image_path, user_prompt):
85
  with open(image_path, "rb") as imgf:
86
  image_data = imgf.read()
@@ -153,16 +187,27 @@ def perform_ai_lookup(q):
153
  # Ask model for answer
154
  r2 = client.predict(q,"mistralai/Mixtral-8x7B-Instruct-v0.1",True,api_name="/ask_llm")
155
  result = f"### πŸ”Ž {q}\n\n{r2}\n\n{refs}"
 
 
156
  # Speak results
157
- speech_synthesis_html(r2)
 
 
 
158
 
159
- # Attempt to speak summaries and titles from refs
160
- # Assuming refs contain a set of references in Markdown with possible titles.
161
- # We'll just re-speak refs as "summaries".
 
 
 
 
162
  summaries_text = "Here are the summaries from the references: " + refs.replace('"','')
163
- speech_synthesis_html(summaries_text)
 
 
164
 
165
- # Extract titles from refs (looking for markdown links [Title](URL))
166
  titles = []
167
  for line in refs.split('\n'):
168
  m = re.search(r"\[([^\]]+)\]", line)
@@ -170,7 +215,11 @@ def perform_ai_lookup(q):
170
  titles.append(m.group(1))
171
  if titles:
172
  titles_text = "Here are the titles of the papers: " + ", ".join(titles)
173
- speech_synthesis_html(titles_text)
 
 
 
 
174
 
175
  st.markdown(result)
176
  elapsed = time.time()-start
@@ -282,15 +331,13 @@ def display_file_manager():
282
  if st.button("πŸ—‘",key="d"+f):
283
  os.remove(f)
284
  st.experimental_rerun()
285
-
286
  def main():
287
  st.sidebar.markdown("### 🚲BikeAIπŸ† Multi-Agent Research AI")
288
  tab_main = st.radio("Action:",["🎀 Voice Input","πŸ“Έ Media Gallery","πŸ” Search ArXiv","πŸ“ File Editor"],horizontal=True)
289
 
290
- # Changed model order and default:
291
  model_choice = st.sidebar.radio("AI Model:", ["Arxiv","GPT-4o","Claude-3","GPT+Claude+Arxiv"], index=0)
292
 
293
- # Speech-to-Text component placeholder (example)
294
  mycomponent = components.declare_component("mycomponent", path="mycomponent")
295
  val = mycomponent(my_input_value="Hello")
296
  if val:
@@ -300,11 +347,9 @@ def main():
300
  elif model_choice == "Claude-3":
301
  process_with_claude(user_input)
302
  elif model_choice == "Arxiv":
303
- # Just Arxiv on its own, full column, speak results
304
  st.subheader("Arxiv Only Results:")
305
  perform_ai_lookup(user_input)
306
  else:
307
- # GPT+Claude+Arxiv
308
  col1,col2,col3=st.columns(3)
309
  with col1:
310
  st.subheader("GPT-4o Omni:")
@@ -335,7 +380,6 @@ def main():
335
  st.subheader("Arxiv Only Results:")
336
  perform_ai_lookup(user_text)
337
  else:
338
- # GPT+Claude+Arxiv
339
  col1,col2,col3=st.columns(3)
340
  with col1:
341
  st.subheader("GPT-4o Omni:")
@@ -379,4 +423,4 @@ def main():
379
  display_file_manager()
380
 
381
  if __name__=="__main__":
382
- main()
 
17
  from openai import OpenAI
18
  import extra_streamlit_components as stx
19
  from streamlit.runtime.scriptrunner import get_script_run_ctx
20
+ import asyncio
21
+ import edge_tts # ensure this is installed (pip install edge-tts)
22
 
23
  # πŸ”§ Config & Setup
24
  st.set_page_config(
 
73
 
74
  @st.cache_resource
75
  def speech_synthesis_html(result):
76
+ # This old function can remain as a fallback, but we won't use it after integrating EdgeTTS.
77
  html_code = f"""
78
  <html><body>
79
  <script>
 
84
  """
85
  components.html(html_code, height=0)
86
 
87
+ #------------add EdgeTTS
88
+ # --- NEW FUNCTIONS FOR EDGE TTS ---
89
+ async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0):
90
+ """
91
+ Generate audio from text using Edge TTS and return the path to the MP3 file.
92
+ """
93
+ if not text.strip():
94
+ return None
95
+ rate_str = f"{rate:+d}%"
96
+ pitch_str = f"{pitch:+d}Hz"
97
+ communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
98
+ out_fn = generate_filename(text,"mp3")
99
+ await communicate.save(out_fn)
100
+ return out_fn
101
+
102
+ def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0):
103
+ """
104
+ Synchronous wrapper to call the async TTS generation and return the file path.
105
+ """
106
+ return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch))
107
+
108
+ def play_and_download_audio(file_path):
109
+ """
110
+ Display an audio player and a download link for the generated MP3 file.
111
+ """
112
+ if file_path and os.path.exists(file_path):
113
+ st.audio(file_path)
114
+ st.markdown(get_download_link(file_path), unsafe_allow_html=True)
115
+ #---------------------------
116
+
117
+
118
  def process_image(image_path, user_prompt):
119
  with open(image_path, "rb") as imgf:
120
  image_data = imgf.read()
 
187
  # Ask model for answer
188
  r2 = client.predict(q,"mistralai/Mixtral-8x7B-Instruct-v0.1",True,api_name="/ask_llm")
189
  result = f"### πŸ”Ž {q}\n\n{r2}\n\n{refs}"
190
+
191
+ #---------------------------------------------------------------
192
  # Speak results
193
+ #speech_synthesis_html(r2)
194
+
195
+ # Instead of speech_synthesis_html, use EdgeTTS now:
196
+ st.markdown(result)
197
 
198
+ # Speak main result
199
+ audio_file_main = speak_with_edge_tts(r2, voice="en-US-AriaNeural", rate=0, pitch=0)
200
+ st.write("### Audio Output for Main Result")
201
+ play_and_download_audio(audio_file_main)
202
+
203
+
204
+ # Speak references summaries
205
  summaries_text = "Here are the summaries from the references: " + refs.replace('"','')
206
+ audio_file_refs = speak_with_edge_tts(summaries_text, voice="en-US-AriaNeural", rate=0, pitch=0)
207
+ st.write("### Audio Output for References Summaries")
208
+ play_and_download_audio(audio_file_refs)
209
 
210
+ # Extract titles from refs and speak them
211
  titles = []
212
  for line in refs.split('\n'):
213
  m = re.search(r"\[([^\]]+)\]", line)
 
215
  titles.append(m.group(1))
216
  if titles:
217
  titles_text = "Here are the titles of the papers: " + ", ".join(titles)
218
+ audio_file_titles = speak_with_edge_tts(titles_text, voice="en-US-AriaNeural", rate=0, pitch=0)
219
+ st.write("### Audio Output for Paper Titles")
220
+ play_and_download_audio(audio_file_titles)
221
+ # --------------------------------------------
222
+
223
 
224
  st.markdown(result)
225
  elapsed = time.time()-start
 
331
  if st.button("πŸ—‘",key="d"+f):
332
  os.remove(f)
333
  st.experimental_rerun()
 
334
  def main():
335
  st.sidebar.markdown("### 🚲BikeAIπŸ† Multi-Agent Research AI")
336
  tab_main = st.radio("Action:",["🎀 Voice Input","πŸ“Έ Media Gallery","πŸ” Search ArXiv","πŸ“ File Editor"],horizontal=True)
337
 
 
338
  model_choice = st.sidebar.radio("AI Model:", ["Arxiv","GPT-4o","Claude-3","GPT+Claude+Arxiv"], index=0)
339
 
340
+ # A simple component placeholder
341
  mycomponent = components.declare_component("mycomponent", path="mycomponent")
342
  val = mycomponent(my_input_value="Hello")
343
  if val:
 
347
  elif model_choice == "Claude-3":
348
  process_with_claude(user_input)
349
  elif model_choice == "Arxiv":
 
350
  st.subheader("Arxiv Only Results:")
351
  perform_ai_lookup(user_input)
352
  else:
 
353
  col1,col2,col3=st.columns(3)
354
  with col1:
355
  st.subheader("GPT-4o Omni:")
 
380
  st.subheader("Arxiv Only Results:")
381
  perform_ai_lookup(user_text)
382
  else:
 
383
  col1,col2,col3=st.columns(3)
384
  with col1:
385
  st.subheader("GPT-4o Omni:")
 
423
  display_file_manager()
424
 
425
  if __name__=="__main__":
426
+ main()