Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -79,25 +79,24 @@ async def text_to_speech(text):
|
|
79 |
await communicate.save(tmp_path)
|
80 |
return tmp_path
|
81 |
|
82 |
-
def
|
83 |
if audio is None:
|
84 |
-
return
|
85 |
|
86 |
text = speech_to_text(audio)
|
87 |
response = generate(text, history)
|
88 |
history.append((text, response))
|
89 |
|
90 |
-
return
|
91 |
|
92 |
-
def
|
93 |
response = generate(text, history)
|
94 |
history.append((text, response))
|
95 |
-
return
|
96 |
|
97 |
-
async def generate_audio(
|
98 |
-
if
|
99 |
-
|
100 |
-
audio_path = await text_to_speech(last_response)
|
101 |
return audio_path
|
102 |
return None
|
103 |
|
@@ -106,14 +105,31 @@ with gr.Blocks() as demo:
|
|
106 |
gr.Markdown("# Mood-Based Music Recommender with Voice Chat")
|
107 |
|
108 |
chatbot = gr.Chatbot()
|
109 |
-
audio_input = gr.Audio(
|
110 |
text_input = gr.Textbox(placeholder="Type your message here...")
|
111 |
audio_output = gr.Audio(label="AI Response")
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
demo.launch()
|
|
|
79 |
await communicate.save(tmp_path)
|
80 |
return tmp_path
|
81 |
|
82 |
+
def process_audio(audio, history):
|
83 |
if audio is None:
|
84 |
+
return history, None
|
85 |
|
86 |
text = speech_to_text(audio)
|
87 |
response = generate(text, history)
|
88 |
history.append((text, response))
|
89 |
|
90 |
+
return history, response
|
91 |
|
92 |
+
def process_text(text, history):
|
93 |
response = generate(text, history)
|
94 |
history.append((text, response))
|
95 |
+
return history, response
|
96 |
|
97 |
+
async def generate_audio(response):
|
98 |
+
if response:
|
99 |
+
audio_path = await text_to_speech(response)
|
|
|
100 |
return audio_path
|
101 |
return None
|
102 |
|
|
|
105 |
gr.Markdown("# Mood-Based Music Recommender with Voice Chat")
|
106 |
|
107 |
chatbot = gr.Chatbot()
|
108 |
+
audio_input = gr.Audio(source="microphone", type="filepath")
|
109 |
text_input = gr.Textbox(placeholder="Type your message here...")
|
110 |
audio_output = gr.Audio(label="AI Response")
|
111 |
|
112 |
+
state = gr.State([])
|
113 |
+
|
114 |
+
audio_input.change(
|
115 |
+
process_audio,
|
116 |
+
inputs=[audio_input, state],
|
117 |
+
outputs=[chatbot, state]
|
118 |
+
).then(
|
119 |
+
generate_audio,
|
120 |
+
inputs=[state],
|
121 |
+
outputs=[audio_output]
|
122 |
+
)
|
123 |
+
|
124 |
+
text_input.submit(
|
125 |
+
process_text,
|
126 |
+
inputs=[text_input, state],
|
127 |
+
outputs=[chatbot, state]
|
128 |
+
).then(
|
129 |
+
generate_audio,
|
130 |
+
inputs=[state],
|
131 |
+
outputs=[audio_output]
|
132 |
+
)
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
demo.launch()
|