Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,63 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from TTS.api import TTS
|
| 4 |
import noisereduce as nr
|
| 5 |
import soundfile as sf
|
| 6 |
-
import gc
|
| 7 |
|
| 8 |
# Memory optimize
|
| 9 |
gc.collect()
|
| 10 |
os.environ['COQUI_TOS_AGREED'] = '1'
|
| 11 |
|
| 12 |
-
print("β³ Starting
|
| 13 |
|
| 14 |
-
# Bina kisi auto-switch ke, seedha sabse stable engine load kar rahe hain
|
| 15 |
tts = TTS("tts_models/multilingual/multi-dataset/your_tts")
|
| 16 |
|
| 17 |
-
print("β
System
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def generate_api_voice(text, reference_audio):
|
| 20 |
if not text or not reference_audio:
|
| 21 |
-
return None, "Error: Script ya Voice missing hai
|
| 22 |
-
|
| 23 |
try:
|
| 24 |
-
# Audio se shor (noise) hatana
|
| 25 |
data, rate = sf.read(reference_audio)
|
| 26 |
if len(data.shape) > 1: data = data.mean(axis=1)
|
| 27 |
clean_data = nr.reduce_noise(y=data, sr=rate)
|
| 28 |
sf.write("clean_ref.wav", clean_data, rate)
|
| 29 |
|
| 30 |
output_file = "output_voice.wav"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
return None, f"β οΈ
|
| 38 |
|
| 39 |
-
# Ekdum saaf UI (No extra clutter)
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=generate_api_voice,
|
| 42 |
inputs=[
|
|
@@ -44,7 +65,7 @@ iface = gr.Interface(
|
|
| 44 |
gr.Audio(type="filepath", label="Voice Upload")
|
| 45 |
],
|
| 46 |
outputs=[
|
| 47 |
-
gr.Audio(label="Voice Output"),
|
| 48 |
gr.Textbox(label="API Status")
|
| 49 |
],
|
| 50 |
title="ποΈ VoiceForge AI - Stable Backend"
|
|
|
|
| 1 |
import os
|
| 2 |
+
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
|
| 9 |
import soundfile as sf
|
|
|
|
| 10 |
|
| 11 |
# Memory optimize
|
| 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 (HF 48hr sleep se pehle) ββ
|
| 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 |
+
restart_thread = threading.Thread(target=auto_restart, daemon=True)
|
| 29 |
+
restart_thread.start()
|
| 30 |
+
print("β±οΈ Auto-restart timer started (47 hrs)")
|
| 31 |
+
|
| 32 |
+
# ββ Keep-alive ping: har 25 min mein Space ko active rakhta hai ββ
|
| 33 |
+
def keep_alive():
|
| 34 |
+
while True:
|
| 35 |
+
time.sleep(1500) # 25 minutes
|
| 36 |
+
print("π Keep-alive ping β Space still running!")
|
| 37 |
+
|
| 38 |
+
ping_thread = threading.Thread(target=keep_alive, daemon=True)
|
| 39 |
+
ping_thread.start()
|
| 40 |
|
| 41 |
def generate_api_voice(text, reference_audio):
|
| 42 |
if not text or not reference_audio:
|
| 43 |
+
return None, "Error: Script ya Voice missing hai!"
|
|
|
|
| 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 |
+
return output_file, "β
Success!"
|
| 58 |
except Exception as e:
|
| 59 |
+
return None, f"β οΈ Error: {str(e)}"
|
| 60 |
|
|
|
|
| 61 |
iface = gr.Interface(
|
| 62 |
fn=generate_api_voice,
|
| 63 |
inputs=[
|
|
|
|
| 65 |
gr.Audio(type="filepath", label="Voice Upload")
|
| 66 |
],
|
| 67 |
outputs=[
|
| 68 |
+
gr.Audio(label="Voice Output"),
|
| 69 |
gr.Textbox(label="API Status")
|
| 70 |
],
|
| 71 |
title="ποΈ VoiceForge AI - Stable Backend"
|