Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,33 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gc
|
| 3 |
-
import threading
|
| 4 |
-
import time
|
| 5 |
-
import sys
|
| 6 |
-
import requests
|
| 7 |
import gradio as gr
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Memory optimize
|
| 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 |
-
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
if not
|
| 41 |
-
return None, "Error: Script missing!"
|
| 42 |
-
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 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 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
|
| 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 |
-
#
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
gr.
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
title="ποΈ VoiceForge AI"
|
| 99 |
-
)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
iface.launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
print("β
Glowmation API Server Booting up! (Dev: Rahul Terpathi)")
|
| 7 |
|
| 8 |
+
def generate_image(prompt):
|
| 9 |
+
if not prompt: return None
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
# Hugging Face ke backend se unlimited call jaayegi (Koi ad-blocker nahi rok payega)
|
| 12 |
+
safe_prompt = prompt.replace(" ", "%20")
|
| 13 |
+
url = f"https://image.pollinations.ai/prompt/{safe_prompt}?width=1024&height=1024&nologo=true"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
response = requests.get(url)
|
| 16 |
+
image = Image.open(BytesIO(response.content))
|
| 17 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Clean Dark Mode API Interface
|
| 22 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 23 |
+
gr.Markdown("# βοΈ VoiceForge API Engine")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
inp = gr.Textbox(label="Input Prompt")
|
| 27 |
+
btn = gr.Button("Generate")
|
| 28 |
+
out = gr.Image(label="Output Image")
|
| 29 |
+
|
| 30 |
+
# Ye api_name="generate" hi tere frontend se connect hoga
|
| 31 |
+
btn.click(fn=generate_image, inputs=inp, outputs=out, api_name="generate")
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
demo.launch()
|
|
|