Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import gradio as gr
|
4 |
+
import edge_tts
|
5 |
+
import asyncio
|
6 |
+
import time
|
7 |
+
import tempfile
|
8 |
+
from huggingface_hub import InferenceClient
|
9 |
+
DESCRIPTION = """ # <center><b>ZARVIS⚡</b></center>
|
10 |
+
### <center>A personal voice assistant for YOU
|
11 |
+
### <center>I'm your ZEN Voice Assistant.</center>
|
12 |
+
"""
|
13 |
+
MORE = """ ## TRY Other Models
|
14 |
+
### https://zenai.biz
|
15 |
+
"""
|
16 |
+
Fast = """## Fastest Model"""
|
17 |
+
Complex = """## Best in Complex Question"""
|
18 |
+
Detail = """## Best for Detailed Generation or Long Answers"""
|
19 |
+
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
20 |
+
system_instructions1 = "[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'ZEN', 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]"
|
21 |
+
async def generate1(prompt):
|
22 |
+
generate_kwargs = dict(
|
23 |
+
temperature=0.6,
|
24 |
+
max_new_tokens=756,
|
25 |
+
top_p=0.95,
|
26 |
+
repetition_penalty=1,
|
27 |
+
do_sample=True,
|
28 |
+
seed=42,
|
29 |
+
)
|
30 |
+
formatted_prompt = system_instructions1 + prompt + "[JARVIS]"
|
31 |
+
stream = client1.text_generation(
|
32 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
33 |
+
output = ""
|
34 |
+
for response in stream:
|
35 |
+
output += response.token.text
|
36 |
+
communicate = edge_tts.Communicate(output)
|
37 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
38 |
+
tmp_path = tmp_file.name
|
39 |
+
await communicate.save(tmp_path)
|
40 |
+
yield tmp_path
|
41 |
+
client2 = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
|
42 |
+
system_instructions2 = "[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'ZEN', Must answer in friendly style and Easy Manner. You can answer Complex Questions. Do not say who are you or Hi, Hello, Just Start answering. Stop, as answer ends. [USER]"
|
43 |
+
async def generate2(prompt):
|
44 |
+
generate_kwargs = dict(
|
45 |
+
temperature=0.6,
|
46 |
+
max_new_tokens=512,
|
47 |
+
top_p=0.95,
|
48 |
+
repetition_penalty=1,
|
49 |
+
do_sample=True,
|
50 |
+
)
|
51 |
+
formatted_prompt = system_instructions2 + prompt + "[ASSISTANT]"
|
52 |
+
stream = client2.text_generation(
|
53 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
54 |
+
output = ""
|
55 |
+
for response in stream:
|
56 |
+
output += response.token.text
|
57 |
+
communicate = edge_tts.Communicate(output)
|
58 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
59 |
+
tmp_path = tmp_file.name
|
60 |
+
await communicate.save(tmp_path)
|
61 |
+
yield tmp_path
|
62 |
+
client3 = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
|
63 |
+
system_instructions3 = "[SYSTEM]The text provided is a request for a specific type of response from me, the virtual assistant. The request asks me to provide detailed and friendly responses as if I am the character Jarvis, inspired 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, my task is to generate responses that appear as if they are created by the user and not an AI-powered assistant.[USER]"
|
64 |
+
async def generate3(prompt):
|
65 |
+
generate_kwargs = dict(
|
66 |
+
temperature=0.6,
|
67 |
+
max_new_tokens=2048,
|
68 |
+
top_p=0.95,
|
69 |
+
repetition_penalty=1,
|
70 |
+
do_sample=True,
|
71 |
+
)
|
72 |
+
formatted_prompt = system_instructions3 + prompt + "[ASSISTANT]"
|
73 |
+
stream = client3.text_generation(
|
74 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
75 |
+
output = ""
|
76 |
+
for response in stream:
|
77 |
+
output += response.token.text
|
78 |
+
communicate = edge_tts.Communicate(output)
|
79 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
80 |
+
tmp_path = tmp_file.name
|
81 |
+
await communicate.save(tmp_path)
|
82 |
+
yield tmp_path
|
83 |
+
with gr.Blocks(css="style.css") as demo:
|
84 |
+
gr.Markdown(DESCRIPTION)
|
85 |
+
with gr.Row():
|
86 |
+
user_input = gr.Textbox(label="Prompt", value="What is Wikipedia")
|
87 |
+
input_text = gr.Textbox(label="Input Text", elem_id="important")
|
88 |
+
output_audio = gr.Audio(label="JARVIS", type="filepath",
|
89 |
+
interactive=False,
|
90 |
+
autoplay=True,
|
91 |
+
elem_classes="audio")
|
92 |
+
with gr.Row():
|
93 |
+
translate_btn = gr.Button("Response")
|
94 |
+
translate_btn.click(fn=generate1, inputs=user_input,
|
95 |
+
outputs=output_audio, api_name="translate")
|
96 |
+
|
97 |
+
gr.Markdown(MORE)
|
98 |
+
if __name__ == "__main__":
|
99 |
+
demo.queue(max_size=200).launch()
|