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