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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
VM_ENDPOINT = os.environ.get("VM_ENDPOINT", "") # Add later via HF Secrets
|
| 6 |
+
|
| 7 |
+
# ββ Your samples + transcripts ββββββββββββββββββββββββββββββ
|
| 8 |
+
SAMPLES = [
|
| 9 |
+
{
|
| 10 |
+
"transcript": "Hello, this is Priya from customer support. [clears throat] Iβm calling regarding your refund request. The amount has been processed and should appear in your account within three business days.",
|
| 11 |
+
"file": "samples/sample_01.wav"
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"transcript": "Hey Rahul, are we still meeting for dinner tonight? [chuckles] I actually reached early for once, [m_laugh] so donβt make me wait outside again.",
|
| 15 |
+
"file": "samples/sample_02.wav"
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"transcript": "Good morning. [m_clear throat] You have a meeting with the research team at 10 A.M., followed by a dentist appointment in the evening.",
|
| 19 |
+
"file": "samples/sample_03.wav"
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"transcript": "The current temperature is twenty-seven degrees Celsius. [clears throat] Light rain is expected after 4 P.M., so carrying an umbrella is recommended.",
|
| 23 |
+
"file": "samples/sample_04.wav"
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"transcript": "Did you finish your homework before watching videos? [sigh] Dinner is almost ready, and Iβve reminded you several times already.",
|
| 27 |
+
"file": "samples/sample_05.wav"
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"transcript": "Your order is currently being prepared and should arrive soon. [m_clear throat] Thank you for choosing our delivery service today.",
|
| 31 |
+
"file": "samples/sample_06.wav"
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"transcript": "Hello, this is a reminder for your appointment tomorrow at 11:30 A.M. [clears throat] Please arrive ten minutes early for registration.",
|
| 35 |
+
"file": "samples/sample_07.wav"
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"transcript": "We need to finalize the evaluation report before Friday. [m_sigh] Can you also review the benchmark results after lunch?",
|
| 39 |
+
"file": "samples/sample_08.wav"
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"transcript": "Attention passengers traveling to Bengaluru. [clears throat] Your flight has been delayed due to weather conditions. We regret the inconvenience.",
|
| 43 |
+
"file": "samples/sample_09.wav"
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"transcript": "This is an automated security notification. [m_clear throat] A login attempt was detected from a new device associated with your account.",
|
| 47 |
+
"file": "samples/sample_10.wav"
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"transcript": "Hey, you sounded stressed yesterday. [m_sigh] I just wanted to know if youβre feeling any better today.",
|
| 51 |
+
"file": "samples/sample_11.wav"
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"transcript": "The living room lights are now turned off. [clears throat] Your washing machine cycle will finish in approximately twelve minutes.",
|
| 55 |
+
"file": "samples/sample_12.wav"
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"transcript": "Please submit your assignments before midnight today. [sigh] Late submissions may receive reduced marks without prior approval.",
|
| 59 |
+
"file": "samples/sample_13.wav"
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"transcript": "Hi, I ordered a laptop last week and it still hasnβt arrived. [sigh] I need an update because this delay is affecting my work.",
|
| 63 |
+
"file": "samples/sample_14.wav"
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"transcript": "Warning. Heavy rainfall may cause flooding in nearby areas tonight. [m_clear throat] Residents are advised to avoid unnecessary travel.",
|
| 67 |
+
"file": "samples/sample_15.wav"
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"transcript": "We should leave early on Saturday morning to avoid traffic. [s_laugh] Last time we spent four hours stuck on the highway.",
|
| 71 |
+
"file": "samples/sample_16.wav"
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"transcript": "Hello, Iβm calling regarding your interview for the machine learning engineer role. [clears throat] Weβd like to schedule a technical discussion this week.",
|
| 75 |
+
"file": "samples/sample_17.wav"
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"transcript": "I found three products matching your search query. [m_clear throat] The highest-rated option currently has a fifteen percent discount.",
|
| 79 |
+
"file": "samples/sample_18.wav"
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"transcript": "[m_laugh] When I was your age, we walked to school every day. Even during heavy rain, we carried books in plastic covers.",
|
| 83 |
+
"file": "samples/sample_19.wav"
|
| 84 |
+
},
|
| 85 |
+
{
|
| 86 |
+
"transcript": "Did you watch the new AI demo yesterday? [chuckles] The response speed looked impressive, [m_sigh] though a few answers were inaccurate.",
|
| 87 |
+
"file": "samples/sample_20.wav"
|
| 88 |
+
}
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
SSML_TAGS = """
|
| 92 |
+
| Tag | Effect | Example Usage|
|
| 93 |
+
|-----|--------|----------------|
|
| 94 |
+
| `[clear_throat]` | Clear throat β highest intensity | `[clear_throat] Hello, welcome.` |
|
| 95 |
+
| `[m_clear_throat]` | Clear throat β medium intensity | `[m_clear_throat] Let me explain.` |
|
| 96 |
+
| `[sighs]` | Sigh β highest intensity | `I can't believe it. [sighs]` |
|
| 97 |
+
| `[m_sighs]` | Sigh β medium intensity | `[m_sighs] That was a long day.` |
|
| 98 |
+
| `[laugh]` | Laugh β highest intensity | `That's so funny! [laugh]` |
|
| 99 |
+
| `[s_laugh]` | Laugh β subtle/lesser intensity | `Oh really? [s_laugh] Interesting.` |
|
| 100 |
+
| `[chuckles]` | Chuckles β highest intensity | `Well [chuckles] that was unexpected.` |
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
# ββ Inference function βββββββββββββββββββββββββββββββββββββββ
|
| 104 |
+
def run_tts(text):
|
| 105 |
+
if not text.strip():
|
| 106 |
+
return None, "β οΈ Please enter some text."
|
| 107 |
+
if len(text) > 500:
|
| 108 |
+
return None, "β οΈ Please keep input under 500 characters."
|
| 109 |
+
if not VM_ENDPOINT:
|
| 110 |
+
return None, "β οΈ Live inference endpoint not configured yet."
|
| 111 |
+
try:
|
| 112 |
+
response = requests.post(
|
| 113 |
+
VM_ENDPOINT,
|
| 114 |
+
json={"text": text},
|
| 115 |
+
timeout=30
|
| 116 |
+
)
|
| 117 |
+
if response.status_code == 200:
|
| 118 |
+
tmp_path = "/tmp/tts_output.wav"
|
| 119 |
+
with open(tmp_path, "wb") as f:
|
| 120 |
+
f.write(response.content)
|
| 121 |
+
return tmp_path, "β
Audio generated!"
|
| 122 |
+
else:
|
| 123 |
+
return None, f"β Server error: {response.status_code}"
|
| 124 |
+
except requests.exceptions.Timeout:
|
| 125 |
+
return None, "β Request timed out."
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return None, f"β Error: {str(e)}"
|
| 128 |
+
|
| 129 |
+
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 130 |
+
with gr.Blocks(
|
| 131 |
+
title="TTS Demo",
|
| 132 |
+
theme=gr.themes.Soft(),
|
| 133 |
+
css="""
|
| 134 |
+
.sample-row { border: 1px solid #e0e0e0; border-radius: 8px; padding: 12px; margin-bottom: 8px; }
|
| 135 |
+
.ssml-table { font-size: 0.85em; }
|
| 136 |
+
footer { display: none !important; }
|
| 137 |
+
"""
|
| 138 |
+
) as demo:
|
| 139 |
+
|
| 140 |
+
gr.Markdown("""
|
| 141 |
+
# π£οΈ TTS Demo
|
| 142 |
+
**Single female voice Β· English Β· SSML supported**
|
| 143 |
+
| [π Paper](#) | [π» Code](#)
|
| 144 |
+
""")
|
| 145 |
+
|
| 146 |
+
# ββ Top Section: Input | Output ββββββββββββββββββββββββββ
|
| 147 |
+
with gr.Row(equal_height=True):
|
| 148 |
+
|
| 149 |
+
# Left β Text Input
|
| 150 |
+
with gr.Column(scale=1):
|
| 151 |
+
gr.Markdown("### βοΈ Input Text")
|
| 152 |
+
text_input = gr.Textbox(
|
| 153 |
+
label="",
|
| 154 |
+
placeholder="Type your text here...\nYou can use SSML tags from the reference below.",
|
| 155 |
+
lines=8,
|
| 156 |
+
max_lines=12,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# SSML reference below the text box
|
| 160 |
+
gr.Markdown("#### π·οΈ Supported SSML Tags")
|
| 161 |
+
gr.Markdown(SSML_TAGS, elem_classes=["ssml-table"])
|
| 162 |
+
|
| 163 |
+
with gr.Row():
|
| 164 |
+
submit_btn = gr.Button("π Generate Audio", variant="primary", scale=2)
|
| 165 |
+
clear_btn = gr.Button("ποΈ Clear", scale=1)
|
| 166 |
+
|
| 167 |
+
status_msg = gr.Markdown("")
|
| 168 |
+
|
| 169 |
+
# Right β Audio Output
|
| 170 |
+
with gr.Column(scale=1):
|
| 171 |
+
gr.Markdown("### π§ Generated Audio")
|
| 172 |
+
audio_output = gr.Audio(
|
| 173 |
+
label="",
|
| 174 |
+
interactive=False,
|
| 175 |
+
show_download_button=True,
|
| 176 |
+
)
|
| 177 |
+
gr.Markdown("""
|
| 178 |
+
<br><br>
|
| 179 |
+
**Tips:**
|
| 180 |
+
- Use SSML tags for finer control over pronunciation
|
| 181 |
+
- Max 500 characters per request
|
| 182 |
+
- Audio will auto-play after generation
|
| 183 |
+
""")
|
| 184 |
+
|
| 185 |
+
# ββ Button Actions βββββββββββββββββββββββββββββββββββββββ
|
| 186 |
+
submit_btn.click(
|
| 187 |
+
fn=run_tts,
|
| 188 |
+
inputs=[text_input],
|
| 189 |
+
outputs=[audio_output, status_msg]
|
| 190 |
+
)
|
| 191 |
+
clear_btn.click(
|
| 192 |
+
fn=lambda: ("", None, ""),
|
| 193 |
+
outputs=[text_input, audio_output, status_msg]
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
gr.Markdown("---")
|
| 197 |
+
|
| 198 |
+
# ββ Bottom Section: Audio Samples βββββββββββββββββββββββ
|
| 199 |
+
gr.Markdown("### π Audio Samples")
|
| 200 |
+
gr.Markdown("Reference outputs from our test set β no SSML tags used.")
|
| 201 |
+
|
| 202 |
+
for i, sample in enumerate(SAMPLES):
|
| 203 |
+
with gr.Row(elem_classes=["sample-row"]):
|
| 204 |
+
with gr.Column(scale=3):
|
| 205 |
+
gr.Markdown(f"**Sample {i+1}**")
|
| 206 |
+
gr.Markdown(f"*\"{sample['transcript']}\"*")
|
| 207 |
+
with gr.Column(scale=2):
|
| 208 |
+
gr.Audio(
|
| 209 |
+
value=sample["file"],
|
| 210 |
+
label="",
|
| 211 |
+
interactive=False,
|
| 212 |
+
show_download_button=False,
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
requests>=2.28.0
|