MoShow commited on
Commit
7215eee
·
verified ·
1 Parent(s): 720b43c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +252 -54
app.py CHANGED
@@ -1,59 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
3
  import os
4
 
5
- # The token is securely pulled from the Hugging Face Space Secret named HUGGINGFACEHUB_API_TOKEN
6
- HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
7
-
8
- # Setup the inference client with your model and token
9
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
10
-
11
- def respond(
12
- message,
13
- history: list[tuple[str, str]],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
18
- ):
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
26
-
27
- messages.append({"role": "user", "content": message})
28
-
29
- response = ""
30
-
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = message.choices[0].delta.content
39
- response += token
40
- yield response
41
-
42
- demo = gr.ChatInterface(
43
- respond,
44
- additional_inputs=[
45
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
46
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
47
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
48
- gr.Slider(
49
- minimum=0.1,
50
- maximum=1.0,
51
- value=0.95,
52
- step=0.05,
53
- label="Top-p (nucleus sampling)",
54
- ),
55
- ],
56
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  if __name__ == "__main__":
59
- demo.launch()
 
 
 
 
 
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Models
4
+ Datasets
5
+ Spaces
6
+ Community
7
+ Docs
8
+ Pricing
9
+
10
+
11
+
12
+ Spaces:
13
+
14
+
15
+ MoShow
16
+ /
17
+ deeptalkv1
18
+
19
+ private
20
+
21
+ App
22
+ Files
23
+ Community
24
+ Settings
25
+ deeptalkv1
26
+ /
27
+ app.py
28
+
29
+ NihalGazi's picture
30
+ NihalGazi
31
+ Update app.py
32
+ 940ff5b
33
+ verified
34
+ 25 days ago
35
+ raw
36
+
37
+ Copy download link
38
+ history
39
+ blame
40
+ edit
41
+ delete
42
+
43
+ 7.74 kB
44
+ # app.py
45
  import gradio as gr
46
+ import requests
47
+ import random
48
+ import urllib.parse
49
+ import tempfile
50
  import os
51
 
52
+ NSFW_URL_TEMPLATE = os.getenv("NSFW_API_URL_TEMPLATE")
53
+ TTS_URL_TEMPLATE = os.getenv("TTS_API_URL_TEMPLATE")
54
+
55
+
56
+ if not NSFW_URL_TEMPLATE:
57
+ raise ValueError("Missing Secret: NSFW_API_URL_TEMPLATE is not set in Hugging Face Space secrets.")
58
+ if not TTS_URL_TEMPLATE:
59
+ raise ValueError("Missing Secret: TTS_API_URL_TEMPLATE is not set in Hugging Face Space secrets.")
60
+ # VOICES
61
+ VOICES = [
62
+ "alloy", "echo", "fable", "onyx", "nova", "shimmer", # Standard OpenAI Voices
63
+ "coral", "verse", "ballad", "ash", "sage", "amuch", "dan" # Some additional pre-trained
64
+ ]
65
+
66
+
67
+
68
+ def check_nsfw(prompt: str) -> bool:
69
+ global NSFW_URL_TEMPLATE
70
+ try:
71
+ encoded_prompt = urllib.parse.quote(prompt)
72
+ url = NSFW_URL_TEMPLATE.format(prompt=encoded_prompt)
73
+ print(f"DEBUG: Checking NSFW URL: {url.split('?')[0]}... (query params hidden)")
74
+
75
+ response = requests.get(url, timeout=20)
76
+ response.raise_for_status()
77
+
78
+ result = response.text.strip().upper()
79
+ print(f"DEBUG: NSFW Check Response: '{result}'")
80
+
81
+ if result == "YES":
82
+ return True
83
+ elif result == "NO":
84
+ return False
85
+ else:
86
+ print(f"Warning: Unexpected response from NSFW checker: {response.text}")
87
+ return True # unexpected responses = potentially NSFW
88
+
89
+ except requests.exceptions.RequestException as e:
90
+ print(f"Error during NSFW check: {e}")
91
+ raise gr.Error(f"Failed to check prompt safety.")
92
+ except Exception as e:
93
+ print(f"Unexpected error during NSFW check: {e}")
94
+ raise gr.Error(f"An unexpected error occurred during safety check. Please wait for a second and try again.")
95
+
96
+
97
+ def generate_audio(prompt: str, voice: str, emotion: str, seed: int) -> bytes:
98
+ # Generates audio using the API from server
99
+ global TTS_URL_TEMPLATE
100
+ try:
101
+ encoded_prompt = urllib.parse.quote(prompt)
102
+ encoded_emotion = urllib.parse.quote(emotion)
103
+
104
+ url = TTS_URL_TEMPLATE.format(
105
+ prompt=encoded_prompt,
106
+ emotion=encoded_emotion,
107
+ voice=voice,
108
+ seed=seed
109
+ )
110
+ print(f"DEBUG: Generating Audio URL: {url.split('?')[0]}... (query params hidden)")
111
+
112
+ response = requests.get(url, timeout=60)
113
+ response.raise_for_status()
114
+
115
+ content_type = response.headers.get('content-type', '').lower()
116
+ if 'audio' not in content_type:
117
+ print(f"Warning: Unexpected content type received: {content_type}")
118
+ print(f"Response Text: {response.text[:500]}")
119
+ raise gr.Error(f"API did not return audio.")
120
+
121
+ return response.content
122
+
123
+ except requests.exceptions.RequestException as e:
124
+ print(f"Error during audio generation: {e}")
125
+ error_details = ""
126
+ if hasattr(e, 'response') and e.response is not None:
127
+ error_details = e.response.text[:200]
128
+ raise gr.Error(f"Failed to generate audio. Please wait for a second and try again.")
129
+ except Exception as e:
130
+ print(f"Unexpected error during audio generation: {e}")
131
+ raise gr.Error(f"An unexpected error occurred during audio generation. Please wait for a second and try again.")
132
+
133
+
134
+
135
+ def text_to_speech_app(prompt: str, voice: str, emotion: str, use_random_seed: bool, specific_seed: int):
136
+
137
+ print("\n\n\n"+prompt+"\n\n\n")
138
+ if not prompt:
139
+ raise gr.Error("Prompt cannot be empty.")
140
+ if not emotion:
141
+ emotion = "neutral"
142
+ print("Warning: No emotion provided, defaulting to 'neutral'.")
143
+ if not voice:
144
+ raise gr.Error("Please select a voice.")
145
+
146
+ seed = random.randint(0, 2**32 - 1) if use_random_seed else int(specific_seed)
147
+ print(f"Using Seed: {seed}")
148
+
149
+ # check NSFW
150
+ print("Checking prompt safety...")
151
+ try:
152
+ # is_nsfw = check_nsfw(prompt)
153
+ is_nsfw = False
154
+ except gr.Error as e:
155
+ return None, f"There was an error. Please wait for a second and try again."
156
+
157
+ if is_nsfw:
158
+ print("Prompt flagged as inappropriate.")
159
+ return None, "Error: The prompt was flagged as inappropriate and cannot be processed."
160
+
161
+ # if not nsfw
162
+ print("Prompt is safe. Generating audio...")
163
+ try:
164
+ audio_bytes = generate_audio(prompt, voice, emotion, seed)
165
+
166
+ # audio save to a temporary file
167
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio_file:
168
+ temp_audio_file.write(audio_bytes)
169
+ temp_file_path = temp_audio_file.name
170
+ print(f"Audio saved temporarily to: {temp_file_path}")
171
+
172
+ return temp_file_path, f"Audio generated successfully with voice '{voice}', emotion '{emotion}', and seed {seed}."
173
+
174
+ except gr.Error as e:
175
+ return None, str(e)
176
+ except Exception as e:
177
+ print(f"Unexpected error in main function: {e}")
178
+ return None, f"An unexpected error occurred: {e}"
179
+
180
+
181
+
182
+
183
+ def toggle_seed_input(use_random_seed):
184
+
185
+ return gr.update(visible=not use_random_seed, value=12345)
186
+
187
+ with gr.Blocks() as app:
188
+ gr.Markdown("# Advanced OpenAI Text-To-Speech Unlimited")
189
+ gr.Markdown(
190
+ """Enter text, choose a voice and emotion, and generate audio.
191
+ The text will be checked for appropriateness before generation.
192
+ Use it as much as you want.
193
+
194
+
195
+ **Like & follow** for more AI projects:
196
+
197
+ • Instagram: [@nihal_gazi_io](https://www.instagram.com/nihal_gazi_io/)
198
+ • Discord: nihal_gazi_io"""
199
+ )
200
+
201
+ with gr.Row():
202
+ with gr.Column(scale=2):
203
+ prompt_input = gr.Textbox(label="Prompt", placeholder="Enter the text you want to convert to speech...")
204
+ emotion_input = gr.Textbox(label="Emotion Style", placeholder="e.g., happy, sad, excited, calm...")
205
+ voice_dropdown = gr.Dropdown(label="Voice", choices=VOICES, value="alloy")
206
+ with gr.Column(scale=1):
207
+ random_seed_checkbox = gr.Checkbox(label="Use Random Seed", value=True)
208
+ seed_input = gr.Number(label="Specific Seed", value=12345, visible=False, precision=0)
209
+
210
+ submit_button = gr.Button("Generate Audio", variant="primary")
211
+
212
+ with gr.Row():
213
+ audio_output = gr.Audio(label="Generated Audio", type="filepath")
214
+ status_output = gr.Textbox(label="Status")
215
+
216
+
217
+ random_seed_checkbox.change(
218
+ fn=toggle_seed_input,
219
+ inputs=[random_seed_checkbox],
220
+ outputs=[seed_input]
221
+ )
222
+
223
+ submit_button.click(
224
+ fn=text_to_speech_app,
225
+ inputs=[
226
+ prompt_input,
227
+ voice_dropdown,
228
+ emotion_input,
229
+ random_seed_checkbox,
230
+ seed_input
231
+ ],
232
+ outputs=[audio_output, status_output],
233
+ concurrency_limit=30
234
+ )
235
+
236
+
237
+ gr.Examples(
238
+ examples=[
239
+ ["Hello there! This is a test of the text-to-speech system.", "alloy", "neutral", False, 12345],
240
+ ["Surely *you* wouldn't want *that*. [laughs]", "shimmer", "sarcastic and mocking", True, 12345],
241
+ ["[sobbing] I am feeling... [sighs] a bit down today [cry]", "ballad", "sad and depressed, with stammering", True, 662437],
242
+ ["This technology is absolutely amazing!", "nova", "excited and joyful", True, 12345],
243
+ ],
244
+ inputs=[prompt_input, voice_dropdown, emotion_input, random_seed_checkbox, seed_input],
245
+ outputs=[audio_output, status_output],
246
+ fn=text_to_speech_app,
247
+ cache_examples=False,
248
+ )
249
+
250
 
251
  if __name__ == "__main__":
252
+
253
+ if NSFW_URL_TEMPLATE and TTS_URL_TEMPLATE:
254
+ app.launch()
255
+ else:
256
+ print("ERROR: Cannot launch app. Required API URL secrets are missing.")
257
+