Update app.py
Browse files
app.py
CHANGED
@@ -173,7 +173,10 @@ with gr.Blocks() as demo:
|
|
173 |
pdf_button = gr.Button("Ask")
|
174 |
|
175 |
with gr.Tab("Voice Chat"):
|
176 |
-
|
|
|
|
|
|
|
177 |
audio_query = gr.Textbox(label="Ask about the transcription")
|
178 |
audio_output = gr.Textbox(label="Response", interactive=False)
|
179 |
audio_button = gr.Button("Ask")
|
@@ -188,18 +191,27 @@ with gr.Blocks() as demo:
|
|
188 |
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], image_output)
|
189 |
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
190 |
|
191 |
-
# For Voice Chat
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
temperature, top_p, max_output_tokens
|
196 |
-
)
|
197 |
-
|
|
|
198 |
|
199 |
# Fix: Clear button resets all necessary fields correctly
|
200 |
clear_button.click(
|
201 |
clear_chat,
|
202 |
-
outputs=[
|
203 |
image_url, image_query, image_url_output,
|
204 |
text_query, text_output,
|
205 |
image_text_query, image_output,
|
|
|
173 |
pdf_button = gr.Button("Ask")
|
174 |
|
175 |
with gr.Tab("Voice Chat"):
|
176 |
+
# Record Audio Component for Voice Chat
|
177 |
+
audio_record = gr.Audio(label="Record your Voice", type="bytes", source="microphone", show_label=True)
|
178 |
+
# Upload Audio File Component
|
179 |
+
audio_upload = gr.File(label="Or Upload an Audio File", type="file", file_types=["audio/wav", "audio/mp3"])
|
180 |
audio_query = gr.Textbox(label="Ask about the transcription")
|
181 |
audio_output = gr.Textbox(label="Response", interactive=False)
|
182 |
audio_button = gr.Button("Ask")
|
|
|
191 |
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], image_output)
|
192 |
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
193 |
|
194 |
+
# For Voice Chat (record or upload audio and process query)
|
195 |
+
def process_audio(audio, query, temperature, top_p, max_output_tokens):
|
196 |
+
# Check if audio is recorded or uploaded
|
197 |
+
if audio is None:
|
198 |
+
return "Please either record or upload an audio file."
|
199 |
+
|
200 |
+
# Process the audio (either from recording or upload)
|
201 |
+
transcription = transcribe_audio(audio, api_key)
|
202 |
+
if transcription.startswith("Error"):
|
203 |
+
return transcription # Return transcription error
|
204 |
+
return query_openai(
|
205 |
+
[{"role": "user", "content": [{"type": "text", "text": transcription}, {"type": "text", "text": query}]}],
|
206 |
temperature, top_p, max_output_tokens
|
207 |
+
)
|
208 |
+
|
209 |
+
audio_button.click(process_audio, [audio_record, audio_upload, audio_query, temperature, top_p, max_output_tokens], audio_output)
|
210 |
|
211 |
# Fix: Clear button resets all necessary fields correctly
|
212 |
clear_button.click(
|
213 |
clear_chat,
|
214 |
+
outputs=[
|
215 |
image_url, image_query, image_url_output,
|
216 |
text_query, text_output,
|
217 |
image_text_query, image_output,
|