Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
ASR_MODEL = "openai/whisper-tiny" # small/medium if you switch to ZeroGPU
|
| 6 |
+
ZSC_MODEL = "facebook/bart-large-mnli" # for multilingual use: "MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7"
|
| 7 |
+
|
| 8 |
+
asr = pipeline("automatic-speech-recognition", model=ASR_MODEL)
|
| 9 |
+
zsc = pipeline("zero-shot-classification", model=ZSC_MODEL)
|
| 10 |
+
|
| 11 |
+
DEFAULT_INTENTS = [
|
| 12 |
+
"turn_on_lights","turn_off_lights","volume_up","volume_down",
|
| 13 |
+
"start_music","pause_music","set_timer","cancel_timer",
|
| 14 |
+
"open_calendar","create_note","start_recording","stop_recording"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
def tool_turn_on_lights():
|
| 18 |
+
return "Lights → ON"
|
| 19 |
+
|
| 20 |
+
def tool_turn_off_lights():
|
| 21 |
+
return "Lights → OFF"
|
| 22 |
+
|
| 23 |
+
def tool_volume_up():
|
| 24 |
+
return "Volume → UP"
|
| 25 |
+
|
| 26 |
+
def tool_volume_down():
|
| 27 |
+
return "Volume → DOWN"
|
| 28 |
+
|
| 29 |
+
def tool_start_music():
|
| 30 |
+
return "Music → PLAY"
|
| 31 |
+
|
| 32 |
+
def tool_pause_music():
|
| 33 |
+
return "Music → PAUSE"
|
| 34 |
+
|
| 35 |
+
def tool_set_timer():
|
| 36 |
+
return "Timer → 5 min (demo)"
|
| 37 |
+
|
| 38 |
+
def tool_cancel_timer():
|
| 39 |
+
return "Timer → CANCELLED"
|
| 40 |
+
|
| 41 |
+
def tool_open_calendar():
|
| 42 |
+
return "Calendar → OPENED"
|
| 43 |
+
|
| 44 |
+
def tool_create_note(text):
|
| 45 |
+
return f"Note saved: '{text[:60]}'"
|
| 46 |
+
|
| 47 |
+
def tool_start_recording():
|
| 48 |
+
return "Recording → STARTED"
|
| 49 |
+
|
| 50 |
+
def tool_stop_recording():
|
| 51 |
+
return "Recording → STOPPED"
|
| 52 |
+
|
| 53 |
+
TOOLS = {
|
| 54 |
+
"turn_on_lights": tool_turn_on_lights,
|
| 55 |
+
"turn_off_lights": tool_turn_off_lights,
|
| 56 |
+
"volume_up": tool_volume_up,
|
| 57 |
+
"volume_down": tool_volume_down,
|
| 58 |
+
"start_music": tool_start_music,
|
| 59 |
+
"pause_music": tool_pause_music,
|
| 60 |
+
"set_timer": tool_set_timer,
|
| 61 |
+
"cancel_timer": tool_cancel_timer,
|
| 62 |
+
"open_calendar": tool_open_calendar,
|
| 63 |
+
"create_note": tool_create_note,
|
| 64 |
+
"start_recording": tool_start_recording,
|
| 65 |
+
"stop_recording": tool_stop_recording,
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
def parse_intents(custom):
|
| 69 |
+
if not custom or not custom.strip():
|
| 70 |
+
return DEFAULT_INTENTS
|
| 71 |
+
return [t.strip() for t in custom.split(",") if t.strip()]
|
| 72 |
+
|
| 73 |
+
def agent(audio_path, custom_intents, history):
|
| 74 |
+
if not audio_path:
|
| 75 |
+
return gr.update(), gr.update(), "No audio.", history
|
| 76 |
+
transcript = asr(audio_path)["text"].strip()
|
| 77 |
+
if not transcript:
|
| 78 |
+
return gr.update(), gr.update(), "No speech detected.", history
|
| 79 |
+
|
| 80 |
+
intents = parse_intents(custom_intents)
|
| 81 |
+
out = zsc(transcript, candidate_labels=intents, multi_label=False)
|
| 82 |
+
labels = out["labels"]
|
| 83 |
+
scores = out["scores"]
|
| 84 |
+
top3 = {labels[i]: float(scores[i]) for i in range(min(3, len(labels)))}
|
| 85 |
+
|
| 86 |
+
chosen = labels[0]
|
| 87 |
+
if chosen == "create_note":
|
| 88 |
+
result = TOOLS[chosen](transcript)
|
| 89 |
+
else:
|
| 90 |
+
result = TOOLS.get(chosen, lambda: f"No tool bound: {chosen}")()
|
| 91 |
+
|
| 92 |
+
stamp = time.strftime("%H:%M:%S")
|
| 93 |
+
history = history + [[f"User: {transcript}", f"Agent: {chosen} → {result}"]]
|
| 94 |
+
return top3, chosen, result, history
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(title="Voice Agent: ASR → Intent → Tools") as demo:
|
| 97 |
+
gr.Markdown("# 🎙️ Voice Agent\nSpeak or upload audio → transcript via Whisper → zero-shot intent → tool execution.")
|
| 98 |
+
with gr.Row():
|
| 99 |
+
audio = gr.Audio(sources=["microphone","upload"], type="filepath", label="Audio")
|
| 100 |
+
intents_box = gr.Textbox(label="Intents (comma-separated)", value=", ".join(DEFAULT_INTENTS))
|
| 101 |
+
run = gr.Button("Run")
|
| 102 |
+
topk = gr.Label(num_top_classes=3, label="Top-k Intents")
|
| 103 |
+
chosen = gr.Textbox(label="Chosen Intent")
|
| 104 |
+
result = gr.Textbox(label="Action Result")
|
| 105 |
+
chat = gr.Chatbot(label="Execution Log")
|
| 106 |
+
state = gr.State([])
|
| 107 |
+
|
| 108 |
+
run.click(agent, inputs=[audio, intents_box, state], outputs=[topk, chosen, result, chat], queue=True)
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
demo.launch()
|