NamanRaii commited on
Commit
87e8563
Β·
verified Β·
1 Parent(s): 76d84c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -94
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
- from TTS.api import TTS
9
- import noisereduce as nr
10
- import soundfile as sf
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
- 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=[
91
- gr.Textbox(label="Script"),
92
- gr.Audio(type="filepath", label="Voice Upload")
93
- ],
94
- outputs=[
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)
 
 
 
 
 
 
 
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()