Spaces:
Runtime error
Runtime error
Create tts_module,py
Browse files- tts_module,py +106 -0
tts_module,py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import random
|
4 |
+
import urllib.parse
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
NSFW_URL_TEMPLATE = os.getenv("NSFW_API_URL_TEMPLATE")
|
9 |
+
TTS_URL_TEMPLATE = os.getenv("TTS_API_URL_TEMPLATE")
|
10 |
+
|
11 |
+
VOICES = [
|
12 |
+
"alloy", "echo", "fable", "onyx", "nova", "shimmer",
|
13 |
+
"coral", "verse", "ballad", "ash", "sage", "amuch", "dan"
|
14 |
+
]
|
15 |
+
|
16 |
+
def check_nsfw(prompt: str) -> bool:
|
17 |
+
try:
|
18 |
+
encoded_prompt = urllib.parse.quote(prompt)
|
19 |
+
url = NSFW_URL_TEMPLATE.format(prompt=encoded_prompt)
|
20 |
+
response = requests.get(url, timeout=20)
|
21 |
+
result = response.text.strip().upper()
|
22 |
+
return result == "YES"
|
23 |
+
except:
|
24 |
+
return True # assume NSFW if check fails
|
25 |
+
|
26 |
+
def generate_audio(prompt: str, voice: str, emotion: str, seed: int) -> bytes:
|
27 |
+
try:
|
28 |
+
encoded_prompt = urllib.parse.quote(prompt)
|
29 |
+
encoded_emotion = urllib.parse.quote(emotion)
|
30 |
+
url = TTS_URL_TEMPLATE.format(
|
31 |
+
prompt=encoded_prompt, emotion=encoded_emotion, voice=voice, seed=seed
|
32 |
+
)
|
33 |
+
response = requests.get(url, timeout=60)
|
34 |
+
response.raise_for_status()
|
35 |
+
|
36 |
+
if 'audio' not in response.headers.get('content-type', '').lower():
|
37 |
+
raise gr.Error("API response is not audio.")
|
38 |
+
return response.content
|
39 |
+
|
40 |
+
except Exception as e:
|
41 |
+
print("Error:", e)
|
42 |
+
raise gr.Error("Error generating audio.")
|
43 |
+
|
44 |
+
def text_to_speech_app(prompt, voice, emotion, use_random_seed, specific_seed):
|
45 |
+
if not prompt: raise gr.Error("Prompt cannot be empty.")
|
46 |
+
if not voice: raise gr.Error("Voice selection required.")
|
47 |
+
if not emotion: emotion = "neutral"
|
48 |
+
|
49 |
+
seed = random.randint(0, 2**32 - 1) if use_random_seed else int(specific_seed)
|
50 |
+
is_nsfw = False # Set to check_nsfw(prompt) if needed
|
51 |
+
|
52 |
+
if is_nsfw:
|
53 |
+
return None, "⚠️ Inappropriate prompt detected."
|
54 |
+
|
55 |
+
audio_bytes = generate_audio(prompt, voice, emotion, seed)
|
56 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
57 |
+
tmp.write(audio_bytes)
|
58 |
+
return tmp.name, f"✅ Audio ready | Voice: {voice} | Emotion: {emotion} | Seed: {seed}"
|
59 |
+
|
60 |
+
def toggle_seed_input(use_random_seed):
|
61 |
+
return gr.update(visible=not use_random_seed, value=12345)
|
62 |
+
|
63 |
+
with gr.Blocks() as app:
|
64 |
+
gr.Markdown("## 🎙️ DeepCAL Oracle Voice Engine")
|
65 |
+
gr.Markdown("Enter text, select a voice and emotion, then generate oracle-grade audio.")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column(scale=2):
|
69 |
+
prompt_input = gr.Textbox(label="Prompt")
|
70 |
+
emotion_input = gr.Textbox(label="Emotion", placeholder="e.g., sarcastic, serious")
|
71 |
+
voice_dropdown = gr.Dropdown(label="Voice", choices=VOICES, value="alloy")
|
72 |
+
with gr.Column(scale=1):
|
73 |
+
random_seed_checkbox = gr.Checkbox(label="Use Random Seed", value=True)
|
74 |
+
seed_input = gr.Number(label="Specific Seed", visible=False, value=12345)
|
75 |
+
|
76 |
+
submit_button = gr.Button("🎧 Generate Audio")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
audio_output = gr.Audio(label="Audio Output", type="filepath")
|
80 |
+
status_output = gr.Textbox(label="Status")
|
81 |
+
|
82 |
+
random_seed_checkbox.change(toggle_seed_input, [random_seed_checkbox], [seed_input])
|
83 |
+
|
84 |
+
submit_button.click(
|
85 |
+
fn=text_to_speech_app,
|
86 |
+
inputs=[prompt_input, voice_dropdown, emotion_input, random_seed_checkbox, seed_input],
|
87 |
+
outputs=[audio_output, status_output],
|
88 |
+
concurrency_limit=30
|
89 |
+
)
|
90 |
+
|
91 |
+
gr.Examples(
|
92 |
+
examples=[
|
93 |
+
["Welcome to DeepCAL Oracle.", "ballad", "wise, cosmic", True, 12345],
|
94 |
+
["Headshot logistics incoming.", "shimmer", "sarcastic and aggressive", True, 5555],
|
95 |
+
],
|
96 |
+
inputs=[prompt_input, voice_dropdown, emotion_input, random_seed_checkbox, seed_input],
|
97 |
+
outputs=[audio_output, status_output],
|
98 |
+
fn=text_to_speech_app,
|
99 |
+
cache_examples=False
|
100 |
+
)
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
if NSFW_URL_TEMPLATE and TTS_URL_TEMPLATE:
|
104 |
+
app.launch()
|
105 |
+
else:
|
106 |
+
print("🚫 Missing required secrets. Please set NSFW_API_URL_TEMPLATE and TTS_API_URL_TEMPLATE.")
|