ajoy0071998 commited on
Commit
5bc1570
·
verified ·
1 Parent(s): 5ac1f8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -1,16 +1,17 @@
 
1
  import streamlit as st
2
- import asyncio
3
- import os
4
- from datetime import datetime # For unique filenames
5
  from llm_response import get_response
6
  from male_voice import text_to_speech
7
 
8
  st.set_page_config(page_title="Ajoy Prasad Bot", layout="centered")
9
  st.title("Bot on behalf of Ajoy Prasad")
10
 
11
- # Persistent directory
12
- PERSISTENT_DIR = "/data/audio_responses"
13
- os.makedirs(PERSISTENT_DIR, exist_ok=True)
14
 
15
  tab_text, tab_voice = st.tabs(["Type Your Question", "Voice Input (Coming Soon)"])
16
 
@@ -21,38 +22,40 @@ with tab_text:
21
  key="text_input",
22
  label_visibility="collapsed"
23
  )
 
24
  if user_input:
25
- with st.spinner("Ajoy is thinking..."):
26
  try:
27
  response = asyncio.run(get_response(user_input))
28
-
29
- # Generate unique filename
30
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
31
- audio_file = os.path.join(PERSISTENT_DIR, f"response_{timestamp}.wav")
32
-
33
- asyncio.run(text_to_speech(response, audio_file))
34
-
35
- if os.path.exists(audio_file):
36
- # Serve from persistent path
37
- st.audio(audio_file, format="audio/wav", autoplay=True)
38
-
39
- # Optional: Provide download link for "use anywhere"
40
- with open(audio_file, "rb") as f:
41
- st.download_button(
42
- label="Download Audio",
43
- data=f.read(),
44
- file_name=os.path.basename(audio_file),
45
- mime="audio/wav"
46
- )
47
  else:
48
- st.warning("Audio file not generated.")
49
-
50
- st.markdown("### **Ajoy's Answer:**")
51
- st.write(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  except Exception as e:
53
- st.error(f"An error occurred: {str(e)}")
54
- st.info("Please try again with a different question.")
55
 
56
  with tab_voice:
57
- st.info("Voice input feature coming soon!")
58
- st.write("You'll be able to speak your question directly to Ajoy.")
 
1
+ # app.py
2
  import streamlit as st
3
+ import asyncio, os
4
+ from datetime import datetime
5
+ from pathlib import Path
6
  from llm_response import get_response
7
  from male_voice import text_to_speech
8
 
9
  st.set_page_config(page_title="Ajoy Prasad Bot", layout="centered")
10
  st.title("Bot on behalf of Ajoy Prasad")
11
 
12
+ # ---------- PERSISTENT STORAGE ----------
13
+ PERSISTENT_DIR = Path("/data/audio")
14
+ PERSISTENT_DIR.mkdir(parents=True, exist_ok=True)
15
 
16
  tab_text, tab_voice = st.tabs(["Type Your Question", "Voice Input (Coming Soon)"])
17
 
 
22
  key="text_input",
23
  label_visibility="collapsed"
24
  )
25
+
26
  if user_input:
27
+ with st.spinner("Ajoy is thinking"):
28
  try:
29
  response = asyncio.run(get_response(user_input))
30
+ if not response.strip():
31
+ st.warning("LLM returned empty text.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  else:
33
+ # unique filename → never overwritten
34
+ ts = datetime.now().strftime("%Y%m%d_%H%M%S")
35
+ audio_path = PERSISTENT_DIR / f"response_{ts}.wav"
36
+
37
+ success = asyncio.run(text_to_speech(response, audio_path))
38
+
39
+ if success and audio_path.is_file() and audio_path.stat().st_size > 1000:
40
+ st.audio(str(audio_path), format="audio/wav", autoplay=True)
41
+
42
+ # ---- download button (works anywhere) ----
43
+ with open(audio_path, "rb") as f:
44
+ st.download_button(
45
+ "Download Audio",
46
+ data=f.read(),
47
+ file_name=f"ajoy_{ts}.wav",
48
+ mime="audio/wav"
49
+ )
50
+ else:
51
+ st.error("No audio was generated – check logs below.")
52
+ st.code(open("streamlit.log").read() if Path("streamlit.log").exists() else "No log file yet")
53
+
54
+ st.markdown("### **Ajoy's Answer:**")
55
+ st.write(response)
56
+
57
  except Exception as e:
58
+ st.exception(e)
 
59
 
60
  with tab_voice:
61
+ st.info("Voice input feature coming soon!")