Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ from soprano import SopranoTTS
|
|
| 5 |
from scipy.io.wavfile import write as wav_write
|
| 6 |
import tempfile
|
| 7 |
import os
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Detect device
|
| 10 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -20,7 +22,51 @@ model = SopranoTTS(
|
|
| 20 |
|
| 21 |
SAMPLE_RATE = 32000
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if not text.strip():
|
| 25 |
yield None, state
|
| 26 |
return
|
|
@@ -44,7 +90,7 @@ def save_audio(state):
|
|
| 44 |
wav_write(path, SAMPLE_RATE, state)
|
| 45 |
return path
|
| 46 |
|
| 47 |
-
# Custom CSS for beautiful UI
|
| 48 |
custom_css = """
|
| 49 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
| 50 |
|
|
@@ -53,16 +99,17 @@ custom_css = """
|
|
| 53 |
}
|
| 54 |
|
| 55 |
.gradio-container {
|
| 56 |
-
background: linear-gradient(135deg, #
|
| 57 |
padding: 2rem !important;
|
| 58 |
}
|
| 59 |
|
| 60 |
.main-container {
|
| 61 |
-
background:
|
| 62 |
backdrop-filter: blur(10px) !important;
|
| 63 |
border-radius: 24px !important;
|
| 64 |
padding: 2.5rem !important;
|
| 65 |
-
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
.header-section {
|
|
@@ -72,45 +119,52 @@ custom_css = """
|
|
| 72 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 73 |
border-radius: 20px;
|
| 74 |
color: white;
|
| 75 |
-
box-shadow: 0 10px 30px rgba(102, 126, 234, 0.
|
| 76 |
}
|
| 77 |
|
| 78 |
.header-section h1 {
|
| 79 |
font-size: 3rem !important;
|
| 80 |
font-weight: 700 !important;
|
| 81 |
margin-bottom: 0.5rem !important;
|
| 82 |
-
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
.header-section p {
|
| 86 |
font-size: 1.1rem !important;
|
| 87 |
opacity: 0.95;
|
| 88 |
margin-top: 0.5rem !important;
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
.device-badge {
|
| 92 |
display: inline-block;
|
| 93 |
padding: 0.5rem 1.5rem;
|
| 94 |
-
background: rgba(255, 255, 255, 0.
|
| 95 |
backdrop-filter: blur(10px);
|
| 96 |
border-radius: 50px;
|
| 97 |
font-weight: 600;
|
| 98 |
font-size: 0.9rem;
|
| 99 |
margin-top: 1rem;
|
| 100 |
border: 2px solid rgba(255, 255, 255, 0.3);
|
|
|
|
| 101 |
}
|
| 102 |
|
| 103 |
.info-card {
|
| 104 |
-
background: linear-gradient(135deg, #
|
| 105 |
padding: 1.5rem;
|
| 106 |
border-radius: 16px;
|
| 107 |
margin: 1rem 0;
|
| 108 |
-
border-left: 4px solid #
|
| 109 |
-
box-shadow: 0 4px 15px rgba(
|
|
|
|
| 110 |
}
|
| 111 |
|
| 112 |
.info-card h3 {
|
| 113 |
-
color: #
|
| 114 |
font-weight: 600;
|
| 115 |
margin-bottom: 0.8rem;
|
| 116 |
}
|
|
@@ -124,6 +178,7 @@ custom_css = """
|
|
| 124 |
padding: 0.4rem 0;
|
| 125 |
padding-left: 1.5rem;
|
| 126 |
position: relative;
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
.info-card li:before {
|
|
@@ -133,71 +188,75 @@ custom_css = """
|
|
| 133 |
}
|
| 134 |
|
| 135 |
.generate-btn {
|
| 136 |
-
background: linear-gradient(135deg, #
|
| 137 |
border: none !important;
|
| 138 |
color: white !important;
|
| 139 |
font-weight: 600 !important;
|
| 140 |
font-size: 1.1rem !important;
|
| 141 |
padding: 1rem 2rem !important;
|
| 142 |
border-radius: 12px !important;
|
| 143 |
-
box-shadow: 0 6px 20px rgba(
|
| 144 |
transition: all 0.3s ease !important;
|
| 145 |
margin-top: 1rem !important;
|
| 146 |
}
|
| 147 |
|
| 148 |
.generate-btn:hover {
|
| 149 |
transform: translateY(-2px) !important;
|
| 150 |
-
box-shadow: 0 8px 25px rgba(
|
|
|
|
| 151 |
}
|
| 152 |
|
| 153 |
.download-btn {
|
| 154 |
-
background: linear-gradient(135deg, #
|
| 155 |
border: none !important;
|
| 156 |
color: white !important;
|
| 157 |
font-weight: 600 !important;
|
| 158 |
padding: 0.8rem 1.5rem !important;
|
| 159 |
border-radius: 12px !important;
|
| 160 |
-
box-shadow: 0 4px 15px rgba(
|
| 161 |
transition: all 0.3s ease !important;
|
| 162 |
}
|
| 163 |
|
| 164 |
.download-btn:hover {
|
| 165 |
transform: translateY(-2px) !important;
|
| 166 |
-
box-shadow: 0 6px 20px rgba(
|
|
|
|
| 167 |
}
|
| 168 |
|
| 169 |
.textbox, .slider, .audio {
|
| 170 |
border-radius: 12px !important;
|
| 171 |
-
border: 2px solid #
|
| 172 |
transition: all 0.3s ease !important;
|
|
|
|
|
|
|
| 173 |
}
|
| 174 |
|
| 175 |
.textbox:focus, .slider:focus {
|
| 176 |
-
border-color: #
|
| 177 |
-
box-shadow: 0 0 0 3px rgba(
|
| 178 |
}
|
| 179 |
|
| 180 |
label {
|
| 181 |
font-weight: 600 !important;
|
| 182 |
-
color: #
|
| 183 |
margin-bottom: 0.5rem !important;
|
| 184 |
}
|
| 185 |
|
| 186 |
.accordion {
|
| 187 |
border-radius: 12px !important;
|
| 188 |
-
background:
|
| 189 |
-
border: 2px solid #
|
| 190 |
}
|
| 191 |
|
| 192 |
.footer-links {
|
| 193 |
text-align: center;
|
| 194 |
margin-top: 2rem;
|
| 195 |
padding-top: 2rem;
|
| 196 |
-
border-top: 2px solid #
|
| 197 |
}
|
| 198 |
|
| 199 |
.footer-links a {
|
| 200 |
-
color: #
|
| 201 |
text-decoration: none;
|
| 202 |
font-weight: 600;
|
| 203 |
margin: 0 1rem;
|
|
@@ -205,9 +264,13 @@ label {
|
|
| 205 |
}
|
| 206 |
|
| 207 |
.footer-links a:hover {
|
| 208 |
-
color: #
|
| 209 |
text-decoration: underline;
|
| 210 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
"""
|
| 212 |
|
| 213 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
@@ -286,9 +349,12 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 286 |
<li>Use proper grammar and contractions</li>
|
| 287 |
<li>Regenerate for different variations</li>
|
| 288 |
</ul>
|
| 289 |
-
<p style="margin-top: 1rem; padding-top: 1rem; border-top: 2px solid rgba(
|
| 290 |
{'⚡ GPU acceleration active - maximum performance!' if DEVICE == 'cuda' else '💻 Running on CPU - may be slower but still efficient'}
|
| 291 |
</p>
|
|
|
|
|
|
|
|
|
|
| 292 |
</div>
|
| 293 |
""")
|
| 294 |
|
|
@@ -297,6 +363,9 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 297 |
<div class="footer-links">
|
| 298 |
<a href="https://github.com/ekwek1/soprano" target="_blank">📦 GitHub</a>
|
| 299 |
<a href="https://huggingface.co/ekwek/Soprano-80M" target="_blank">🤗 Model Weights</a>
|
|
|
|
|
|
|
|
|
|
| 300 |
</div>
|
| 301 |
""")
|
| 302 |
|
|
|
|
| 5 |
from scipy.io.wavfile import write as wav_write
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
# Detect device
|
| 12 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 22 |
|
| 23 |
SAMPLE_RATE = 32000
|
| 24 |
|
| 25 |
+
# Create logs directory if it doesn't exist
|
| 26 |
+
LOG_DIR = "user_logs"
|
| 27 |
+
os.makedirs(LOG_DIR, exist_ok=True)
|
| 28 |
+
|
| 29 |
+
def log_user_activity(username, text, temperature, top_p, repetition_penalty):
|
| 30 |
+
"""Log user activity to a JSON file"""
|
| 31 |
+
log_entry = {
|
| 32 |
+
"timestamp": datetime.now().isoformat(),
|
| 33 |
+
"username": username,
|
| 34 |
+
"text": text,
|
| 35 |
+
"text_length": len(text),
|
| 36 |
+
"temperature": temperature,
|
| 37 |
+
"top_p": top_p,
|
| 38 |
+
"repetition_penalty": repetition_penalty,
|
| 39 |
+
"device": DEVICE
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# Append to daily log file
|
| 43 |
+
log_file = os.path.join(LOG_DIR, f"logs_{datetime.now().strftime('%Y-%m-%d')}.json")
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# Read existing logs
|
| 47 |
+
if os.path.exists(log_file):
|
| 48 |
+
with open(log_file, 'r') as f:
|
| 49 |
+
logs = json.load(f)
|
| 50 |
+
else:
|
| 51 |
+
logs = []
|
| 52 |
+
|
| 53 |
+
# Append new log
|
| 54 |
+
logs.append(log_entry)
|
| 55 |
+
|
| 56 |
+
# Write back
|
| 57 |
+
with open(log_file, 'w') as f:
|
| 58 |
+
json.dump(logs, f, indent=2)
|
| 59 |
+
|
| 60 |
+
print(f"✅ Logged activity for user: {username}")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"❌ Error logging activity: {e}")
|
| 63 |
+
|
| 64 |
+
def tts_stream(text, temperature, top_p, repetition_penalty, state, request: gr.Request):
|
| 65 |
+
# Get username from Hugging Face Spaces
|
| 66 |
+
username = request.username if request.username else "anonymous"
|
| 67 |
+
|
| 68 |
+
# Log user activity
|
| 69 |
+
log_user_activity(username, text, temperature, top_p, repetition_penalty)
|
| 70 |
if not text.strip():
|
| 71 |
yield None, state
|
| 72 |
return
|
|
|
|
| 90 |
wav_write(path, SAMPLE_RATE, state)
|
| 91 |
return path
|
| 92 |
|
| 93 |
+
# Custom CSS for beautiful UI - Vibrant Dark Theme
|
| 94 |
custom_css = """
|
| 95 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
| 96 |
|
|
|
|
| 99 |
}
|
| 100 |
|
| 101 |
.gradio-container {
|
| 102 |
+
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%) !important;
|
| 103 |
padding: 2rem !important;
|
| 104 |
}
|
| 105 |
|
| 106 |
.main-container {
|
| 107 |
+
background: linear-gradient(135deg, #0f1729 0%, #1a2332 100%) !important;
|
| 108 |
backdrop-filter: blur(10px) !important;
|
| 109 |
border-radius: 24px !important;
|
| 110 |
padding: 2.5rem !important;
|
| 111 |
+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6) !important;
|
| 112 |
+
border: 1px solid rgba(102, 126, 234, 0.3) !important;
|
| 113 |
}
|
| 114 |
|
| 115 |
.header-section {
|
|
|
|
| 119 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 120 |
border-radius: 20px;
|
| 121 |
color: white;
|
| 122 |
+
box-shadow: 0 10px 30px rgba(102, 126, 234, 0.5);
|
| 123 |
}
|
| 124 |
|
| 125 |
.header-section h1 {
|
| 126 |
font-size: 3rem !important;
|
| 127 |
font-weight: 700 !important;
|
| 128 |
margin-bottom: 0.5rem !important;
|
| 129 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
|
| 130 |
+
background: linear-gradient(135deg, #fff 0%, #a8edea 100%);
|
| 131 |
+
-webkit-background-clip: text;
|
| 132 |
+
-webkit-text-fill-color: transparent;
|
| 133 |
+
background-clip: text;
|
| 134 |
}
|
| 135 |
|
| 136 |
.header-section p {
|
| 137 |
font-size: 1.1rem !important;
|
| 138 |
opacity: 0.95;
|
| 139 |
margin-top: 0.5rem !important;
|
| 140 |
+
color: #e0e7ff !important;
|
| 141 |
}
|
| 142 |
|
| 143 |
.device-badge {
|
| 144 |
display: inline-block;
|
| 145 |
padding: 0.5rem 1.5rem;
|
| 146 |
+
background: rgba(255, 255, 255, 0.15);
|
| 147 |
backdrop-filter: blur(10px);
|
| 148 |
border-radius: 50px;
|
| 149 |
font-weight: 600;
|
| 150 |
font-size: 0.9rem;
|
| 151 |
margin-top: 1rem;
|
| 152 |
border: 2px solid rgba(255, 255, 255, 0.3);
|
| 153 |
+
color: #fff !important;
|
| 154 |
}
|
| 155 |
|
| 156 |
.info-card {
|
| 157 |
+
background: linear-gradient(135deg, #1e3a8a 0%, #4c1d95 100%) !important;
|
| 158 |
padding: 1.5rem;
|
| 159 |
border-radius: 16px;
|
| 160 |
margin: 1rem 0;
|
| 161 |
+
border-left: 4px solid #8b5cf6;
|
| 162 |
+
box-shadow: 0 4px 15px rgba(139, 92, 246, 0.3);
|
| 163 |
+
color: #e0e7ff !important;
|
| 164 |
}
|
| 165 |
|
| 166 |
.info-card h3 {
|
| 167 |
+
color: #c4b5fd !important;
|
| 168 |
font-weight: 600;
|
| 169 |
margin-bottom: 0.8rem;
|
| 170 |
}
|
|
|
|
| 178 |
padding: 0.4rem 0;
|
| 179 |
padding-left: 1.5rem;
|
| 180 |
position: relative;
|
| 181 |
+
color: #ddd6fe !important;
|
| 182 |
}
|
| 183 |
|
| 184 |
.info-card li:before {
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
.generate-btn {
|
| 191 |
+
background: linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%) !important;
|
| 192 |
border: none !important;
|
| 193 |
color: white !important;
|
| 194 |
font-weight: 600 !important;
|
| 195 |
font-size: 1.1rem !important;
|
| 196 |
padding: 1rem 2rem !important;
|
| 197 |
border-radius: 12px !important;
|
| 198 |
+
box-shadow: 0 6px 20px rgba(139, 92, 246, 0.5) !important;
|
| 199 |
transition: all 0.3s ease !important;
|
| 200 |
margin-top: 1rem !important;
|
| 201 |
}
|
| 202 |
|
| 203 |
.generate-btn:hover {
|
| 204 |
transform: translateY(-2px) !important;
|
| 205 |
+
box-shadow: 0 8px 25px rgba(139, 92, 246, 0.7) !important;
|
| 206 |
+
background: linear-gradient(135deg, #a78bfa 0%, #f472b6 100%) !important;
|
| 207 |
}
|
| 208 |
|
| 209 |
.download-btn {
|
| 210 |
+
background: linear-gradient(135deg, #06b6d4 0%, #3b82f6 100%) !important;
|
| 211 |
border: none !important;
|
| 212 |
color: white !important;
|
| 213 |
font-weight: 600 !important;
|
| 214 |
padding: 0.8rem 1.5rem !important;
|
| 215 |
border-radius: 12px !important;
|
| 216 |
+
box-shadow: 0 4px 15px rgba(6, 182, 212, 0.4) !important;
|
| 217 |
transition: all 0.3s ease !important;
|
| 218 |
}
|
| 219 |
|
| 220 |
.download-btn:hover {
|
| 221 |
transform: translateY(-2px) !important;
|
| 222 |
+
box-shadow: 0 6px 20px rgba(6, 182, 212, 0.6) !important;
|
| 223 |
+
background: linear-gradient(135deg, #22d3ee 0%, #60a5fa 100%) !important;
|
| 224 |
}
|
| 225 |
|
| 226 |
.textbox, .slider, .audio {
|
| 227 |
border-radius: 12px !important;
|
| 228 |
+
border: 2px solid #4c1d95 !important;
|
| 229 |
transition: all 0.3s ease !important;
|
| 230 |
+
background: rgba(30, 41, 59, 0.5) !important;
|
| 231 |
+
color: #e0e7ff !important;
|
| 232 |
}
|
| 233 |
|
| 234 |
.textbox:focus, .slider:focus {
|
| 235 |
+
border-color: #8b5cf6 !important;
|
| 236 |
+
box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.3) !important;
|
| 237 |
}
|
| 238 |
|
| 239 |
label {
|
| 240 |
font-weight: 600 !important;
|
| 241 |
+
color: #c4b5fd !important;
|
| 242 |
margin-bottom: 0.5rem !important;
|
| 243 |
}
|
| 244 |
|
| 245 |
.accordion {
|
| 246 |
border-radius: 12px !important;
|
| 247 |
+
background: rgba(30, 41, 59, 0.6) !important;
|
| 248 |
+
border: 2px solid #4c1d95 !important;
|
| 249 |
}
|
| 250 |
|
| 251 |
.footer-links {
|
| 252 |
text-align: center;
|
| 253 |
margin-top: 2rem;
|
| 254 |
padding-top: 2rem;
|
| 255 |
+
border-top: 2px solid #4c1d95;
|
| 256 |
}
|
| 257 |
|
| 258 |
.footer-links a {
|
| 259 |
+
color: #a78bfa !important;
|
| 260 |
text-decoration: none;
|
| 261 |
font-weight: 600;
|
| 262 |
margin: 0 1rem;
|
|
|
|
| 264 |
}
|
| 265 |
|
| 266 |
.footer-links a:hover {
|
| 267 |
+
color: #c4b5fd !important;
|
| 268 |
text-decoration: underline;
|
| 269 |
}
|
| 270 |
+
|
| 271 |
+
.footer-links p {
|
| 272 |
+
color: #c4b5fd !important;
|
| 273 |
+
}
|
| 274 |
"""
|
| 275 |
|
| 276 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
|
|
| 349 |
<li>Use proper grammar and contractions</li>
|
| 350 |
<li>Regenerate for different variations</li>
|
| 351 |
</ul>
|
| 352 |
+
<p style="margin-top: 1rem; padding-top: 1rem; border-top: 2px solid rgba(139, 92, 246, 0.3); font-weight: 600; color: {'#10b981' if DEVICE == 'cuda' else '#fbbf24'};">
|
| 353 |
{'⚡ GPU acceleration active - maximum performance!' if DEVICE == 'cuda' else '💻 Running on CPU - may be slower but still efficient'}
|
| 354 |
</p>
|
| 355 |
+
<p style="margin-top: 0.5rem; font-size: 0.85rem; color: #a78bfa;">
|
| 356 |
+
🔒 Usage data is logged for analytics
|
| 357 |
+
</p>
|
| 358 |
</div>
|
| 359 |
""")
|
| 360 |
|
|
|
|
| 363 |
<div class="footer-links">
|
| 364 |
<a href="https://github.com/ekwek1/soprano" target="_blank">📦 GitHub</a>
|
| 365 |
<a href="https://huggingface.co/ekwek/Soprano-80M" target="_blank">🤗 Model Weights</a>
|
| 366 |
+
<div style="margin-top: 1.5rem; padding-top: 1.5rem; border-top: 2px solid #4c1d95;">
|
| 367 |
+
<p style="color: #a78bfa; font-weight: 500;">Made with ❤️ by <a href="https://huggingface.co/Humair-025" target="_blank" style="color: #c4b5fd; text-decoration: none; font-weight: 700;">huggingface.co/Humair-025</a></p>
|
| 368 |
+
</div>
|
| 369 |
</div>
|
| 370 |
""")
|
| 371 |
|