Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,35 @@
|
|
|
|
|
| 1 |
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 |
"text-generation",
|
| 7 |
model="microsoft/Phi-3-mini-4k-instruct",
|
| 8 |
-
device_map="auto",
|
| 9 |
-
torch_dtype="auto"
|
| 10 |
)
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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},
|
|
@@ -27,14 +39,11 @@ def process_audio(audio_path):
|
|
| 27 |
max_new_tokens=100,
|
| 28 |
do_sample=False,
|
| 29 |
)
|
| 30 |
-
# Chat-formatted input returns the full conversation in "generated_text";
|
| 31 |
-
# the assistant's reply is the last message in that list.
|
| 32 |
reply = output[0]["generated_text"][-1]["content"].strip()
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return reply, (speech["sampling_rate"], audio)
|
| 38 |
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=process_audio,
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline, VitsModel, AutoTokenizer
|
| 4 |
|
| 5 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 6 |
+
|
| 7 |
llm_pipe = pipeline(
|
| 8 |
"text-generation",
|
| 9 |
model="microsoft/Phi-3-mini-4k-instruct",
|
| 10 |
+
device_map="auto",
|
| 11 |
+
torch_dtype="auto",
|
| 12 |
)
|
| 13 |
+
|
| 14 |
+
tts_model = VitsModel.from_pretrained("facebook/mms-tts-eng")
|
| 15 |
+
tts_tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def text_to_speech(text):
|
| 19 |
+
inputs = tts_tokenizer(text, return_tensors="pt")
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
output = tts_model(**inputs).waveform
|
| 22 |
+
audio = output.squeeze().cpu().numpy()
|
| 23 |
+
sampling_rate = tts_model.config.sampling_rate
|
| 24 |
+
return sampling_rate, audio
|
| 25 |
+
|
| 26 |
|
| 27 |
def process_audio(audio_path):
|
| 28 |
if audio_path is None:
|
| 29 |
return "Please record something first.", None
|
| 30 |
|
|
|
|
| 31 |
user_text = asr_pipe(audio_path)["text"].strip()
|
| 32 |
|
|
|
|
| 33 |
messages = [
|
| 34 |
{"role": "system", "content": "You are a helpful voice assistant. Answer in 1-3 short sentences."},
|
| 35 |
{"role": "user", "content": user_text},
|
|
|
|
| 39 |
max_new_tokens=100,
|
| 40 |
do_sample=False,
|
| 41 |
)
|
|
|
|
|
|
|
| 42 |
reply = output[0]["generated_text"][-1]["content"].strip()
|
| 43 |
|
| 44 |
+
sampling_rate, audio = text_to_speech(reply)
|
| 45 |
+
return reply, (sampling_rate, audio)
|
| 46 |
+
|
|
|
|
| 47 |
|
| 48 |
demo = gr.Interface(
|
| 49 |
fn=process_audio,
|