File size: 1,547 Bytes
d45e0d4
839f819
c95ed49
 
 
 
7dafaa4
d45e0d4
 
c95ed49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d45e0d4
c95ed49
 
d45e0d4
 
c95ed49
d45e0d4
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import gradio as gr
import asyncio
import sys
import os
from pydub import AudioSegment

sys.path.append('voice_chat') 
from voice_chat import respond

def process_audio(audio):
    if audio is None:
        return None
    
    if isinstance(audio, tuple):  # Gradio returns a tuple (path, sampling_rate)
        audio_path = audio[0]
    else:
        audio_path = audio
    
    # Ensure the file exists
    if not os.path.exists(audio_path):
        raise FileNotFoundError(f"Audio file not found: {audio_path}")
    
    # Convert audio to wav if it's not already
    if not audio_path.endswith('.wav'):
        audio = AudioSegment.from_file(audio_path)
        wav_path = audio_path.rsplit('.', 1)[0] + '.wav'
        audio.export(wav_path, format="wav")
        audio_path = wav_path
    
    return audio_path

async def voice_chat(audio):
    try:
        processed_audio = process_audio(audio)
        if processed_audio is None:
            return None
        
        response_audio = await respond(processed_audio)
        return response_audio
    except Exception as e:
        print(f"Error in voice_chat: {str(e)}")
        return None

interface = gr.Interface(
    fn=lambda audio: asyncio.run(voice_chat(audio)),
    inputs=gr.Audio(type="filepath"),
    outputs=gr.Audio(),
    title="Voice Chat with AI Assistant",
    description="Upload an audio file or record using the microphone, and the AI will respond with voice.",
    examples=None,
    theme="default"
)

if __name__ == "__main__":
    interface.launch(debug=True)