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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -61
app.py CHANGED
@@ -1,61 +1,54 @@
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
-
18
- with tab_text:
19
- user_input = st.text_input(
20
- "Type your question and press Enter:",
21
- placeholder="Ask Ajoy anything...",
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!")
 
1
+ # app.py
2
+ import streamlit as st
3
+ import asyncio
4
+ import os
5
+ from pathlib import Path
6
+ from llm_response import get_response
7
+ from male_voice import text_to_speech_async
8
+
9
+ # Page config
10
+ st.set_page_config(page_title="Ajoy Prasad Bot", layout="centered")
11
+ st.title("Ajoy Prasad Bot")
12
+ st.caption("Ask me anything I’ll answer in text and voice!")
13
+
14
+ # Tabs
15
+ tab_text, tab_voice = st.tabs(["Type Your Question", "Voice Input (Coming Soon)"])
16
+
17
+ with tab_text:
18
+ user_input = st.text_input(
19
+ "Type your question and press Enter:",
20
+ placeholder="Ask Ajoy anything...",
21
+ key="text_input",
22
+ label_visibility="collapsed"
23
+ )
24
+
25
+ if user_input:
26
+ with st.spinner("Ajoy is thinking..."):
27
+ try:
28
+ # Get LLM response
29
+ response = asyncio.run(get_response(user_input))
30
+
31
+ # Generate unique audio file
32
+ audio_file = Path("response.wav")
33
+
34
+ # Generate speech (async, non-blocking)
35
+ success = asyncio.run(text_to_speech_async(response, audio_file))
36
+
37
+ # Show response
38
+ st.markdown("### **Ajoy's Answer:**")
39
+ st.write(response)
40
+
41
+ # Play audio
42
+ if success and audio_file.exists():
43
+ st.audio(str(audio_file), format="audio/wav", autoplay=True)
44
+ else:
45
+ st.warning("Audio could not be generated.")
46
+
47
+ except Exception as e:
48
+ st.error(f"An error occurred: {str(e)}")
49
+ st.info("Please try again with a different question.")
50
+
51
+ with tab_voice:
52
+ st.info("Voice input feature coming soon!")
53
+ st.write("You'll be able to speak your question directly to Ajoy.")
54
+ st.caption("Stay tuned for microphone support!")