Spaces:
Running
Running
Commit
·
673efce
1
Parent(s):
8d854d6
added counter and name
Browse files- .gitignore +45 -0
- app.py +56 -11
.gitignore
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Generation counter persistence file
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# Python
|
| 5 |
+
__pycache__/
|
| 6 |
+
*.py[cod]
|
| 7 |
+
*$py.class
|
| 8 |
+
*.so
|
| 9 |
+
.Python
|
| 10 |
+
build/
|
| 11 |
+
develop-eggs/
|
| 12 |
+
dist/
|
| 13 |
+
downloads/
|
| 14 |
+
eggs/
|
| 15 |
+
.eggs/
|
| 16 |
+
lib/
|
| 17 |
+
lib64/
|
| 18 |
+
parts/
|
| 19 |
+
sdist/
|
| 20 |
+
var/
|
| 21 |
+
wheels/
|
| 22 |
+
*.egg-info/
|
| 23 |
+
.installed.cfg
|
| 24 |
+
*.egg
|
| 25 |
+
|
| 26 |
+
# Virtual environments
|
| 27 |
+
.env
|
| 28 |
+
.venv
|
| 29 |
+
env/
|
| 30 |
+
venv/
|
| 31 |
+
ENV/
|
| 32 |
+
env.bak/
|
| 33 |
+
venv.bak/
|
| 34 |
+
|
| 35 |
+
# IDE
|
| 36 |
+
.vscode/
|
| 37 |
+
.idea/
|
| 38 |
+
*.swp
|
| 39 |
+
*.swo
|
| 40 |
+
*~
|
| 41 |
+
|
| 42 |
+
# OS
|
| 43 |
+
.DS_Store
|
| 44 |
+
Thumbs.db
|
| 45 |
+
|
app.py
CHANGED
|
@@ -2,11 +2,16 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# gr.NO_RELOAD = False
|
| 7 |
# API Base URL
|
| 8 |
BASE_URL = os.environ.get("BASE_URL", "")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# Example texts
|
| 11 |
EXAMPLE_TEXT_ENGLISH = "Welcome to Ringg TTS! This is a text to speech system that can convert your text into natural-sounding audio. Try it out with your own content!"
|
| 12 |
|
|
@@ -15,6 +20,27 @@ EXAMPLE_TEXT_HINDI = "नमस्ते! मैं रिंग टीटीए
|
|
| 15 |
EXAMPLE_TEXT_MIXED = "Hello दोस्तों! Welcome to Ringg TTS. यह एक बहुत ही शानदार text to speech system है जो Hindi और English दोनों languages को support करता है।"
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def get_voices():
|
| 19 |
"""Fetch available voices from API"""
|
| 20 |
try:
|
|
@@ -103,6 +129,9 @@ def synthesize_speech(text, voice_id):
|
|
| 103 |
return None, f"❌ Error: {str(e)}", "", "", "", "", "", ""
|
| 104 |
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
# Create Gradio interface
|
| 107 |
with gr.Blocks(
|
| 108 |
theme=gr.themes.Base(
|
|
@@ -113,14 +142,19 @@ with gr.Blocks(
|
|
| 113 |
# Title with Health Status
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
<
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# Best Practices Section
|
| 126 |
gr.Markdown("""
|
|
@@ -180,6 +214,9 @@ with gr.Blocks(
|
|
| 180 |
"- Special thanks to [@jeremylee12](https://huggingface.co/jeremylee12) for his contributions"
|
| 181 |
)
|
| 182 |
|
|
|
|
|
|
|
|
|
|
| 183 |
# Event Handlers
|
| 184 |
def update_char_count(text):
|
| 185 |
"""Update character count as user types"""
|
|
@@ -195,7 +232,7 @@ with gr.Blocks(
|
|
| 195 |
"""Clear text input"""
|
| 196 |
return "", "**Character count:** 0 / 500"
|
| 197 |
|
| 198 |
-
def on_generate(text, voice_display):
|
| 199 |
voice_id = voice_choices.get(voice_display)
|
| 200 |
audio_url, _status, t_time, rtf, wav_dur, voc_time, no_voc_time, rtf_no_voc = (
|
| 201 |
synthesize_speech(text, voice_id)
|
|
@@ -208,9 +245,13 @@ with gr.Blocks(
|
|
| 208 |
audio_response = requests.get(audio_url, timeout=30)
|
| 209 |
if audio_response.status_code == 200:
|
| 210 |
# Save to temporary file
|
| 211 |
-
audio_file = "/tmp/
|
| 212 |
with open(audio_file, "wb") as f:
|
| 213 |
f.write(audio_response.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
except Exception as e:
|
| 215 |
_status = f"⚠️ Audio generated but download failed: {str(e)}"
|
| 216 |
|
|
@@ -234,6 +275,8 @@ with gr.Blocks(
|
|
| 234 |
audio_file,
|
| 235 |
gr.update(visible=has_metrics),
|
| 236 |
gr.update(value=metrics_json, visible=has_metrics),
|
|
|
|
|
|
|
| 237 |
)
|
| 238 |
|
| 239 |
# Update character count on text input change
|
|
@@ -260,12 +303,14 @@ with gr.Blocks(
|
|
| 260 |
|
| 261 |
generate_btn.click(
|
| 262 |
fn=on_generate,
|
| 263 |
-
inputs=[text_input, voice_dropdown],
|
| 264 |
outputs=[
|
| 265 |
audio_output,
|
| 266 |
# status_output,
|
| 267 |
metrics_header,
|
| 268 |
metrics_output,
|
|
|
|
|
|
|
| 269 |
],
|
| 270 |
)
|
| 271 |
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import uuid
|
| 7 |
|
| 8 |
# gr.NO_RELOAD = False
|
| 9 |
# API Base URL
|
| 10 |
BASE_URL = os.environ.get("BASE_URL", "")
|
| 11 |
|
| 12 |
+
# Counter persistence file
|
| 13 |
+
COUNTER_FILE = Path("generation_counter.json")
|
| 14 |
+
|
| 15 |
# Example texts
|
| 16 |
EXAMPLE_TEXT_ENGLISH = "Welcome to Ringg TTS! This is a text to speech system that can convert your text into natural-sounding audio. Try it out with your own content!"
|
| 17 |
|
|
|
|
| 20 |
EXAMPLE_TEXT_MIXED = "Hello दोस्तों! Welcome to Ringg TTS. यह एक बहुत ही शानदार text to speech system है जो Hindi और English दोनों languages को support करता है।"
|
| 21 |
|
| 22 |
|
| 23 |
+
def load_counter():
|
| 24 |
+
"""Load generation counter from file"""
|
| 25 |
+
try:
|
| 26 |
+
if COUNTER_FILE.exists():
|
| 27 |
+
with open(COUNTER_FILE, "r") as f:
|
| 28 |
+
data = json.load(f)
|
| 29 |
+
return data.get("count", 0)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Error loading counter: {e}")
|
| 32 |
+
return 0
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def save_counter(count):
|
| 36 |
+
"""Save generation counter to file"""
|
| 37 |
+
try:
|
| 38 |
+
with open(COUNTER_FILE, "w") as f:
|
| 39 |
+
json.dump({"count": count}, f)
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error saving counter: {e}")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
def get_voices():
|
| 45 |
"""Fetch available voices from API"""
|
| 46 |
try:
|
|
|
|
| 129 |
return None, f"❌ Error: {str(e)}", "", "", "", "", "", ""
|
| 130 |
|
| 131 |
|
| 132 |
+
# Load initial counter value
|
| 133 |
+
initial_counter = load_counter()
|
| 134 |
+
|
| 135 |
# Create Gradio interface
|
| 136 |
with gr.Blocks(
|
| 137 |
theme=gr.themes.Base(
|
|
|
|
| 142 |
# Title with Health Status
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
+
with gr.Column(scale=4):
|
| 146 |
+
audio_image = gr.HTML(
|
| 147 |
+
value="""
|
| 148 |
+
<div style="display: flex; align-items: center; gap: 10px;">
|
| 149 |
+
<img style="width: 50px; height: 50px; background-color: white; border-radius: 10%;" src="https://storage.googleapis.com/desivocal-prod/desi-vocal/ringg.svg" alt="Logo">
|
| 150 |
+
<h1 style="margin: 0;">Ringg Squirrel TTS v1.0 🐿️</h1>
|
| 151 |
+
</div>
|
| 152 |
+
"""
|
| 153 |
+
)
|
| 154 |
+
with gr.Column(scale=1):
|
| 155 |
+
generation_counter = gr.Markdown(
|
| 156 |
+
f"**Generations:** {initial_counter}", elem_id="counter"
|
| 157 |
+
)
|
| 158 |
|
| 159 |
# Best Practices Section
|
| 160 |
gr.Markdown("""
|
|
|
|
| 214 |
"- Special thanks to [@jeremylee12](https://huggingface.co/jeremylee12) for his contributions"
|
| 215 |
)
|
| 216 |
|
| 217 |
+
# State variable for generation counter
|
| 218 |
+
gen_count_state = gr.State(value=initial_counter)
|
| 219 |
+
|
| 220 |
# Event Handlers
|
| 221 |
def update_char_count(text):
|
| 222 |
"""Update character count as user types"""
|
|
|
|
| 232 |
"""Clear text input"""
|
| 233 |
return "", "**Character count:** 0 / 500"
|
| 234 |
|
| 235 |
+
def on_generate(text, voice_display, gen_count):
|
| 236 |
voice_id = voice_choices.get(voice_display)
|
| 237 |
audio_url, _status, t_time, rtf, wav_dur, voc_time, no_voc_time, rtf_no_voc = (
|
| 238 |
synthesize_speech(text, voice_id)
|
|
|
|
| 245 |
audio_response = requests.get(audio_url, timeout=30)
|
| 246 |
if audio_response.status_code == 200:
|
| 247 |
# Save to temporary file
|
| 248 |
+
audio_file = f"/tmp/ringg_{str(uuid.uuid4())}.wav"
|
| 249 |
with open(audio_file, "wb") as f:
|
| 250 |
f.write(audio_response.content)
|
| 251 |
+
# Increment counter only on successful generation
|
| 252 |
+
gen_count += 1
|
| 253 |
+
# Save counter to file for persistence
|
| 254 |
+
save_counter(gen_count)
|
| 255 |
except Exception as e:
|
| 256 |
_status = f"⚠️ Audio generated but download failed: {str(e)}"
|
| 257 |
|
|
|
|
| 275 |
audio_file,
|
| 276 |
gr.update(visible=has_metrics),
|
| 277 |
gr.update(value=metrics_json, visible=has_metrics),
|
| 278 |
+
gen_count,
|
| 279 |
+
f"**Generations:** {gen_count}",
|
| 280 |
)
|
| 281 |
|
| 282 |
# Update character count on text input change
|
|
|
|
| 303 |
|
| 304 |
generate_btn.click(
|
| 305 |
fn=on_generate,
|
| 306 |
+
inputs=[text_input, voice_dropdown, gen_count_state],
|
| 307 |
outputs=[
|
| 308 |
audio_output,
|
| 309 |
# status_output,
|
| 310 |
metrics_header,
|
| 311 |
metrics_output,
|
| 312 |
+
gen_count_state,
|
| 313 |
+
generation_counter,
|
| 314 |
],
|
| 315 |
)
|
| 316 |
|