Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,122 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import edge_tts
|
5 |
-
import asyncio
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
#important
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
.gradio-container {
|
10 |
+
max-width: 1000px !important;
|
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 |
+
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
55 |
+
|
56 |
+
client = OpenAI(
|
57 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
58 |
+
api_key=ACCESS_TOKEN,
|
59 |
+
)
|
60 |
+
|
61 |
+
async def respond(
|
62 |
+
message,
|
63 |
+
history: list[tuple[str, str]],
|
64 |
+
system_message,
|
65 |
+
max_tokens,
|
66 |
+
temperature,
|
67 |
+
top_p,
|
68 |
+
):
|
69 |
+
messages = [{"role": "system", "content": system_message}]
|
70 |
+
|
71 |
+
for val in history:
|
72 |
+
if val[0]:
|
73 |
+
messages.append({"role": "user", "content": val[0]})
|
74 |
+
if val[1]:
|
75 |
+
messages.append({"role": "assistant", "content": val[1]})
|
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 |
+
demo = gr.ChatInterface(
|
102 |
+
respond,
|
103 |
+
additional_inputs=[
|
104 |
+
gr.Textbox(value="", label="System message", lines=2),
|
105 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
106 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
107 |
+
gr.Slider(
|
108 |
+
minimum=0.1,
|
109 |
+
maximum=1.0,
|
110 |
+
value=0.95,
|
111 |
+
step=0.05,
|
112 |
+
label="Top-P",
|
113 |
+
),
|
114 |
+
],
|
115 |
+
css=css,
|
116 |
+
title="Old TV Terminal Chat",
|
117 |
+
description="Welcome to the Old TV Terminal. Type your message below.",
|
118 |
+
additional_outputs=[gr.Audio(label="Generated Speech", autoplay=True)]
|
119 |
+
)
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
demo.launch()
|