File size: 1,471 Bytes
be7970b
6ca9967
050a485
a3cf65d
2dec0df
e4904ed
 
 
2dec0df
 
e4904ed
2dec0df
6ca9967
2dec0df
 
 
6ca9967
 
 
2dec0df
050a485
e4904ed
 
 
44d6c1d
e4904ed
78cfa7e
e4904ed
 
 
 
07d7fb4
e4904ed
 
 
07d7fb4
 
44d6c1d
2dec0df
 
 
78cfa7e
e4904ed
 
 
 
 
 
 
6ca9967
e4904ed
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
from transformers import pipeline

asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")

llm_pipe = pipeline(
    "text-generation",
    model="microsoft/Phi-3-mini-4k-instruct",
    device_map="auto",
    torch_dtype="auto",
)

tts_pipe = pipeline("text-to-speech", model="suno/bark")


def text_to_speech(text):
    speech = tts_pipe(text)
    audio = speech["audio"].squeeze()
    return speech["sampling_rate"], audio


def process_audio(audio_path):
    if audio_path is None:
        return "Please record something first.", None

    user_text = asr_pipe(audio_path)["text"].strip()

    messages = [
        {"role": "system", "content": "You are a helpful voice assistant. Answer in 1-3 short sentences."},
        {"role": "user", "content": user_text},
    ]
    output = llm_pipe(
        messages,
        max_new_tokens=100,
        do_sample=False,
    )
    reply = output[0]["generated_text"][-1]["content"].strip()

    sampling_rate, audio = text_to_speech(reply)
    return reply, (sampling_rate, audio)


demo = gr.Interface(
    fn=process_audio,
    inputs=gr.Audio(label="Audio Input", sources=["microphone"], type="filepath"),
    outputs=[
        gr.Textbox(label="Assistant reply"),
        gr.Audio(label="Spoken reply", type="numpy"),
    ],
    title="A Simple AI Voice Assistant",
    description="An AI Assistant That Will Answer Your Questions!",
)

demo.launch(inbrowser=True, debug=True)