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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -40
app.py CHANGED
@@ -1,41 +1,58 @@
1
- import streamlit as st
2
- import asyncio
3
- import os
4
- from llm_response import get_response
5
- from male_voice import text_to_speech
6
-
7
- st.set_page_config(page_title="Ajoy Prasad Bot", layout="centered")
8
- st.title("Bot on behalf of Ajoy Prasad")
9
-
10
- tab_text, tab_voice = st.tabs(["Type Your Question", "Voice Input (Coming Soon)"])
11
-
12
- with tab_text:
13
- user_input = st.text_input(
14
- "Type your question and press Enter:",
15
- placeholder="Ask Ajoy anything...",
16
- key="text_input",
17
- label_visibility="collapsed"
18
- )
19
-
20
- if user_input:
21
- with st.spinner("Ajoy is thinking..."):
22
- try:
23
- response = asyncio.run(get_response(user_input))
24
- audio_file = "response.wav"
25
- asyncio.run(text_to_speech(response, audio_file))
26
-
27
- if os.path.exists(audio_file):
28
- st.audio(audio_file, format="audio/wav", autoplay=True)
29
- else:
30
- st.warning("Audio file not generated.")
31
-
32
- st.markdown("### **Ajoy's Answer:**")
33
- st.write(response)
34
-
35
- except Exception as e:
36
- st.error(f"An error occurred: {str(e)}")
37
- st.info("Please try again with a different question.")
38
-
39
- with tab_voice:
40
- st.info("Voice input feature coming soon!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  st.write("You'll be able to speak your question directly to Ajoy.")
 
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
+
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
+ 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.")