latterworks commited on
Commit
ba4903d
·
verified ·
1 Parent(s): 075cb83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +212 -27
app.py CHANGED
@@ -1,11 +1,35 @@
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,
@@ -15,18 +39,16 @@ def respond(
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,
@@ -35,30 +57,193 @@ def respond(
35
  top_p=top_p,
36
  ):
37
  token = message.choices[0].delta.content
 
 
 
38
 
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from transformers import pipeline
4
+ import numpy as np
5
 
6
  """
7
  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
8
  """
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
+ # Initialize Bark TTS model
12
+ try:
13
+ synthesizer = pipeline("text-to-speech", "suno/bark")
14
+ tts_available = True
15
+ except Exception as e:
16
+ print(f"TTS model failed to load: {e}")
17
+ tts_available = False
18
+ synthesizer = None
19
+
20
+ def generate_speech(text):
21
+ """Generate speech from text using Bark TTS"""
22
+ if not tts_available or not synthesizer:
23
+ return None, "TTS not available"
24
+
25
+ try:
26
+ speech = synthesizer(text, forward_params={"do_sample": True})
27
+ # Convert to format Gradio expects
28
+ audio_data = speech["audio"].flatten()
29
+ sample_rate = speech["sampling_rate"]
30
+ return sample_rate, audio_data
31
+ except Exception as e:
32
+ return None, f"TTS Error: {str(e)}"
33
 
34
  def respond(
35
  message,
 
39
  temperature,
40
  top_p,
41
  ):
42
+ """Generate chat response"""
43
  messages = [{"role": "system", "content": system_message}]
 
44
  for val in history:
45
  if val[0]:
46
  messages.append({"role": "user", "content": val[0]})
47
  if val[1]:
48
  messages.append({"role": "assistant", "content": val[1]})
 
49
  messages.append({"role": "user", "content": message})
50
+
51
  response = ""
 
52
  for message in client.chat_completion(
53
  messages,
54
  max_tokens=max_tokens,
 
57
  top_p=top_p,
58
  ):
59
  token = message.choices[0].delta.content
60
+ if token:
61
+ response += token
62
+ yield response
63
 
64
+ def respond_with_audio(
65
+ message,
66
+ history: list[tuple[str, str]],
67
+ system_message,
68
+ max_tokens,
69
+ temperature,
70
+ top_p,
71
+ enable_tts
72
+ ):
73
+ """Generate chat response and optionally convert to speech"""
74
+ # Get text response
75
+ final_response = ""
76
+ for response in respond(message, history, system_message, max_tokens, temperature, top_p):
77
+ final_response = response
78
+ yield response, None # Yield text first, audio comes later
79
+
80
+ # Generate audio if TTS is enabled
81
+ if enable_tts and tts_available and final_response.strip():
82
+ try:
83
+ # Clean response for TTS (remove markdown, keep essential punctuation)
84
+ clean_text = final_response.replace("*", "").replace("#", "").replace("`", "")
85
+ # Limit length for TTS (Bark works best with shorter texts)
86
+ if len(clean_text) > 500:
87
+ clean_text = clean_text[:500] + "..."
88
+
89
+ sample_rate, audio_data = generate_speech(clean_text)
90
+ if sample_rate:
91
+ yield final_response, (sample_rate, audio_data)
92
+ else:
93
+ yield final_response, None
94
+ except Exception as e:
95
+ print(f"TTS generation failed: {e}")
96
+ yield final_response, None
97
+ else:
98
+ yield final_response, None
99
 
100
+ # Create the main chat interface with TTS option
101
+ with gr.Blocks(title="Chat + TTS Bot") as demo:
102
+ gr.Markdown("# 🤖 Chat Bot with Text-to-Speech")
103
+ gr.Markdown("Chat with Zephyr-7B and optionally hear responses with Bark TTS")
104
+
105
+ with gr.Row():
106
+ with gr.Column(scale=2):
107
+ chatbot = gr.Chatbot(height=400)
108
+ msg = gr.Textbox(
109
+ placeholder="Type your message here...",
110
+ label="Message",
111
+ lines=2
112
+ )
113
+
114
+ with gr.Row():
115
+ submit = gr.Button("💬 Send", variant="primary")
116
+ clear = gr.Button("🗑️ Clear")
117
+
118
+ with gr.Column(scale=1):
119
+ # TTS Controls
120
+ gr.Markdown("### 🔊 Text-to-Speech")
121
+ enable_tts = gr.Checkbox(
122
+ label="Enable TTS for responses",
123
+ value=False,
124
+ info="Generate audio for bot responses"
125
+ )
126
+
127
+ audio_output = gr.Audio(
128
+ label="Response Audio",
129
+ autoplay=False,
130
+ visible=True
131
+ )
132
+
133
+ # Manual TTS
134
+ gr.Markdown("### 🎤 Manual TTS")
135
+ tts_input = gr.Textbox(
136
+ placeholder="Enter text to convert to speech...",
137
+ label="Text for TTS",
138
+ lines=2
139
+ )
140
+ tts_button = gr.Button("🗣️ Generate Speech")
141
+
142
+ # Chat Settings (Collapsible)
143
+ with gr.Accordion("⚙️ Chat Settings", open=False):
144
+ system_message = gr.Textbox(
145
+ value="You are a friendly and helpful AI assistant.",
146
+ label="System Message",
147
+ lines=2
148
+ )
149
+ with gr.Row():
150
+ max_tokens = gr.Slider(
151
+ minimum=1,
152
+ maximum=2048,
153
+ value=512,
154
+ step=1,
155
+ label="Max tokens"
156
+ )
157
+ temperature = gr.Slider(
158
+ minimum=0.1,
159
+ maximum=4.0,
160
+ value=0.7,
161
+ step=0.1,
162
+ label="Temperature"
163
+ )
164
+ top_p = gr.Slider(
165
+ minimum=0.1,
166
+ maximum=1.0,
167
+ value=0.95,
168
+ step=0.05,
169
+ label="Top-p"
170
+ )
171
+
172
+ # State for chat history
173
+ chat_history = gr.State([])
174
+
175
+ def user_message(message, history):
176
+ """Add user message to chat"""
177
+ return "", history + [[message, None]]
178
+
179
+ def bot_response(history, system_msg, max_tok, temp, top_p, tts_enabled):
180
+ """Generate bot response with optional TTS"""
181
+ if not history or not history[-1][0]:
182
+ return history, None
183
+
184
+ user_msg = history[-1][0]
185
+
186
+ # Generate response
187
+ for response, audio in respond_with_audio(
188
+ user_msg,
189
+ history[:-1],
190
+ system_msg,
191
+ max_tok,
192
+ temp,
193
+ top_p,
194
+ tts_enabled
195
+ ):
196
+ history[-1][1] = response
197
+ yield history, audio
198
+
199
+ def manual_tts(text):
200
+ """Generate TTS for manual input"""
201
+ if not text.strip():
202
+ return None
203
+ return generate_speech(text)
204
+
205
+ # Event handlers
206
+ msg.submit(
207
+ user_message,
208
+ [msg, chatbot],
209
+ [msg, chatbot],
210
+ queue=False
211
+ ).then(
212
+ bot_response,
213
+ [chatbot, system_message, max_tokens, temperature, top_p, enable_tts],
214
+ [chatbot, audio_output]
215
+ )
216
+
217
+ submit.click(
218
+ user_message,
219
+ [msg, chatbot],
220
+ [msg, chatbot],
221
+ queue=False
222
+ ).then(
223
+ bot_response,
224
+ [chatbot, system_message, max_tokens, temperature, top_p, enable_tts],
225
+ [chatbot, audio_output]
226
+ )
227
+
228
+ clear.click(lambda: ([], None), outputs=[chatbot, audio_output])
229
+
230
+ tts_button.click(
231
+ manual_tts,
232
+ inputs=[tts_input],
233
+ outputs=[audio_output]
234
+ )
235
+
236
+ # Add examples
237
+ gr.Examples(
238
+ examples=[
239
+ ["Hello! How are you today?"],
240
+ ["Tell me a short joke [laughs]"],
241
+ ["Explain quantum physics in simple terms"],
242
+ ["What's the weather like? [sighs]"]
243
+ ],
244
+ inputs=[msg],
245
+ label="Example messages (try the ones with [laughs] or [sighs] for TTS effects!)"
246
+ )
247
 
248
  if __name__ == "__main__":
249
+ demo.launch()