Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import librosa
|
4 |
+
from transformers import pipeline
|
5 |
+
import tempfile
|
6 |
+
from functools import lru_cache
|
7 |
+
|
8 |
+
# Cache the model to avoid reloading on every interaction
|
9 |
+
@lru_cache(maxsize=1)
|
10 |
+
def load_model():
|
11 |
+
return pipeline(
|
12 |
+
model='fixie-ai/ultravox-v0_5-llama-3_2-1b',
|
13 |
+
trust_remote_code=True,
|
14 |
+
device_map="auto" # Automatically uses GPU if available
|
15 |
+
)
|
16 |
+
|
17 |
+
def process_audio(audio_file, user_message):
|
18 |
+
try:
|
19 |
+
# Load audio (supports file upload or microphone input)
|
20 |
+
if isinstance(audio_file, (str, tempfile._TemporaryFileWrapper)):
|
21 |
+
audio_path = audio_file.name if hasattr(audio_file, 'name') else audio_file
|
22 |
+
audio, sr = librosa.load(audio_path, sr=16000)
|
23 |
+
else: # Handle direct numpy array from microphone
|
24 |
+
sr, audio = audio_file
|
25 |
+
|
26 |
+
# Initialize conversation
|
27 |
+
turns = [
|
28 |
+
{
|
29 |
+
"role": "system",
|
30 |
+
"content": "You are a friendly and helpful AI assistant. Respond conversationally to the user's audio input."
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"role": "user",
|
34 |
+
"content": user_message if user_message else "Describe what you heard in the audio."
|
35 |
+
}
|
36 |
+
]
|
37 |
+
|
38 |
+
# Get model prediction
|
39 |
+
pipe = load_model()
|
40 |
+
result = pipe({'audio': audio, 'turns': turns, 'sampling_rate': sr}, max_new_tokens=100)
|
41 |
+
|
42 |
+
return result[-1]["content"]
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
return f"Error processing audio: {str(e)}"
|
46 |
+
|
47 |
+
# Gradio UI
|
48 |
+
with gr.Blocks(title="UltraVox Audio Assistant") as demo:
|
49 |
+
gr.Markdown("## 🎤 UltraVox Audio Assistant")
|
50 |
+
gr.Markdown("Upload an audio file or speak via microphone, then ask questions about it.")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
audio_input = gr.Audio(
|
54 |
+
sources=["upload", "microphone"],
|
55 |
+
type="filepath",
|
56 |
+
label="Input Audio"
|
57 |
+
)
|
58 |
+
text_input = gr.Textbox(
|
59 |
+
label="Your Question (Optional)",
|
60 |
+
placeholder="Ask me about the audio..."
|
61 |
+
)
|
62 |
+
|
63 |
+
submit_btn = gr.Button("Process")
|
64 |
+
output = gr.Textbox(label="AI Response", interactive=False)
|
65 |
+
|
66 |
+
submit_btn.click(
|
67 |
+
fn=process_audio,
|
68 |
+
inputs=[audio_input, text_input],
|
69 |
+
outputs=output
|
70 |
+
)
|
71 |
+
|
72 |
+
gr.Examples(
|
73 |
+
examples=[
|
74 |
+
["examples/weather_report.wav", "What's the weather forecast?"],
|
75 |
+
["examples/meeting_notes.mp3", "Summarize the key points"]
|
76 |
+
],
|
77 |
+
inputs=[audio_input, text_input]
|
78 |
+
)
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
demo.launch()
|