Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import WhisperForConditionalGeneration, WhisperProcessor, AutoTokenizer, AutoModelForCausalLM, VitsModel
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
asr_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
| 19 |
-
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
|
| 20 |
-
model.config.forced_decoder_ids = None
|
| 21 |
-
|
| 22 |
-
input_features = processor(audio_input, return_tensors="pt").input_features
|
| 23 |
-
predicted_ids = model.generate(input_features)
|
| 24 |
-
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
| 25 |
-
|
| 26 |
-
return transcription
|
| 27 |
|
| 28 |
-
|
| 29 |
-
def generate_text(transcription):
|
| 30 |
-
input_ids = llm_tokenizer.encode(transcription, return_tensors="pt")
|
| 31 |
-
output_ids = llm_model.generate(input_ids)
|
| 32 |
-
generated_text = llm_tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 33 |
-
return generated_text
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
output = tts_model(**text_input).waveform
|
| 40 |
-
|
| 41 |
-
return output
|
| 42 |
-
|
| 43 |
-
def my_alexa(audio_input):
|
| 44 |
-
transcription = transcribe_audio(audio_input)
|
| 45 |
-
generated_text = generate_text(transcription)
|
| 46 |
-
speech_output = text_to_speech(generated_text)
|
| 47 |
-
return speech_output
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
gr.Interface(fn=transcribe_audio, inputs=gr.Audio(sources="microphone"), outputs="text", title="MyAlexa AI Assistant",
|
| 51 |
-
description="An AI Assistant That Can Answer Your Questions!").launch(inbrowser=True, debug=True)
|
|
|
|
| 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("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="audio", title="MyAlexa AI Assistant",
|
| 22 |
+
description="An AI Assistant That Will Answer Your Questions!")
|
| 23 |
+
my_alexa_output.launch(inbrowser=True, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|