Spaces:
Running
Running
Update voice_chat.py
Browse files- voice_chat.py +7 -28
voice_chat.py
CHANGED
@@ -9,7 +9,6 @@ import torch
|
|
9 |
import sentencepiece as spm
|
10 |
import onnxruntime as ort
|
11 |
from huggingface_hub import hf_hub_download, InferenceClient
|
12 |
-
import os
|
13 |
|
14 |
# Speech Recognition Model Configuration
|
15 |
model_name = "neongeckocom/stt_en_citrinet_512_gamma_0_25"
|
@@ -20,28 +19,9 @@ preprocessor = torch.jit.load(hf_hub_download(model_name, "preprocessor.ts", sub
|
|
20 |
encoder = ort.InferenceSession(hf_hub_download(model_name, "model.onnx", subfolder="onnx"))
|
21 |
tokenizer = spm.SentencePieceProcessor(hf_hub_download(model_name, "tokenizer.spm", subfolder="onnx"))
|
22 |
|
23 |
-
# Model Configuration
|
24 |
-
|
25 |
-
|
26 |
-
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
27 |
-
|
28 |
-
|
29 |
-
# Define a function to get the appropriate InferenceClient based on model name
|
30 |
-
def client_fn(model):
|
31 |
-
if "Nous" in model:
|
32 |
-
return InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO")
|
33 |
-
elif "Star" in model:
|
34 |
-
return InferenceClient("HuggingFaceH4/starchat2-15b-v0.1")
|
35 |
-
elif "Mistral" in model:
|
36 |
-
return InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
37 |
-
elif "Phi" in model:
|
38 |
-
return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
|
39 |
-
elif "Zephyr" in model:
|
40 |
-
return InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
41 |
-
else:
|
42 |
-
return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
43 |
-
|
44 |
-
system_instructions1 = "[SYSTEM] Answer as Real OpenGPT 4o, Made by 'KingNish', 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. You 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]"
|
45 |
|
46 |
def resample(audio_fp32, sr):
|
47 |
return soxr.resample(audio_fp32, sr, sample_rate)
|
@@ -69,15 +49,14 @@ def transcribe(audio_path):
|
|
69 |
|
70 |
return text
|
71 |
|
72 |
-
def model(text
|
73 |
-
|
74 |
-
formatted_prompt = system_instructions1 + text + "[OpenGPT 4o]"
|
75 |
stream = client1.text_generation(formatted_prompt, max_new_tokens=512, stream=True, details=True, return_full_text=False)
|
76 |
return "".join([response.token.text for response in stream if response.token.text != "</s>"])
|
77 |
|
78 |
-
async def respond(audio
|
79 |
user = transcribe(audio)
|
80 |
-
reply = model(user
|
81 |
communicate = edge_tts.Communicate(reply)
|
82 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
83 |
tmp_path = tmp_file.name
|
|
|
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"
|
|
|
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)
|
|
|
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
|