Namanrai commited on
Commit
76d84c7
Β·
verified Β·
1 Parent(s): ae6e9f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -16
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 (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=[
@@ -68,7 +95,8 @@ iface = gr.Interface(
68
  gr.Audio(label="Voice Output"),
69
  gr.Textbox(label="API Status")
70
  ],
71
- title="πŸŽ™οΈ VoiceForge AI - Stable Backend"
72
  )
73
 
74
- iface.launch()
 
 
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)