Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
-
import sys
|
3 |
import asyncio
|
4 |
-
sys
|
|
|
|
|
|
|
|
|
5 |
from voice_chat import respond
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
interface = gr.Interface(
|
11 |
-
fn=voice_chat,
|
12 |
-
inputs=gr.Audio(type="filepath"),
|
13 |
outputs=gr.Audio(),
|
14 |
title="Voice Chat with AI Assistant",
|
15 |
-
description="
|
16 |
examples=None,
|
17 |
theme="default"
|
18 |
)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import asyncio
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
from pydub import AudioSegment
|
6 |
+
|
7 |
+
sys.path.append('/content/voice_chat') # Replace with the actual path
|
8 |
from voice_chat import respond
|
9 |
|
10 |
+
def process_audio(audio):
|
11 |
+
if audio is None:
|
12 |
+
return None
|
13 |
+
|
14 |
+
if isinstance(audio, tuple): # Gradio returns a tuple (path, sampling_rate)
|
15 |
+
audio_path = audio[0]
|
16 |
+
else:
|
17 |
+
audio_path = audio
|
18 |
+
|
19 |
+
# Ensure the file exists
|
20 |
+
if not os.path.exists(audio_path):
|
21 |
+
raise FileNotFoundError(f"Audio file not found: {audio_path}")
|
22 |
+
|
23 |
+
# Convert audio to wav if it's not already
|
24 |
+
if not audio_path.endswith('.wav'):
|
25 |
+
audio = AudioSegment.from_file(audio_path)
|
26 |
+
wav_path = audio_path.rsplit('.', 1)[0] + '.wav'
|
27 |
+
audio.export(wav_path, format="wav")
|
28 |
+
audio_path = wav_path
|
29 |
+
|
30 |
+
return audio_path
|
31 |
+
|
32 |
+
async def voice_chat(audio):
|
33 |
+
try:
|
34 |
+
processed_audio = process_audio(audio)
|
35 |
+
if processed_audio is None:
|
36 |
+
return None
|
37 |
+
|
38 |
+
response_audio = await respond(processed_audio)
|
39 |
+
return response_audio
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error in voice_chat: {str(e)}")
|
42 |
+
return None
|
43 |
+
|
44 |
interface = gr.Interface(
|
45 |
+
fn=lambda audio: asyncio.run(voice_chat(audio)),
|
46 |
+
inputs=gr.Audio(type="filepath"),
|
47 |
outputs=gr.Audio(),
|
48 |
title="Voice Chat with AI Assistant",
|
49 |
+
description="Upload an audio file or record using the microphone, and the AI will respond with voice.",
|
50 |
examples=None,
|
51 |
theme="default"
|
52 |
)
|