Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import edge_tts
|
| 3 |
+
import asyncio
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
import re
|
| 8 |
+
from streaming_stt_nemo import Model
|
| 9 |
+
import torch
|
| 10 |
+
import random
|
| 11 |
+
from openai import OpenAI
|
| 12 |
+
|
| 13 |
+
default_lang = "en"
|
| 14 |
+
|
| 15 |
+
engines = { default_lang: Model(default_lang) }
|
| 16 |
+
|
| 17 |
+
def transcribe(audio):
|
| 18 |
+
lang = "en"
|
| 19 |
+
model = engines[lang]
|
| 20 |
+
text = model.stt_file(audio)[0]
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 24 |
+
|
| 25 |
+
def client_fn(model):
|
| 26 |
+
if "Mixtral" in model:
|
| 27 |
+
return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 28 |
+
elif "Llama" in model:
|
| 29 |
+
return InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
| 30 |
+
elif "Mistral" in model:
|
| 31 |
+
return InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
| 32 |
+
elif "Phi" in model:
|
| 33 |
+
return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
|
| 34 |
+
elif "Llama 3B" in model:
|
| 35 |
+
return OpenAI(
|
| 36 |
+
base_url="http://52.76.81.56:60002/v1",
|
| 37 |
+
api_key="token-abc123"
|
| 38 |
+
)
|
| 39 |
+
else:
|
| 40 |
+
return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
|
| 41 |
+
|
| 42 |
+
def randomize_seed_fn(seed: int) -> int:
|
| 43 |
+
seed = random.randint(0, 999999)
|
| 44 |
+
return seed
|
| 45 |
+
|
| 46 |
+
system_instructions1 = """
|
| 47 |
+
[SYSTEM] Answer as Real Optimus OPTIMUS, Made by 'Jaward.'
|
| 48 |
+
Keep conversation friendly, short, clear, and concise.
|
| 49 |
+
Avoid unnecessary introductions and answer the user's questions directly.
|
| 50 |
+
Respond in a normal, conversational manner while being friendly and helpful.
|
| 51 |
+
[USER]
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
def models(text, model="Mixtral 8x7B", seed=42):
|
| 55 |
+
seed = int(randomize_seed_fn(seed))
|
| 56 |
+
generator = torch.Generator().manual_seed(seed)
|
| 57 |
+
|
| 58 |
+
client = client_fn(model)
|
| 59 |
+
|
| 60 |
+
if "Llama 3B" in model:
|
| 61 |
+
messages = [
|
| 62 |
+
{"role": "system", "content": system_instructions1},
|
| 63 |
+
{"role": "user", "content": text}
|
| 64 |
+
]
|
| 65 |
+
completion = client.chat.completions.create(
|
| 66 |
+
model="/data/shared/huggingface/hub/models--meta-llama--Meta-Llama-3-8B-Instruct/snapshots/c4a54320a52ed5f88b7a2f84496903ea4ff07b45/",
|
| 67 |
+
messages=messages
|
| 68 |
+
)
|
| 69 |
+
return completion.choices[0].message.content
|
| 70 |
+
else:
|
| 71 |
+
generate_kwargs = dict(
|
| 72 |
+
max_new_tokens=300,
|
| 73 |
+
seed=seed
|
| 74 |
+
)
|
| 75 |
+
formatted_prompt = system_instructions1 + text + "[OPTIMUS]"
|
| 76 |
+
stream = client.text_generation(
|
| 77 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 78 |
+
output = ""
|
| 79 |
+
for response in stream:
|
| 80 |
+
if not response.token.text == "</s>":
|
| 81 |
+
output += response.token.text
|
| 82 |
+
return output
|
| 83 |
+
|
| 84 |
+
async def respond(audio, model, seed):
|
| 85 |
+
user = transcribe(audio)
|
| 86 |
+
reply = models(user, model, seed)
|
| 87 |
+
communicate = edge_tts.Communicate(reply)
|
| 88 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 89 |
+
tmp_path = tmp_file.name
|
| 90 |
+
await communicate.save(tmp_path)
|
| 91 |
+
yield tmp_path
|
| 92 |
+
|
| 93 |
+
DESCRIPTION = """ # <center><b>OPTIMUS⚡</b></center>
|
| 94 |
+
### <center>A personal Assistant of Jaward for YOU
|
| 95 |
+
### <center>Voice Chat with your personal Assistant</center>
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
with gr.Blocks(css="style.css") as demo:
|
| 99 |
+
gr.Markdown(DESCRIPTION)
|
| 100 |
+
with gr.Row():
|
| 101 |
+
select = gr.Dropdown([
|
| 102 |
+
'Mixtral 8x7B',
|
| 103 |
+
'Llama 3 8B',
|
| 104 |
+
'Mistral 7B v0.3',
|
| 105 |
+
'Phi 3 mini',
|
| 106 |
+
'Llama 3B'
|
| 107 |
+
],
|
| 108 |
+
value="Mistral 7B v0.3",
|
| 109 |
+
label="Model"
|
| 110 |
+
)
|
| 111 |
+
seed = gr.Slider(
|
| 112 |
+
label="Seed",
|
| 113 |
+
minimum=0,
|
| 114 |
+
maximum=999999,
|
| 115 |
+
step=1,
|
| 116 |
+
value=0,
|
| 117 |
+
visible=False
|
| 118 |
+
)
|
| 119 |
+
input = gr.Audio(label="User", sources="microphone", type="filepath", waveform_options=False)
|
| 120 |
+
output = gr.Audio(label="AI", type="filepath",
|
| 121 |
+
interactive=False,
|
| 122 |
+
autoplay=True,
|
| 123 |
+
elem_classes="audio")
|
| 124 |
+
gr.Interface(
|
| 125 |
+
batch=True,
|
| 126 |
+
max_batch_size=10,
|
| 127 |
+
fn=respond,
|
| 128 |
+
inputs=[input, select, seed],
|
| 129 |
+
outputs=[output], live=True)
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
demo.queue(max_size=200).launch()
|