Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import mimetypes
|
|
| 6 |
from PIL import Image
|
| 7 |
import pytesseract
|
| 8 |
from gtts import gTTS
|
|
|
|
| 9 |
|
| 10 |
def synthesize_speech(text):
|
| 11 |
if not text.strip():
|
|
@@ -203,7 +204,7 @@ def create_chatbot():
|
|
| 203 |
) as demo:
|
| 204 |
|
| 205 |
chatbot = gr.Chatbot(type='messages')
|
| 206 |
-
audio_output = gr.Audio(
|
| 207 |
state = gr.State([])
|
| 208 |
|
| 209 |
with gr.Row():
|
|
@@ -253,21 +254,26 @@ def create_chatbot():
|
|
| 253 |
if file_input:
|
| 254 |
file_text = read_uploaded_file(file_input)
|
| 255 |
message = f"{message or ''}\n\n[File Content]:\n{file_text}"
|
|
|
|
| 256 |
messages_for_gradio, updated_history = chat_with_ai(message, history, role, None)
|
| 257 |
-
|
| 258 |
-
last_reply = updated_history[-1][1] if updated_history else ""
|
| 259 |
-
audio_path = synthesize_speech(last_reply)
|
| 260 |
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
def transcribe_and_respond(audio_file, history, role):
|
| 264 |
message = transcribe_audio(audio_file)
|
| 265 |
messages_for_gradio, updated_history = chat_with_ai(message, history, role, None)
|
| 266 |
-
|
| 267 |
-
last_reply = updated_history[-1][1] if updated_history else ""
|
| 268 |
-
audio_path = synthesize_speech(last_reply)
|
| 269 |
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
audio_btn.click(lambda: gr.Audio(visible=True), None, audio_input)
|
| 273 |
file_btn.click(lambda: gr.File(visible=True), None, file_input)
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import pytesseract
|
| 8 |
from gtts import gTTS
|
| 9 |
+
import uuid
|
| 10 |
|
| 11 |
def synthesize_speech(text):
|
| 12 |
if not text.strip():
|
|
|
|
| 204 |
) as demo:
|
| 205 |
|
| 206 |
chatbot = gr.Chatbot(type='messages')
|
| 207 |
+
audio_output = gr.Audio(type="filepath", visible=False, autoplay=True)
|
| 208 |
state = gr.State([])
|
| 209 |
|
| 210 |
with gr.Row():
|
|
|
|
| 254 |
if file_input:
|
| 255 |
file_text = read_uploaded_file(file_input)
|
| 256 |
message = f"{message or ''}\n\n[File Content]:\n{file_text}"
|
| 257 |
+
|
| 258 |
messages_for_gradio, updated_history = chat_with_ai(message, history, role, None)
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
+
reply = updated_history[-1][1]
|
| 261 |
+
tts = gTTS(reply)
|
| 262 |
+
audio_filename = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 263 |
+
tts.save(audio_filename)
|
| 264 |
+
|
| 265 |
+
return messages_for_gradio, updated_history, audio_filename
|
| 266 |
|
| 267 |
def transcribe_and_respond(audio_file, history, role):
|
| 268 |
message = transcribe_audio(audio_file)
|
| 269 |
messages_for_gradio, updated_history = chat_with_ai(message, history, role, None)
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
+
reply = updated_history[-1][1]
|
| 272 |
+
tts = gTTS(reply)
|
| 273 |
+
audio_filename = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 274 |
+
tts.save(audio_filename)
|
| 275 |
+
|
| 276 |
+
return message, messages_for_gradio, updated_history, audio_filename
|
| 277 |
|
| 278 |
audio_btn.click(lambda: gr.Audio(visible=True), None, audio_input)
|
| 279 |
file_btn.click(lambda: gr.File(visible=True), None, file_input)
|