Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
gr.
|
| 46 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Load models
|
| 7 |
+
whisper = AutoModelForCausalLM.from_pretrained("openai/whisper-tiny")
|
| 8 |
+
whisper_tokenizer = AutoTokenizer.from_pretrained("openai/whisper-tiny")
|
| 9 |
+
|
| 10 |
+
llm_model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
"microsoft/Phi-3-mini-4k-instruct",
|
| 12 |
+
device_map="cpu",
|
| 13 |
+
torch_dtype="auto",
|
| 14 |
+
trust_remote_code=True)
|
| 15 |
+
llm_tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
|
| 16 |
+
phi3_mini_4k = pipeline("text-generation", model=llm_model, tokenizer=llm_tokenizer)
|
| 17 |
+
|
| 18 |
+
vits_tts = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs")
|
| 19 |
+
|
| 20 |
+
# Define function to convert audio to text
|
| 21 |
+
def audio_to_text(audio):
|
| 22 |
+
# Convert audio to text using Whisper model
|
| 23 |
+
inputs = whisper_tokenizer(audio, return_tensors="pt", padding=True, truncation=True)
|
| 24 |
+
outputs = whisper(**inputs)
|
| 25 |
+
predicted_ids = torch.argmax(outputs.logits, dim=-1)
|
| 26 |
+
transcribed_text = whisper_tokenizer.decode(predicted_ids[0])
|
| 27 |
+
return str(transcribed_text)
|
| 28 |
+
|
| 29 |
+
# Define functions for model predictions
|
| 30 |
+
def generate_text(prompt):
|
| 31 |
+
generated_text = phi3_mini_4k(prompt)[0]['generated_text']
|
| 32 |
+
return generated_text
|
| 33 |
+
|
| 34 |
+
def text_to_speech(text):
|
| 35 |
+
generated_text = phi3_mini_4k(text, max_length=50, do_sample=True)[0]['generated_text']
|
| 36 |
+
speech = vits_tts(generated_text)[0]['audio']
|
| 37 |
+
return speech
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
audio_input = gr.Audio(label="Can I Help You?", sources="microphone")
|
| 41 |
+
text_output = gr.Textbox(label="Transcribed Text")
|
| 42 |
+
gr.Interface(
|
| 43 |
+
text_to_speech,
|
| 44 |
+
inputs=audio_input,
|
| 45 |
+
outputs=gr.Audio(label="Answer:"),
|
| 46 |
+
title="MyAlexa AI Assistant").launch(inbrowser=True, debug=True)
|