Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import edge_tts
|
3 |
+
import asyncio
|
4 |
+
import tempfile
|
5 |
+
import numpy as np
|
6 |
+
import soxr
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import torch
|
9 |
+
import sentencepiece as spm
|
10 |
+
import onnxruntime as ort
|
11 |
+
from huggingface_hub import hf_hub_download, InferenceClient
|
12 |
+
|
13 |
+
# Speech Recognition Model Configuration
|
14 |
+
model_name = "neongeckocom/stt_en_citrinet_512_gamma_0_25"
|
15 |
+
sample_rate = 16000
|
16 |
+
|
17 |
+
# Download preprocessor, encoder and tokenizer
|
18 |
+
preprocessor = torch.jit.load(hf_hub_download(model_name, "preprocessor.ts", subfolder="onnx"))
|
19 |
+
encoder = ort.InferenceSession(hf_hub_download(model_name, "model.onnx", subfolder="onnx"))
|
20 |
+
tokenizer = spm.SentencePieceProcessor(hf_hub_download(model_name, "tokenizer.spm", subfolder="onnx"))
|
21 |
+
|
22 |
+
# Mistral Model Configuration
|
23 |
+
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
24 |
+
system_instructions1 = "<s>[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Jarvis, made by 'Tony Stark.' The expectation is that I will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by the user and not an AI-powered assistant. [USER]"
|
25 |
+
|
26 |
+
def resample(audio_fp32, sr):
|
27 |
+
return soxr.resample(audio_fp32, sr, sample_rate)
|
28 |
+
|
29 |
+
def to_float32(audio_buffer):
|
30 |
+
return np.divide(audio_buffer, np.iinfo(audio_buffer.dtype).max, dtype=np.float32)
|
31 |
+
|
32 |
+
def transcribe(audio_path):
|
33 |
+
audio_file = AudioSegment.from_file(audio_path)
|
34 |
+
sr = audio_file.frame_rate
|
35 |
+
audio_buffer = np.array(audio_file.get_array_of_samples())
|
36 |
+
|
37 |
+
audio_fp32 = to_float32(audio_buffer)
|
38 |
+
audio_16k = resample(audio_fp32, sr)
|
39 |
+
|
40 |
+
input_signal = torch.tensor(audio_16k).unsqueeze(0)
|
41 |
+
length = torch.tensor(len(audio_16k)).unsqueeze(0)
|
42 |
+
processed_signal, _ = preprocessor.forward(input_signal=input_signal, length=length)
|
43 |
+
|
44 |
+
logits = encoder.run(None, {'audio_signal': processed_signal.numpy(), 'length': length.numpy()})[0][0]
|
45 |
+
|
46 |
+
blank_id = tokenizer.vocab_size()
|
47 |
+
decoded_prediction = [p for p in logits.argmax(axis=1).tolist() if p != blank_id]
|
48 |
+
text = tokenizer.decode_ids(decoded_prediction)
|
49 |
+
|
50 |
+
return text
|
51 |
+
|
52 |
+
def model(text):
|
53 |
+
formatted_prompt = system_instructions1 + text + "[JARVIS]"
|
54 |
+
stream = client1.text_generation(formatted_prompt, max_new_tokens=512, stream=True, details=True, return_full_text=False)
|
55 |
+
return "".join([response.token.text for response in stream if response.token.text != "</s>"])
|
56 |
+
|
57 |
+
async def respond(audio):
|
58 |
+
user = transcribe(audio)
|
59 |
+
reply = model(user)
|
60 |
+
communicate = edge_tts.Communicate(reply)
|
61 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
62 |
+
tmp_path = tmp_file.name
|
63 |
+
await communicate.save(tmp_path)
|
64 |
+
return tmp_path
|
65 |
+
|
66 |
+
with gr.Blocks() as demo:
|
67 |
+
with gr.Row():
|
68 |
+
input = gr.Audio(label="Voice Chat (BETA)", sources="microphone", type="filepath", waveform_options=False)
|
69 |
+
output = gr.Audio(label="JARVIS", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
|
70 |
+
gr.Interface(fn=respond, inputs=[input], outputs=[output], live=True)
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
demo.queue(max_size=200).launch()
|