Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,148 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import edge_tts
|
6 |
+
import asyncio
|
7 |
+
import tempfile
|
8 |
+
import os
|
9 |
+
import inflect
|
10 |
from huggingface_hub import InferenceClient
|
11 |
+
import re
|
12 |
+
import time
|
13 |
+
from streaming_stt_nemo import Model
|
14 |
|
15 |
+
number_to_word = inflect.engine()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
default_lang = "en"
|
18 |
+
|
19 |
+
engines = { default_lang: Model(default_lang) }
|
20 |
+
|
21 |
+
def transcribe(audio):
|
22 |
+
lang = "fr"
|
23 |
+
model = engines[lang]
|
24 |
+
text = model.stt_file(audio)[0]
|
25 |
+
return text
|
26 |
+
|
27 |
+
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
28 |
+
|
29 |
+
system_instructions1 = "<s>[SYSTEM] Répondez comme le vrai Jarvis JARVIS, fabriqué par 'Tony Stark'. La conversation doit être très courte, claire, amicale et concise. Le texte fourni est une demande de réponse spécifique de votre part, en tant qu'assistant virtuel. Il vous demande de fournir des réponses amicales comme si vous étiez le personnage Jarvis, créé par « Tony Stark ». On s'attend à ce que j'évite les présentations et que je réponde directement à la requête, en développant tous les aspects de la demande. En tant qu'assistant doté d'une IA, votre tâche consiste à générer des réponses qui semblent avoir été créées par l'utilisateur et non par un assistant doté d'une IA.[USER]"
|
30 |
+
|
31 |
+
def model(text):
|
32 |
+
generate_kwargs = dict(
|
33 |
+
temperature=0.7,
|
34 |
+
max_new_tokens=512,
|
35 |
+
top_p=0.95,
|
36 |
+
repetition_penalty=1,
|
37 |
+
do_sample=True,
|
38 |
+
seed=42,
|
39 |
+
)
|
40 |
+
|
41 |
+
formatted_prompt = system_instructions1 + text + "[JARVIS]"
|
42 |
+
stream = client1.text_generation(
|
43 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
44 |
+
output = ""
|
45 |
+
for response in stream:
|
46 |
+
if not response.token.text == "</s>":
|
47 |
+
output += response.token.text
|
48 |
+
|
49 |
+
return output
|
50 |
+
|
51 |
+
def number_to_words(str):
|
52 |
+
words = str.split(' ')
|
53 |
+
result = []
|
54 |
+
|
55 |
+
for word in words:
|
56 |
+
if( any(char.isdigit() for char in word) ):
|
57 |
+
word = number_to_word.number_to_words(word)
|
58 |
+
|
59 |
+
result.append(word)
|
60 |
+
|
61 |
+
final_result = ' '.join(result).replace('point', '')
|
62 |
+
return final_result
|
63 |
+
|
64 |
+
async def respond(audio):
|
65 |
+
user = transcribe(audio)
|
66 |
+
reply = model(user)
|
67 |
+
reply2 = number_to_words(reply)
|
68 |
+
communicate = edge_tts.Communicate(reply2)
|
69 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
70 |
+
tmp_path = tmp_file.name
|
71 |
+
await communicate.save(tmp_path)
|
72 |
+
yield tmp_path
|
73 |
+
|
74 |
+
DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
|
75 |
+
### <center>A personal Assistant of Tony Stark for YOU
|
76 |
+
### <center>Voice Chat with your personal Assistant</center>
|
77 |
+
"""
|
78 |
+
|
79 |
+
MORE = """ ## TRY Other Models
|
80 |
+
### Instant Video: Create Amazing Videos in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Video
|
81 |
+
### Instant Image: 4k images in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Image
|
82 |
+
"""
|
83 |
+
|
84 |
+
BETA = """ ### Voice Chat (BETA)"""
|
85 |
+
|
86 |
+
FAST = """## Fastest Model"""
|
87 |
+
|
88 |
+
Complex = """## Best in Complex Question"""
|
89 |
+
|
90 |
+
Detail = """## Best for Detailed Generation or Long Answers"""
|
91 |
+
|
92 |
+
base_loaded = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
93 |
+
|
94 |
+
client1 = InferenceClient(base_loaded)
|
95 |
+
|
96 |
+
system_instructions1 = "[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]"
|
97 |
+
|
98 |
+
async def generate1(prompt):
|
99 |
+
generate_kwargs = dict(
|
100 |
+
temperature=0.7,
|
101 |
+
max_new_tokens=512,
|
102 |
+
top_p=0.95,
|
103 |
+
repetition_penalty=1,
|
104 |
+
do_sample=False,
|
105 |
+
)
|
106 |
+
formatted_prompt = system_instructions1 + prompt + "[JARVIS]"
|
107 |
+
stream = client1.text_generation(
|
108 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
109 |
+
output = ""
|
110 |
+
for response in stream:
|
111 |
+
if not response.token.text == "</s>":
|
112 |
+
output += response.token.text
|
113 |
+
|
114 |
+
communicate = edge_tts.Communicate(output)
|
115 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
116 |
+
tmp_path = tmp_file.name
|
117 |
+
await communicate.save(tmp_path)
|
118 |
+
yield tmp_path
|
119 |
+
|
120 |
+
with gr.Blocks(css="style.css") as demo:
|
121 |
+
gr.Markdown(DESCRIPTION)
|
122 |
+
with gr.Row():
|
123 |
+
user_input = gr.Audio(label="Voice Chat (BETA)", type="filepath")
|
124 |
+
output_audio = gr.Audio(label="JARVIS", type="filepath",
|
125 |
+
interactive=False,
|
126 |
+
autoplay=True,
|
127 |
+
elem_classes="audio")
|
128 |
+
with gr.Row():
|
129 |
+
translate_btn = gr.Button("Response")
|
130 |
+
translate_btn.click(fn=respond, inputs=user_input,
|
131 |
+
outputs=output_audio, api_name=False)
|
132 |
+
gr.Markdown(FAST)
|
133 |
+
with gr.Row():
|
134 |
+
user_input = gr.Textbox(label="Prompt", value="What is Wikipedia")
|
135 |
+
input_text = gr.Textbox(label="Input Text", elem_id="important")
|
136 |
+
output_audio = gr.Audio(label="JARVIS", type="filepath",
|
137 |
+
interactive=False,
|
138 |
+
autoplay=True,
|
139 |
+
elem_classes="audio")
|
140 |
+
with gr.Row():
|
141 |
+
translate_btn = gr.Button("Response")
|
142 |
+
translate_btn.click(fn=generate1, inputs=user_input,
|
143 |
+
outputs=output_audio, api_name="translate")
|
144 |
+
|
145 |
+
gr.Markdown(MORE)
|
146 |
|
147 |
if __name__ == "__main__":
|
148 |
+
demo.queue(max_size=200).launch()
|