syedmudassir16 commited on
Commit
15196a0
·
verified ·
1 Parent(s): 050d201

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
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 chatbot(audio, history):
83
  if audio is None:
84
- return "", history
85
 
86
  text = speech_to_text(audio)
87
  response = generate(text, history)
88
  history.append((text, response))
89
 
90
- return "", history
91
 
92
- def text_input(text, history):
93
  response = generate(text, history)
94
  history.append((text, response))
95
- return "", history
96
 
97
- async def generate_audio(history):
98
- if len(history) > 0:
99
- last_response = history[-1][1]
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(sources="microphone", type="filepath")
110
  text_input = gr.Textbox(placeholder="Type your message here...")
111
  audio_output = gr.Audio(label="AI Response")
112
 
113
- audio_input.change(chatbot, inputs=[audio_input, chatbot], outputs=[audio_input, chatbot])
114
- text_input.submit(text_input, inputs=[text_input, chatbot], outputs=[text_input, chatbot])
115
-
116
- chatbot.change(generate_audio, inputs=[chatbot], outputs=[audio_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()