prithivMLmods commited on
Commit
fdb09b7
·
verified ·
1 Parent(s): 2e41807

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -72
app.py CHANGED
@@ -1,72 +1,122 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()