Angelo25 commited on
Commit
e4904ed
·
verified ·
1 Parent(s): 7020ca1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -14
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("text-generation", model="microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
6
- tts_pipe = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs")
 
 
 
 
 
7
 
8
- def process_audio(audio):
9
- # Convert audio to text using ASR pipeline
10
- transcribed_text = asr_pipe(audio)[0]['transcription']
11
 
12
- # Generate text using LLM pipeline
13
- generated_text = llm_pipe(transcribed_text)[0]['generated_text']
14
 
15
- # Convert generated text to speech using TTS pipeline
16
- speech = tts_pipe(generated_text)[0]['audio']
 
 
 
 
 
 
 
 
 
17
 
18
- return speech
 
 
 
19
 
20
- audio_input = gr.Audio(label="Audio Input", sources="microphone")
21
- my_alexa_output = gr.Interface(fn=process_audio, inputs=audio_input, outputs="text", title="MyAlexa AI Assistant",
22
- description="An AI Assistant That Will Answer Your Questions!")
23
- my_alexa_output.launch(inbrowser=True, debug=True)
 
 
 
 
 
 
 
 
 
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)