Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,122 +1,75 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from openai import OpenAI
|
3 |
import os
|
|
|
|
|
4 |
import edge_tts
|
5 |
import asyncio
|
|
|
6 |
import tempfile
|
|
|
7 |
|
8 |
-
css
|
9 |
-
|
10 |
-
|
11 |
-
background-color: #000 !important;
|
12 |
-
color: #0f0 !important;
|
13 |
-
font-family: monospace !important;
|
14 |
-
padding: 20px !important;
|
15 |
-
border-radius: 5px !important;
|
16 |
-
border: 10px solid #333 !important;
|
17 |
-
box-shadow: 0 0 20px #0f0 !important;
|
18 |
-
}
|
19 |
-
|
20 |
-
h1 {
|
21 |
-
text-align: center;
|
22 |
-
color: #0f0 !important;
|
23 |
-
text-shadow: 0 0 5px #0f0 !important;
|
24 |
-
}
|
25 |
-
|
26 |
-
footer {
|
27 |
-
visibility: hidden;
|
28 |
-
}
|
29 |
-
|
30 |
-
textarea, input, .output {
|
31 |
-
background-color: #000 !important;
|
32 |
-
color: #0f0 !important;
|
33 |
-
border: 1px solid #0f0 !important;
|
34 |
-
font-family: monospace !important;
|
35 |
-
}
|
36 |
-
|
37 |
-
button {
|
38 |
-
background-color: #0f0 !important;
|
39 |
-
color: #000 !important;
|
40 |
-
border: none !important;
|
41 |
-
font-family: monospace !important;
|
42 |
-
}
|
43 |
-
|
44 |
-
button:hover {
|
45 |
-
background-color: #090 !important;
|
46 |
-
}
|
47 |
-
|
48 |
-
.audio {
|
49 |
-
width: 100%;
|
50 |
-
margin-top: 20px;
|
51 |
}
|
52 |
'''
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
client =
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
)
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
messages.append({"role": "user", "content": message})
|
78 |
-
|
79 |
-
response = ""
|
80 |
-
|
81 |
-
for message in client.chat.completions.create(
|
82 |
-
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
83 |
-
max_tokens=max_tokens,
|
84 |
-
stream=True,
|
85 |
-
temperature=temperature,
|
86 |
-
top_p=top_p,
|
87 |
-
messages=messages,
|
88 |
-
):
|
89 |
-
token = message.choices[0].delta.content
|
90 |
-
|
91 |
-
response += token
|
92 |
-
yield response
|
93 |
-
|
94 |
-
# Convert the response to speech using Edge TTS
|
95 |
-
communicate = edge_tts.Communicate(response)
|
96 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
97 |
tmp_path = tmp_file.name
|
98 |
await communicate.save(tmp_path)
|
99 |
yield tmp_path
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
gr.Textbox(
|
105 |
-
gr.
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
10 |
+
css= '''
|
11 |
+
#important{
|
12 |
+
display: none;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
}
|
14 |
'''
|
15 |
+
DESCRIPTION = """## EDGE TTS
|
16 |
+
"""
|
17 |
+
|
18 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
19 |
+
|
20 |
+
system_instructions = "[INST] Answers by 🔉, Keep conversation very short, clear, friendly and concise."
|
21 |
+
|
22 |
+
async def generate(prompt):
|
23 |
+
generate_kwargs = dict(
|
24 |
+
temperature=0.6,
|
25 |
+
max_new_tokens=256,
|
26 |
+
top_p=0.95,
|
27 |
+
repetition_penalty=1,
|
28 |
+
do_sample=True,
|
29 |
+
seed=42,
|
30 |
+
)
|
31 |
+
formatted_prompt = system_instructions + prompt + "[/INST]"
|
32 |
+
stream = client.text_generation(
|
33 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
34 |
+
output = ""
|
35 |
+
for response in stream:
|
36 |
+
output += response.token.text
|
37 |
+
|
38 |
+
communicate = edge_tts.Communicate(output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
40 |
tmp_path = tmp_file.name
|
41 |
await communicate.save(tmp_path)
|
42 |
yield tmp_path
|
43 |
|
44 |
+
with gr.Blocks(css=css) as demo:
|
45 |
+
gr.Markdown(DESCRIPTION)
|
46 |
+
with gr.Row():
|
47 |
+
user_input = gr.Textbox(label="Prompt")
|
48 |
+
input_text = gr.Textbox(label="Input Text", elem_id="important")
|
49 |
+
output_audio = gr.Audio(label="Audio", type="filepath",
|
50 |
+
interactive=False,
|
51 |
+
autoplay=True,
|
52 |
+
elem_classes="audio")
|
53 |
+
with gr.Row():
|
54 |
+
translate_btn = gr.Button("Response")
|
55 |
+
translate_btn.click(fn=generate, inputs=user_input,
|
56 |
+
outputs=output_audio, api_name="translate")
|
57 |
+
|
58 |
+
# Add examples
|
59 |
+
gr.Examples(
|
60 |
+
examples=[
|
61 |
+
["What is AI?"],
|
62 |
+
["Add 2*3345"],
|
63 |
+
["Describe Mt. Everest"]
|
64 |
+
],
|
65 |
+
inputs=user_input,
|
66 |
+
outputs=output_audio,
|
67 |
+
fn=generate,
|
68 |
+
cache_examples=True
|
69 |
+
)
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
+
demo.queue(max_size=20).launch()
|
73 |
+
|
74 |
+
|
75 |
+
Use the edge tts along with above and use this to have the generated text to convert to tts
|