Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,51 @@
|
|
| 1 |
-
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
return speech
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
description="An AI Assistant That Will Answer Your Questions!")
|
| 24 |
-
my_alexa_output.launch(inbrowser=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import WhisperForConditionalGeneration, WhisperProcessor, AutoTokenizer, AutoModelForCausalLM, VitsModel
|
| 4 |
+
|
| 5 |
+
# Load the models and tokenizers
|
| 6 |
+
asr_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
|
| 7 |
+
asr_tokenizer = AutoTokenizer.from_pretrained("openai/whisper-tiny")
|
| 8 |
+
asr_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
| 9 |
+
|
| 10 |
+
llm_model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
|
| 11 |
+
llm_tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
|
| 12 |
+
|
| 13 |
+
tts_model = VitsModel.from_pretrained("kakao-enterprise/vits-ljs")
|
| 14 |
+
tts_tokenizer = AutoTokenizer.from_pretrained("kakao-enterprise/vits-ljs")
|
| 15 |
|
| 16 |
+
# Define the function to transcribe audio
|
| 17 |
+
def transcribe_audio(audio_input):
|
| 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 |
+
# Define the function to generate text
|
| 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 |
+
def text_to_speech(generated_text):
|
| 36 |
+
text_input = tts_tokenizer(generated_text, return_tensors="pt")
|
| 37 |
+
|
| 38 |
+
with torch.no_grad():
|
| 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)
|
|
|
|
|
|