Chat and voice feature with history retained
Browse files
app.py
CHANGED
@@ -13,34 +13,51 @@ load_dotenv()
|
|
13 |
#Front end using streamlit
|
14 |
def frontend():
|
15 |
st.title("Voice AI Demo")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
status_placeholder = st.empty()
|
17 |
-
status_placeholder.write("Press Mic button to start asking question")
|
|
|
18 |
recorded_audio = audio_recorder(sample_rate=8000)
|
19 |
text = st.chat_input()
|
20 |
-
|
|
|
21 |
status_placeholder.write("Getting response...")
|
22 |
-
response = answer(
|
23 |
-
status_placeholder.write("
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
status_placeholder.write("Press mic button again to ask more questions")
|
26 |
-
|
27 |
-
|
28 |
-
st.
|
|
|
|
|
|
|
|
|
29 |
elif recorded_audio:
|
30 |
status_placeholder.write("Converting audio...")
|
31 |
data_to_file(recorded_audio)
|
32 |
-
status_placeholder.write("Audio conversion done.")
|
33 |
status_placeholder.write("Uploading audio...")
|
34 |
transcription = audio_to_text("temp_audio.wav")
|
35 |
-
status_placeholder.write("Transcription
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
st.write(
|
42 |
-
st.
|
43 |
-
|
44 |
|
45 |
#Fuction to convert audio data to audio file
|
46 |
def data_to_file(recorded_audio):
|
@@ -78,10 +95,9 @@ def answer(user_question):
|
|
78 |
return answer
|
79 |
|
80 |
# Audio conversion
|
81 |
-
async def convert_audio(text):
|
82 |
-
filename = "output.mp3"
|
83 |
voice = "fr-FR-VivienneMultilingualNeural"
|
84 |
-
|
85 |
-
await
|
86 |
|
87 |
frontend()
|
|
|
13 |
#Front end using streamlit
|
14 |
def frontend():
|
15 |
st.title("Voice AI Demo")
|
16 |
+
|
17 |
+
# Initialize session state variables
|
18 |
+
if "conversation" not in st.session_state:
|
19 |
+
st.session_state.conversation = [] # Stores (question, answer, audio_filename)
|
20 |
+
if "audio_count" not in st.session_state:
|
21 |
+
st.session_state.audio_count = 1 # Start numbering audio files from output1.wav
|
22 |
+
|
23 |
status_placeholder = st.empty()
|
24 |
+
status_placeholder.write("Press Mic button to start asking a question")
|
25 |
+
|
26 |
recorded_audio = audio_recorder(sample_rate=8000)
|
27 |
text = st.chat_input()
|
28 |
+
|
29 |
+
def process_input(user_input):
|
30 |
status_placeholder.write("Getting response...")
|
31 |
+
response = answer(user_input)
|
32 |
+
status_placeholder.write("Converting response to audio...")
|
33 |
+
|
34 |
+
# Generate unique audio filename
|
35 |
+
audio_filename = f"output{st.session_state.audio_count}.wav"
|
36 |
+
asyncio.run(convert_audio(response, audio_filename))
|
37 |
+
st.session_state.audio_count += 1 # Increment for next response
|
38 |
+
|
39 |
status_placeholder.write("Press mic button again to ask more questions")
|
40 |
+
|
41 |
+
# Append (question, answer, audio_filename) to conversation history
|
42 |
+
st.session_state.conversation.append((f"Q: {user_input}", f"A: {response}", audio_filename))
|
43 |
+
|
44 |
+
# Handle user input
|
45 |
+
if text:
|
46 |
+
process_input(text)
|
47 |
elif recorded_audio:
|
48 |
status_placeholder.write("Converting audio...")
|
49 |
data_to_file(recorded_audio)
|
|
|
50 |
status_placeholder.write("Uploading audio...")
|
51 |
transcription = audio_to_text("temp_audio.wav")
|
52 |
+
status_placeholder.write("Transcription completed.")
|
53 |
+
process_input(transcription)
|
54 |
+
|
55 |
+
# Display full conversation history
|
56 |
+
for i, (q, a, audio_file) in enumerate(st.session_state.conversation):
|
57 |
+
st.write(q)
|
58 |
+
st.write(a)
|
59 |
+
st.audio(audio_file, format="audio/wav", loop=False, autoplay=(i == len(st.session_state.conversation) - 1))
|
60 |
+
|
61 |
|
62 |
#Fuction to convert audio data to audio file
|
63 |
def data_to_file(recorded_audio):
|
|
|
95 |
return answer
|
96 |
|
97 |
# Audio conversion
|
98 |
+
async def convert_audio(text, filename):
|
|
|
99 |
voice = "fr-FR-VivienneMultilingualNeural"
|
100 |
+
communicate = edge_tts.Communicate(text, voice)
|
101 |
+
await communicate.save(filename)
|
102 |
|
103 |
frontend()
|