Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,22 +2,47 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 5 |
-
llm_pipe = pipeline(
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def process_audio(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 5 |
+
llm_pipe = pipeline(
|
| 6 |
+
"text-generation",
|
| 7 |
+
model="microsoft/Phi-3-mini-4k-instruct",
|
| 8 |
+
device_map="auto", # needs `pip install accelerate`; remove to run on CPU only
|
| 9 |
+
torch_dtype="auto",
|
| 10 |
+
)
|
| 11 |
+
tts_pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 12 |
|
| 13 |
+
def process_audio(audio_path):
|
| 14 |
+
if audio_path is None:
|
| 15 |
+
return "Please record something first.", None
|
| 16 |
|
| 17 |
+
# 1. Speech -> text
|
| 18 |
+
user_text = asr_pipe(audio_path)["text"].strip()
|
| 19 |
|
| 20 |
+
# 2. Text -> reply (chat format, reply only, capped length)
|
| 21 |
+
messages = [
|
| 22 |
+
{"role": "system", "content": "You are a helpful voice assistant. Answer in 1-3 short sentences."},
|
| 23 |
+
{"role": "user", "content": user_text},
|
| 24 |
+
]
|
| 25 |
+
reply = llm_pipe(
|
| 26 |
+
messages,
|
| 27 |
+
max_new_tokens=100,
|
| 28 |
+
return_full_text=False,
|
| 29 |
+
do_sample=False,
|
| 30 |
+
)[0]["generated_text"].strip()
|
| 31 |
|
| 32 |
+
# 3. Reply -> speech
|
| 33 |
+
speech = tts_pipe(reply)
|
| 34 |
+
audio = speech["audio"].squeeze()
|
| 35 |
+
return reply, (speech["sampling_rate"], audio)
|
| 36 |
|
| 37 |
+
demo = gr.Interface(
|
| 38 |
+
fn=process_audio,
|
| 39 |
+
inputs=gr.Audio(label="Audio Input", sources=["microphone"], type="filepath"),
|
| 40 |
+
outputs=[
|
| 41 |
+
gr.Textbox(label="Assistant reply"),
|
| 42 |
+
gr.Audio(label="Spoken reply", type="numpy"),
|
| 43 |
+
],
|
| 44 |
+
title="MyAlexa AI Assistant",
|
| 45 |
+
description="An AI Assistant That Will Answer Your Questions!",
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
demo.launch(inbrowser=True, debug=True)
|