Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import asyncio
|
3 |
+
import edge_tts
|
4 |
+
import speech_recognition as sr
|
5 |
+
from pydub import AudioSegment
|
6 |
+
from pydub.playback import play
|
7 |
+
import os
|
8 |
+
from huggingface_hub import InferenceClient
|
9 |
+
import whisper
|
10 |
+
import torch
|
11 |
+
from io import BytesIO
|
12 |
+
import tempfile
|
13 |
+
|
14 |
+
# Get the Hugging Face token from environment variable
|
15 |
+
hf_token = os.environ.get("HF_TOKEN")
|
16 |
+
if not hf_token:
|
17 |
+
raise ValueError("HF_TOKEN environment variable is not set")
|
18 |
+
|
19 |
+
# Initialize the Hugging Face Inference Client
|
20 |
+
client = InferenceClient(
|
21 |
+
"mistralai/Mistral-Nemo-Instruct-2407",
|
22 |
+
token=hf_token
|
23 |
+
)
|
24 |
+
|
25 |
+
# Load the Whisper model
|
26 |
+
whisper_model = whisper.load_model("base", device='cuda')
|
27 |
+
|
28 |
+
# Initialize an empty chat history
|
29 |
+
chat_history = []
|
30 |
+
|
31 |
+
# ... (rest of the functions remain the same) ...
|
32 |
+
|
33 |
+
# Define the Gradio interface
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("# AI Voice Assistant")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column():
|
39 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Speak here")
|
40 |
+
text_input = gr.Textbox(label="Or type your message here")
|
41 |
+
|
42 |
+
with gr.Column():
|
43 |
+
chat_output = gr.Textbox(label="AI Response")
|
44 |
+
audio_output = gr.Audio(label="AI Voice Response")
|
45 |
+
|
46 |
+
audio_button = gr.Button("Send Audio")
|
47 |
+
text_button = gr.Button("Send Text")
|
48 |
+
|
49 |
+
audio_button.click(transcribe_and_chat, inputs=audio_input, outputs=[chat_output, audio_output])
|
50 |
+
text_button.click(lambda x: asyncio.run(chat_with_ai(x, [])), inputs=text_input, outputs=[chat_output, audio_output])
|
51 |
+
|
52 |
+
# Launch the Gradio app
|
53 |
+
if __name__ == "__main__":
|
54 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|