NikaMimi commited on
Commit
6e33acd
·
verified ·
1 Parent(s): 806d1b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +259 -259
app.py CHANGED
@@ -1,260 +1,260 @@
1
- import gradio as gr
2
- import requests
3
- import os
4
- import time
5
- import json
6
- from dotenv import load_dotenv
7
- import collections # For deque and defaultdict
8
- import threading # For Lock
9
-
10
- # --- Configuration & Constants ---
11
- load_dotenv()
12
-
13
- REPLICATE_API_TOKENS_STR = os.getenv("REPLICATE_API_TOKENS")
14
- if not REPLICATE_API_TOKENS_STR:
15
- print("WARNING: REPLICATE_API_TOKENS not found. App will not function.")
16
- REPLICATE_API_KEYS = []
17
- else:
18
- REPLICATE_API_KEYS = [token.strip() for token in REPLICATE_API_TOKENS_STR.split(',')]
19
-
20
- MODEL_ENDPOINT = "https://api.replicate.com/v1/models/minimax/speech-02-hd/predictions"
21
-
22
- VOICE_ID_MAP = {}
23
- try:
24
- with open("voices.json", "r", encoding="utf-8") as f:
25
- VOICE_ID_MAP = json.load(f)
26
- if not VOICE_ID_MAP: print("WARNING: voices.json is empty or could not be loaded.")
27
- VOICE_ID_PRETTY_NAMES = list(VOICE_ID_MAP.keys())
28
- DEFAULT_VOICE_PRETTY_NAME = "Friendly Person" if "Friendly Person" in VOICE_ID_PRETTY_NAMES else (VOICE_ID_PRETTY_NAMES[0] if VOICE_ID_PRETTY_NAMES else None)
29
- except FileNotFoundError:
30
- print("ERROR: voices.json not found.")
31
- VOICE_ID_PRETTY_NAMES = []; DEFAULT_VOICE_PRETTY_NAME = None
32
- except json.JSONDecodeError:
33
- print("ERROR: voices.json is not valid JSON.")
34
- VOICE_ID_PRETTY_NAMES = []; DEFAULT_VOICE_PRETTY_NAME = None
35
-
36
- EMOTIONS = ["auto", "neutral", "happy", "sad", "angry", "fearful", "disgusted", "surprised"]
37
- SAMPLE_RATES = [8000, 16000, 22050, 24000, 32000, 44100]
38
- BITRATES = [32000, 64000, 128000, 256000]
39
- CHANNELS = ["mono", "stereo"]
40
- LANGUAGE_BOOST_OPTIONS = ["None", "English", "Chinese", "Japanese", "Korean"]
41
-
42
- current_key_index = 0
43
- MAX_POLLING_ATTEMPTS = 60
44
- POLL_INTERVAL = 3
45
-
46
- # --- Per-IP Rate Limiting ---
47
- MAX_REQUESTS_PER_USER_PER_MINUTE = 2
48
- RATE_LIMIT_WINDOW_SECONDS = 60
49
- # Dictionary to store timestamps for each IP: { "ip_address": deque([...]), ... }
50
- ip_request_timestamps = collections.defaultdict(collections.deque)
51
- ip_rate_limit_lock = threading.Lock() # Protects the ip_request_timestamps dictionary
52
-
53
- def get_client_ip(request: gr.Request) -> str:
54
- """Extracts client IP from Gradio request, considering proxies."""
55
- x_forwarded_for = request.headers.get("x-forwarded-for")
56
- if x_forwarded_for:
57
- # The first IP in the list is usually the original client IP
58
- client_ip = x_forwarded_for.split(',')[0].strip()
59
- else:
60
- # Fallback to client.host, though it might be a proxy IP on some setups
61
- client_ip = request.client.host
62
- if not client_ip: # Should ideally not happen
63
- client_ip = "unknown_ip"
64
- return client_ip
65
-
66
- def get_next_api_key():
67
- global current_key_index
68
- if not REPLICATE_API_KEYS: return None
69
- key = REPLICATE_API_KEYS[current_key_index]
70
- current_key_index = (current_key_index + 1) % len(REPLICATE_API_KEYS)
71
- return key
72
-
73
- def generate_speech(
74
- text, pitch, speed, volume, bitrate, channel, emotion,
75
- voice_id_pretty_name, custom_voice_id, sample_rate,
76
- language_boost, english_normalization,
77
- request: gr.Request # Add gr.Request object to the function signature
78
- ):
79
- client_ip = get_client_ip(request)
80
- print(f"Request from IP: {client_ip}") # For logging/debugging
81
-
82
- # --- Apply Per-IP Rate Limiting ---
83
- with ip_rate_limit_lock:
84
- current_time = time.time()
85
- timestamps_for_this_ip = ip_request_timestamps[client_ip] # Get deque for this IP
86
-
87
- while timestamps_for_this_ip and timestamps_for_this_ip[0] < current_time - RATE_LIMIT_WINDOW_SECONDS:
88
- timestamps_for_this_ip.popleft()
89
-
90
- if len(timestamps_for_this_ip) >= MAX_REQUESTS_PER_USER_PER_MINUTE:
91
- gr.Warning(f"Rate limit exceeded for your IP address. Please wait. Max {MAX_REQUESTS_PER_USER_PER_MINUTE} requests per {RATE_LIMIT_WINDOW_SECONDS} seconds.")
92
- return None
93
-
94
- timestamps_for_this_ip.append(current_time)
95
- # --- End Per-IP Rate Limiting ---
96
-
97
- if not text.strip():
98
- gr.Warning("Text input cannot be empty.")
99
- return None
100
-
101
- # ... (rest of your existing generate_speech logic remains the same) ...
102
- if not REPLICATE_API_KEYS:
103
- gr.Error("No Replicate API Tokens configured. Admin: Please set REPLICATE_API_TOKENS in secrets.")
104
- return None
105
-
106
- if not VOICE_ID_MAP and not custom_voice_id.strip():
107
- gr.Error("Voice ID configuration is missing (voices.json empty/invalid) and no custom voice ID provided.")
108
- return None
109
-
110
- actual_voice_id_to_use = ""
111
- if custom_voice_id.strip():
112
- actual_voice_id_to_use = custom_voice_id.strip()
113
- elif voice_id_pretty_name and voice_id_pretty_name in VOICE_ID_MAP:
114
- actual_voice_id_to_use = VOICE_ID_MAP[voice_id_pretty_name]
115
- else:
116
- gr.Error(f"Selected voice '{voice_id_pretty_name}' not found in mappings and no custom ID provided.")
117
- return None
118
-
119
- payload = {
120
- "input": {
121
- "text": text, "pitch": int(pitch), "speed": float(speed), "volume": int(volume),
122
- "bitrate": int(bitrate), "channel": channel, "emotion": emotion,
123
- "voice_id": actual_voice_id_to_use, "sample_rate": int(sample_rate),
124
- "english_normalization": bool(english_normalization)
125
- }
126
- }
127
- if language_boost and language_boost.lower() != "none":
128
- payload["input"]["language_boost"] = language_boost
129
-
130
- num_keys_to_try = len(REPLICATE_API_KEYS)
131
- last_error_message_for_key = ""
132
-
133
- for i in range(num_keys_to_try):
134
- api_key = get_next_api_key()
135
- if not api_key:
136
- gr.Error("Internal error: No API keys available in the cycling pool.")
137
- return None
138
-
139
- headers_post = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
140
- headers_get = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
141
-
142
- print(f"Attempting API call with key ...{api_key[-4:]} for IP {client_ip}. Voice ID: {actual_voice_id_to_use}")
143
- try:
144
- response = requests.post(MODEL_ENDPOINT, json=payload, headers=headers_post, timeout=30)
145
- response.raise_for_status()
146
- result = response.json()
147
- current_status = result.get("status")
148
- print(f"Initial API Response (Key ...{api_key[-4:]}, IP {client_ip}): Status '{current_status}'")
149
- prediction_url = result.get("urls", {}).get("get")
150
- logs_from_initial_call = result.get("logs")
151
- polling_attempts = 0
152
- while current_status in ["starting", "processing"] and prediction_url:
153
- if polling_attempts >= MAX_POLLING_ATTEMPTS:
154
- last_error_message_for_key = f"Polling timed out for key ...{api_key[-4:]}."
155
- print(last_error_message_for_key)
156
- result["error"] = "Polling timed out."
157
- current_status = "failed_polling_timeout"
158
- break
159
- polling_attempts += 1
160
- time.sleep(POLL_INTERVAL)
161
- poll_response = requests.get(prediction_url, headers=headers_get, timeout=30)
162
- poll_response.raise_for_status()
163
- result = poll_response.json()
164
- current_status = result.get("status")
165
-
166
- if current_status == "succeeded":
167
- audio_url = result.get("output")
168
- if audio_url:
169
- success_logs = result.get('logs', logs_from_initial_call if logs_from_initial_call else 'N/A')
170
- print(f"Success with key ...{api_key[-4:]} for IP {client_ip}. Logs: {success_logs}")
171
- gr.Info("Success! Audio generated.")
172
- return audio_url
173
- else:
174
- last_error_message_for_key = f"API succeeded (Key ...{api_key[-4:]}) but no output URL. Resp: {result}"
175
- print(last_error_message_for_key)
176
- continue
177
- else:
178
- error_detail = result.get("error", f"Unknown error or unexpected status '{current_status}'")
179
- last_error_message_for_key = f"Prediction failed/timed out for key ...{api_key[-4:]}. Status: {current_status}. Error: {error_detail}"
180
- print(last_error_message_for_key)
181
- continue
182
- except requests.exceptions.HTTPError as e:
183
- error_text = "Unknown HTTP Error"; text_content =""
184
- try: text_content = e.response.text
185
- except AttributeError: pass
186
- last_error_message_for_key = f"HTTP error for key ...{api_key[-4:]}: {e.response.status_code} - {text_content}"
187
- print(last_error_message_for_key)
188
- continue
189
- except requests.exceptions.RequestException as e:
190
- last_error_message_for_key = f"Request exception for key ...{api_key[-4:]}: {e}"
191
- print(last_error_message_for_key)
192
- continue
193
-
194
- final_error_message = "All API keys failed or an unrecoverable error occurred."
195
- if last_error_message_for_key:
196
- final_error_message += f" Last attempt error: {last_error_message_for_key}"
197
- gr.Error(final_error_message)
198
- return None
199
-
200
-
201
- # --- Gradio UI (no changes needed here for this feature) ---
202
- with gr.Blocks(theme=gr.themes.Soft()) as app:
203
- gr.Markdown("# Purr Machine")
204
- gr.Markdown("Enter text and adjust parameters to generate speech.")
205
- # ... (rest of your UI definition is unchanged) ...
206
- with gr.Row():
207
- with gr.Column(scale=2):
208
- text_input = gr.Textbox(
209
- label="Text to Synthesize",
210
- lines=5,
211
- placeholder="Enter your text here...\n💡Insert '<#0.5#>' to add a 0.5s pause. Adjust duration."
212
- )
213
- with gr.Accordion("Voice Selection", open=True):
214
- voice_id_dropdown = gr.Dropdown(
215
- label="Choose a Voice ID",
216
- choices=VOICE_ID_PRETTY_NAMES,
217
- value=DEFAULT_VOICE_PRETTY_NAME
218
- )
219
- custom_voice_id_input = gr.Textbox(
220
- label="Custom Voice ID (Optional)",
221
- placeholder="e.g., my_cloned_voice_v2",
222
- info="If filled, this will override dropdown."
223
- )
224
- gr.Markdown("[Minimax Voices](https://www.minimax.io/audio/voices) for more options.")
225
-
226
- with gr.Accordion("Advanced Speech Parameters", open=False):
227
- speed_slider = gr.Slider(label="Speed", minimum=0.5, maximum=2, step=0.1, value=1.0)
228
- volume_slider = gr.Slider(label="Volume", minimum=0, maximum=10, step=1, value=1)
229
- pitch_slider = gr.Slider(label="Pitch", minimum=-12, maximum=12, step=1, value=0)
230
- english_norm_checkbox = gr.Checkbox(label="English Normalization", value=False, info="Improves number reading.")
231
-
232
- with gr.Accordion("Audio Format & Emotion", open=False):
233
- emotion_dropdown = gr.Dropdown(label="Emotion", choices=EMOTIONS, value="auto")
234
- sample_rate_dropdown = gr.Dropdown(label="Sample Rate (Hz)", choices=SAMPLE_RATES, value=32000, type="value")
235
- bitrate_dropdown = gr.Dropdown(label="Bitrate (bps)", choices=BITRATES, value=128000, type="value")
236
- channel_dropdown = gr.Dropdown(label="Channels", choices=CHANNELS, value="mono")
237
- language_boost_dropdown = gr.Dropdown(label="Language Boost", choices=LANGUAGE_BOOST_OPTIONS, value="None")
238
-
239
- with gr.Column(scale=1):
240
- generate_button = gr.Button("Generate Speech", variant="primary")
241
- audio_output = gr.Audio(label="Generated Speech", type="filepath")
242
-
243
- # When defining the click event, Gradio will automatically pass the gr.Request
244
- # object to generate_speech if it's listed in its parameters.
245
- generate_button.click(
246
- fn=generate_speech,
247
- inputs=[
248
- text_input, pitch_slider, speed_slider, volume_slider,
249
- bitrate_dropdown, channel_dropdown, emotion_dropdown,
250
- voice_id_dropdown, custom_voice_id_input, sample_rate_dropdown,
251
- language_boost_dropdown, english_norm_checkbox
252
- # No need to explicitly add 'request' to inputs list here for gr.Request
253
- ],
254
- outputs=[audio_output]
255
- )
256
-
257
- if __name__ == "__main__":
258
- if not REPLICATE_API_KEYS: print("FATAL: REPLICATE_API_TOKENS are not set.")
259
- if not VOICE_ID_MAP: print("WARNING: Voice ID map is empty (voices.json issue?).")
260
  app.launch(debug=True)
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import time
5
+ import json
6
+ from dotenv import load_dotenv
7
+ import collections # For deque and defaultdict
8
+ import threading # For Lock
9
+
10
+ # --- Configuration & Constants ---
11
+ load_dotenv()
12
+
13
+ REPLICATE_API_TOKENS_STR = os.getenv("REPLICATE_API_TOKENS")
14
+ if not REPLICATE_API_TOKENS_STR:
15
+ print("WARNING: REPLICATE_API_TOKENS not found. App will not function.")
16
+ REPLICATE_API_KEYS = []
17
+ else:
18
+ REPLICATE_API_KEYS = [token.strip() for token in REPLICATE_API_TOKENS_STR.split(',')]
19
+
20
+ MODEL_ENDPOINT = "https://api.replicate.com/v1/models/minimax/speech-02-hd/predictions"
21
+
22
+ VOICE_ID_MAP = {}
23
+ try:
24
+ with open("voices.json", "r", encoding="utf-8") as f:
25
+ VOICE_ID_MAP = json.load(f)
26
+ if not VOICE_ID_MAP: print("WARNING: voices.json is empty or could not be loaded.")
27
+ VOICE_ID_PRETTY_NAMES = list(VOICE_ID_MAP.keys())
28
+ DEFAULT_VOICE_PRETTY_NAME = "Friendly Person" if "Friendly Person" in VOICE_ID_PRETTY_NAMES else (VOICE_ID_PRETTY_NAMES[0] if VOICE_ID_PRETTY_NAMES else None)
29
+ except FileNotFoundError:
30
+ print("ERROR: voices.json not found.")
31
+ VOICE_ID_PRETTY_NAMES = []; DEFAULT_VOICE_PRETTY_NAME = None
32
+ except json.JSONDecodeError:
33
+ print("ERROR: voices.json is not valid JSON.")
34
+ VOICE_ID_PRETTY_NAMES = []; DEFAULT_VOICE_PRETTY_NAME = None
35
+
36
+ EMOTIONS = ["auto", "neutral", "happy", "sad", "angry", "fearful", "disgusted", "surprised"]
37
+ SAMPLE_RATES = [8000, 16000, 22050, 24000, 32000, 44100]
38
+ BITRATES = [32000, 64000, 128000, 256000]
39
+ CHANNELS = ["mono", "stereo"]
40
+ LANGUAGE_BOOST_OPTIONS = ["None", "English", "Chinese", "Japanese", "Korean"]
41
+
42
+ current_key_index = 0
43
+ MAX_POLLING_ATTEMPTS = 60
44
+ POLL_INTERVAL = 3
45
+
46
+ # --- Per-IP Rate Limiting ---
47
+ MAX_REQUESTS_PER_USER_PER_MINUTE = 2
48
+ RATE_LIMIT_WINDOW_SECONDS = 60
49
+ # Dictionary to store timestamps for each IP: { "ip_address": deque([...]), ... }
50
+ ip_request_timestamps = collections.defaultdict(collections.deque)
51
+ ip_rate_limit_lock = threading.Lock() # Protects the ip_request_timestamps dictionary
52
+
53
+ def get_client_ip(request: gr.Request) -> str:
54
+ """Extracts client IP from Gradio request, considering proxies."""
55
+ x_forwarded_for = request.headers.get("x-forwarded-for")
56
+ if x_forwarded_for:
57
+ # The first IP in the list is usually the original client IP
58
+ client_ip = x_forwarded_for.split(',')[0].strip()
59
+ else:
60
+ # Fallback to client.host, though it might be a proxy IP on some setups
61
+ client_ip = request.client.host
62
+ if not client_ip: # Should ideally not happen
63
+ client_ip = "unknown_ip"
64
+ return client_ip
65
+
66
+ def get_next_api_key():
67
+ global current_key_index
68
+ if not REPLICATE_API_KEYS: return None
69
+ key = REPLICATE_API_KEYS[current_key_index]
70
+ current_key_index = (current_key_index + 1) % len(REPLICATE_API_KEYS)
71
+ return key
72
+
73
+ def generate_speech(
74
+ text, pitch, speed, volume, bitrate, channel, emotion,
75
+ voice_id_pretty_name, custom_voice_id, sample_rate,
76
+ language_boost, english_normalization,
77
+ request: gr.Request # Add gr.Request object to the function signature
78
+ ):
79
+ client_ip = get_client_ip(request)
80
+ print(f"Request from IP: {client_ip}") # For logging/debugging
81
+
82
+ # --- Apply Per-IP Rate Limiting ---
83
+ with ip_rate_limit_lock:
84
+ current_time = time.time()
85
+ timestamps_for_this_ip = ip_request_timestamps[client_ip] # Get deque for this IP
86
+
87
+ while timestamps_for_this_ip and timestamps_for_this_ip[0] < current_time - RATE_LIMIT_WINDOW_SECONDS:
88
+ timestamps_for_this_ip.popleft()
89
+
90
+ if len(timestamps_for_this_ip) >= MAX_REQUESTS_PER_USER_PER_MINUTE:
91
+ gr.Warning(f"Rate limit exceeded for your IP address. Please wait. Max {MAX_REQUESTS_PER_USER_PER_MINUTE} requests per {RATE_LIMIT_WINDOW_SECONDS} seconds.")
92
+ return None
93
+
94
+ timestamps_for_this_ip.append(current_time)
95
+ # --- End Per-IP Rate Limiting ---
96
+
97
+ if not text.strip():
98
+ gr.Warning("Text input cannot be empty.")
99
+ return None
100
+
101
+ # ... (rest of your existing generate_speech logic remains the same) ...
102
+ if not REPLICATE_API_KEYS:
103
+ gr.Error("No Replicate API Tokens configured. Admin: Please set REPLICATE_API_TOKENS in secrets.")
104
+ return None
105
+
106
+ if not VOICE_ID_MAP and not custom_voice_id.strip():
107
+ gr.Error("Voice ID configuration is missing (voices.json empty/invalid) and no custom voice ID provided.")
108
+ return None
109
+
110
+ actual_voice_id_to_use = ""
111
+ if custom_voice_id.strip():
112
+ actual_voice_id_to_use = custom_voice_id.strip()
113
+ elif voice_id_pretty_name and voice_id_pretty_name in VOICE_ID_MAP:
114
+ actual_voice_id_to_use = VOICE_ID_MAP[voice_id_pretty_name]
115
+ else:
116
+ gr.Error(f"Selected voice '{voice_id_pretty_name}' not found in mappings and no custom ID provided.")
117
+ return None
118
+
119
+ payload = {
120
+ "input": {
121
+ "text": text, "pitch": int(pitch), "speed": float(speed), "volume": int(volume),
122
+ "bitrate": int(bitrate), "channel": channel, "emotion": emotion,
123
+ "voice_id": actual_voice_id_to_use, "sample_rate": int(sample_rate),
124
+ "english_normalization": bool(english_normalization)
125
+ }
126
+ }
127
+ if language_boost and language_boost.lower() != "none":
128
+ payload["input"]["language_boost"] = language_boost
129
+
130
+ num_keys_to_try = len(REPLICATE_API_KEYS)
131
+ last_error_message_for_key = ""
132
+
133
+ for i in range(num_keys_to_try):
134
+ api_key = get_next_api_key()
135
+ if not api_key:
136
+ gr.Error("Internal error: No API keys available in the cycling pool.")
137
+ return None
138
+
139
+ headers_post = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
140
+ headers_get = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
141
+
142
+ print(f"Attempting API call with key ...{api_key[-4:]} for IP {client_ip}. \nText: {text} \nVoice: {voice_id_pretty_name} \nVoice ID: {actual_voice_id_to_use}")
143
+ try:
144
+ response = requests.post(MODEL_ENDPOINT, json=payload, headers=headers_post, timeout=30)
145
+ response.raise_for_status()
146
+ result = response.json()
147
+ current_status = result.get("status")
148
+ print(f"Initial API Response (Key ...{api_key[-4:]}, IP {client_ip}): Status '{current_status}'")
149
+ prediction_url = result.get("urls", {}).get("get")
150
+ logs_from_initial_call = result.get("logs")
151
+ polling_attempts = 0
152
+ while current_status in ["starting", "processing"] and prediction_url:
153
+ if polling_attempts >= MAX_POLLING_ATTEMPTS:
154
+ last_error_message_for_key = f"Polling timed out for key ...{api_key[-4:]}."
155
+ print(last_error_message_for_key)
156
+ result["error"] = "Polling timed out."
157
+ current_status = "failed_polling_timeout"
158
+ break
159
+ polling_attempts += 1
160
+ time.sleep(POLL_INTERVAL)
161
+ poll_response = requests.get(prediction_url, headers=headers_get, timeout=30)
162
+ poll_response.raise_for_status()
163
+ result = poll_response.json()
164
+ current_status = result.get("status")
165
+
166
+ if current_status == "succeeded":
167
+ audio_url = result.get("output")
168
+ if audio_url:
169
+ success_logs = result.get('logs', logs_from_initial_call if logs_from_initial_call else 'N/A')
170
+ print(f"Success with key ...{api_key[-4:]} for IP {client_ip}. Logs: {success_logs}")
171
+ gr.Info("Success! Audio generated.")
172
+ return audio_url
173
+ else:
174
+ last_error_message_for_key = f"API succeeded (Key ...{api_key[-4:]}) but no output URL. Resp: {result}"
175
+ print(last_error_message_for_key)
176
+ continue
177
+ else:
178
+ error_detail = result.get("error", f"Unknown error or unexpected status '{current_status}'")
179
+ last_error_message_for_key = f"Prediction failed/timed out for key ...{api_key[-4:]}. Status: {current_status}. Error: {error_detail}"
180
+ print(last_error_message_for_key)
181
+ continue
182
+ except requests.exceptions.HTTPError as e:
183
+ error_text = "Unknown HTTP Error"; text_content =""
184
+ try: text_content = e.response.text
185
+ except AttributeError: pass
186
+ last_error_message_for_key = f"HTTP error for key ...{api_key[-4:]}: {e.response.status_code} - {text_content}"
187
+ print(last_error_message_for_key)
188
+ continue
189
+ except requests.exceptions.RequestException as e:
190
+ last_error_message_for_key = f"Request exception for key ...{api_key[-4:]}: {e}"
191
+ print(last_error_message_for_key)
192
+ continue
193
+
194
+ final_error_message = "All API keys failed or an unrecoverable error occurred."
195
+ if last_error_message_for_key:
196
+ final_error_message += f" Last attempt error: {last_error_message_for_key}"
197
+ gr.Error(final_error_message)
198
+ return None
199
+
200
+
201
+ # --- Gradio UI (no changes needed here for this feature) ---
202
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
203
+ gr.Markdown("# Purr Machine")
204
+ gr.Markdown("Enter text and adjust parameters to generate speech.")
205
+ # ... (rest of your UI definition is unchanged) ...
206
+ with gr.Row():
207
+ with gr.Column(scale=2):
208
+ text_input = gr.Textbox(
209
+ label="Text to Synthesize",
210
+ lines=5,
211
+ placeholder="Enter your text here...\n💡Insert '<#0.5#>' to add a 0.5s pause. Adjust duration."
212
+ )
213
+ with gr.Accordion("Voice Selection", open=True):
214
+ voice_id_dropdown = gr.Dropdown(
215
+ label="Choose a Voice ID",
216
+ choices=VOICE_ID_PRETTY_NAMES,
217
+ value=DEFAULT_VOICE_PRETTY_NAME
218
+ )
219
+ custom_voice_id_input = gr.Textbox(
220
+ label="Custom Voice ID (Optional)",
221
+ placeholder="e.g., my_cloned_voice_v2",
222
+ info="If filled, this will override dropdown."
223
+ )
224
+ gr.Markdown("[Minimax Voices](https://www.minimax.io/audio/voices) for more options.")
225
+
226
+ with gr.Accordion("Advanced Speech Parameters", open=False):
227
+ speed_slider = gr.Slider(label="Speed", minimum=0.5, maximum=2, step=0.1, value=1.0)
228
+ volume_slider = gr.Slider(label="Volume", minimum=0, maximum=10, step=1, value=1)
229
+ pitch_slider = gr.Slider(label="Pitch", minimum=-12, maximum=12, step=1, value=0)
230
+ english_norm_checkbox = gr.Checkbox(label="English Normalization", value=False, info="Improves number reading.")
231
+
232
+ with gr.Accordion("Audio Format & Emotion", open=False):
233
+ emotion_dropdown = gr.Dropdown(label="Emotion", choices=EMOTIONS, value="auto")
234
+ sample_rate_dropdown = gr.Dropdown(label="Sample Rate (Hz)", choices=SAMPLE_RATES, value=32000, type="value")
235
+ bitrate_dropdown = gr.Dropdown(label="Bitrate (bps)", choices=BITRATES, value=128000, type="value")
236
+ channel_dropdown = gr.Dropdown(label="Channels", choices=CHANNELS, value="mono")
237
+ language_boost_dropdown = gr.Dropdown(label="Language Boost", choices=LANGUAGE_BOOST_OPTIONS, value="None")
238
+
239
+ with gr.Column(scale=1):
240
+ generate_button = gr.Button("Generate Speech", variant="primary")
241
+ audio_output = gr.Audio(label="Generated Speech", type="filepath")
242
+
243
+ # When defining the click event, Gradio will automatically pass the gr.Request
244
+ # object to generate_speech if it's listed in its parameters.
245
+ generate_button.click(
246
+ fn=generate_speech,
247
+ inputs=[
248
+ text_input, pitch_slider, speed_slider, volume_slider,
249
+ bitrate_dropdown, channel_dropdown, emotion_dropdown,
250
+ voice_id_dropdown, custom_voice_id_input, sample_rate_dropdown,
251
+ language_boost_dropdown, english_norm_checkbox
252
+ # No need to explicitly add 'request' to inputs list here for gr.Request
253
+ ],
254
+ outputs=[audio_output]
255
+ )
256
+
257
+ if __name__ == "__main__":
258
+ if not REPLICATE_API_KEYS: print("FATAL: REPLICATE_API_TOKENS are not set.")
259
+ if not VOICE_ID_MAP: print("WARNING: Voice ID map is empty (voices.json issue?).")
260
  app.launch(debug=True)