Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gc
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import sys
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
from TTS.api import TTS
|
| 8 |
import noisereduce as nr
|
|
@@ -12,52 +13,78 @@ import soundfile as sf
|
|
| 12 |
gc.collect()
|
| 13 |
os.environ['COQUI_TOS_AGREED'] = '1'
|
| 14 |
|
| 15 |
-
print("β³ Starting VoiceForge Backend...")
|
| 16 |
|
|
|
|
| 17 |
tts = TTS("tts_models/multilingual/multi-dataset/your_tts")
|
| 18 |
|
| 19 |
print("β
System Ready!")
|
| 20 |
|
| 21 |
-
# ββ Auto-restart: 47 hrs baad restart
|
| 22 |
def auto_restart():
|
| 23 |
-
# 47 hours = 169200 seconds
|
| 24 |
time.sleep(169200)
|
| 25 |
print("π Auto-restart: 47hrs complete β restarting now...")
|
| 26 |
os.execv(sys.executable, [sys.executable] + sys.argv)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
restart_thread.start()
|
| 30 |
-
print("β±οΈ Auto-restart timer started (47 hrs)")
|
| 31 |
|
| 32 |
-
# ββ Keep-alive ping:
|
| 33 |
def keep_alive():
|
| 34 |
while True:
|
| 35 |
-
time.sleep(1500)
|
| 36 |
print("π Keep-alive ping β Space still running!")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
ping_thread.start()
|
| 40 |
|
| 41 |
def generate_api_voice(text, reference_audio):
|
| 42 |
-
if not text
|
| 43 |
-
return None, "Error: Script
|
|
|
|
| 44 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
data, rate = sf.read(reference_audio)
|
| 46 |
if len(data.shape) > 1: data = data.mean(axis=1)
|
| 47 |
clean_data = nr.reduce_noise(y=data, sr=rate)
|
| 48 |
sf.write("clean_ref.wav", clean_data, rate)
|
| 49 |
|
| 50 |
output_file = "output_voice.wav"
|
|
|
|
|
|
|
| 51 |
tts.tts_to_file(
|
| 52 |
text=text,
|
| 53 |
speaker_wav="clean_ref.wav",
|
| 54 |
language="en",
|
| 55 |
file_path=output_file
|
| 56 |
)
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
|
|
|
| 61 |
iface = gr.Interface(
|
| 62 |
fn=generate_api_voice,
|
| 63 |
inputs=[
|
|
@@ -68,7 +95,8 @@ iface = gr.Interface(
|
|
| 68 |
gr.Audio(label="Voice Output"),
|
| 69 |
gr.Textbox(label="API Status")
|
| 70 |
],
|
| 71 |
-
title="ποΈ VoiceForge AI
|
| 72 |
)
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import sys
|
| 6 |
+
import requests
|
| 7 |
import gradio as gr
|
| 8 |
from TTS.api import TTS
|
| 9 |
import noisereduce as nr
|
|
|
|
| 13 |
gc.collect()
|
| 14 |
os.environ['COQUI_TOS_AGREED'] = '1'
|
| 15 |
|
| 16 |
+
print("β³ Starting VoiceForge AI Backend (Developer: Rahul Terpathi)...")
|
| 17 |
|
| 18 |
+
# Main Heavy Engine
|
| 19 |
tts = TTS("tts_models/multilingual/multi-dataset/your_tts")
|
| 20 |
|
| 21 |
print("β
System Ready!")
|
| 22 |
|
| 23 |
+
# ββ Auto-restart: 47 hrs baad restart ββ
|
| 24 |
def auto_restart():
|
|
|
|
| 25 |
time.sleep(169200)
|
| 26 |
print("π Auto-restart: 47hrs complete β restarting now...")
|
| 27 |
os.execv(sys.executable, [sys.executable] + sys.argv)
|
| 28 |
|
| 29 |
+
threading.Thread(target=auto_restart, daemon=True).start()
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# ββ Keep-alive ping: 25 min ββ
|
| 32 |
def keep_alive():
|
| 33 |
while True:
|
| 34 |
+
time.sleep(1500)
|
| 35 |
print("π Keep-alive ping β Space still running!")
|
| 36 |
|
| 37 |
+
threading.Thread(target=keep_alive, daemon=True).start()
|
|
|
|
| 38 |
|
| 39 |
def generate_api_voice(text, reference_audio):
|
| 40 |
+
if not text:
|
| 41 |
+
return None, "Error: Script missing!"
|
| 42 |
+
|
| 43 |
try:
|
| 44 |
+
if not reference_audio:
|
| 45 |
+
raise Exception("No reference audio provided for main engine")
|
| 46 |
+
|
| 47 |
+
# Audio clean karna
|
| 48 |
data, rate = sf.read(reference_audio)
|
| 49 |
if len(data.shape) > 1: data = data.mean(axis=1)
|
| 50 |
clean_data = nr.reduce_noise(y=data, sr=rate)
|
| 51 |
sf.write("clean_ref.wav", clean_data, rate)
|
| 52 |
|
| 53 |
output_file = "output_voice.wav"
|
| 54 |
+
|
| 55 |
+
# Main Voice Generation
|
| 56 |
tts.tts_to_file(
|
| 57 |
text=text,
|
| 58 |
speaker_wav="clean_ref.wav",
|
| 59 |
language="en",
|
| 60 |
file_path=output_file
|
| 61 |
)
|
| 62 |
+
|
| 63 |
+
# β
Process ke baad kachra saaf
|
| 64 |
+
gc.collect()
|
| 65 |
+
return output_file, "β
Success: Main Engine"
|
| 66 |
+
|
| 67 |
except Exception as e:
|
| 68 |
+
print(f"β οΈ Main Engine Overloaded. Auto-Switching... Error: {e}")
|
| 69 |
+
# π AUTO-SWITCH: Agar Your_TTS fail ho gaya (OOM ya error), toh halki API par switch karega
|
| 70 |
+
try:
|
| 71 |
+
gc.collect() # Pura RAM free karo
|
| 72 |
+
fallback_url = "https://api-inference.huggingface.co/models/facebook/mms-tts-eng"
|
| 73 |
+
resp = requests.post(fallback_url, json={"inputs": text})
|
| 74 |
+
|
| 75 |
+
if resp.status_code == 200:
|
| 76 |
+
with open("fallback_voice.wav", "wb") as f:
|
| 77 |
+
f.write(resp.content)
|
| 78 |
+
return "fallback_voice.wav", "β‘ Auto-Switched to Backup Engine!"
|
| 79 |
+
else:
|
| 80 |
+
raise Exception("Backup API Busy")
|
| 81 |
+
except Exception as backup_e:
|
| 82 |
+
# Agar dono fail ho jayein (Extreme Memory Crash), toh khud ko restart kar lo
|
| 83 |
+
print("π₯ Fatal Crash! Rebooting System...")
|
| 84 |
+
os.execv(sys.executable, [sys.executable] + sys.argv)
|
| 85 |
+
return None, "π Restarting Server..."
|
| 86 |
|
| 87 |
+
# ββ Clean & Simple UI ββ
|
| 88 |
iface = gr.Interface(
|
| 89 |
fn=generate_api_voice,
|
| 90 |
inputs=[
|
|
|
|
| 95 |
gr.Audio(label="Voice Output"),
|
| 96 |
gr.Textbox(label="API Status")
|
| 97 |
],
|
| 98 |
+
title="ποΈ VoiceForge AI"
|
| 99 |
)
|
| 100 |
|
| 101 |
+
# show_api=False se faltu clutter nahi aayega backend mein
|
| 102 |
+
iface.launch(show_api=False)
|