Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration, AutoModelForCausalLM, AutoTokenizer, VitsModel, pipeline
|
| 4 |
+
|
| 5 |
+
def ASR(audio_input):
|
| 6 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
| 7 |
+
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
|
| 8 |
+
model.config.forced_decoder_ids = None
|
| 9 |
+
|
| 10 |
+
input_features = processor(audio_input, return_tensors="pt").input_features
|
| 11 |
+
predicted_ids = model.generate(input_features)
|
| 12 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
| 13 |
+
|
| 14 |
+
return transcription
|
| 15 |
+
|
| 16 |
+
def LLM(transcription):
|
| 17 |
+
torch.random.manual_seed(0)
|
| 18 |
+
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
"microsoft/Phi-3-mini-4k-instruct",
|
| 21 |
+
device_map="cuda",
|
| 22 |
+
torch_dtype="auto",
|
| 23 |
+
trust_remote_code=True)
|
| 24 |
+
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
|
| 26 |
+
|
| 27 |
+
llm_pipe = pipeline("text-generation", model=model,tokenizer=tokenizer,)
|
| 28 |
+
|
| 29 |
+
generation_args = {
|
| 30 |
+
"max_new_tokens": 500,
|
| 31 |
+
"return_full_text": False,
|
| 32 |
+
"temperature": 0.0,
|
| 33 |
+
"do_sample": False,
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
output = llm_pipe(transcription, **generation_args)
|
| 37 |
+
print(output[0]['generated_text'])
|
| 38 |
+
|
| 39 |
+
def TTS(llm_text):
|
| 40 |
+
model = VitsModel.from_pretrained("kakao-enterprise/vits-ljs")
|
| 41 |
+
tokenizer = AutoTokenizer.from_pretrained("kakao-enterprise/vits-ljs")
|
| 42 |
+
|
| 43 |
+
inputs = tokenizer(llm_text, return_tensors="pt")
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
output = model(**inputs).waveform
|
| 47 |
+
|
| 48 |
+
return output
|
| 49 |
+
|
| 50 |
+
def my_alexa(audio_input):
|
| 51 |
+
asr_output_text = ASR(audio_input)
|
| 52 |
+
llm_output = LLM(asr_output_text)
|
| 53 |
+
tts_output = TTS(llm_output)
|
| 54 |
+
|
| 55 |
+
return gr.Audio(tts_output)
|
| 56 |
+
|
| 57 |
+
alexa_interface = gr.Interface(fn=my_alexa, inputs=gr.Audio(sources="microphone"), outputs="audio", title="MyAlexa AI Model",
|
| 58 |
+
description="An AI assistant that can help you with your daily tasks.")
|
| 59 |
+
|
| 60 |
+
alexa_interface.launch(inbrowser=True)
|