Singhp08 commited on
Commit
cb6aeb9
Β·
verified Β·
1 Parent(s): 85456b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -80
app.py CHANGED
@@ -1,94 +1,26 @@
1
- import os
2
- import gc
3
- import torch
4
  from flask import Flask, request, send_file
5
- from rvc_python.infer import RVCInference
6
  from gtts import gTTS
7
- from pydub import AudioSegment
8
 
9
  app = Flask(__name__)
10
 
11
- # Engine lazy load
12
- rvc_engine = None
13
-
14
- def get_engine():
15
- global rvc_engine
16
- if rvc_engine is None:
17
- print("πŸ“₯ Loading RVC Engine (CPU Mode)...")
18
- rvc_engine = RVCInference(device="cpu")
19
- return rvc_engine
20
-
21
- @app.route('/')
22
  def home():
23
- return "βœ… RVC Engine is Running!", 200
24
-
25
- @app.route('/convert', methods=['POST'])
26
- def convert():
27
- raw_mp3 = "input.mp3"
28
- raw_wav = "input.wav"
29
- out_file = "output.wav"
30
-
31
- try:
32
- data = request.json
33
- text = data.get('text', '')
34
- model_name = data.get('model', 'elvish')
35
-
36
- if not text:
37
- return "❌ No text provided", 400
38
-
39
- print("πŸ“₯ Text:", text)
40
- print("πŸ“¦ Model:", model_name)
41
-
42
- # πŸ”Š STEP 1: gTTS (MP3)
43
- tts = gTTS(text=text, lang='hi')
44
- tts.save(raw_mp3)
45
-
46
- # πŸ” STEP 2: MP3 β†’ WAV (RVC compatible)
47
- sound = AudioSegment.from_mp3(raw_mp3)
48
- sound = sound.set_channels(1).set_frame_rate(16000)
49
- sound.export(raw_wav, format="wav")
50
-
51
- # βš™οΈ STEP 3: Load RVC Engine
52
- engine = get_engine()
53
-
54
- # πŸ“ Model paths
55
- m_path = f"./models/{model_name}.pth"
56
- i_path = f"./models/{model_name}.index"
57
-
58
- print("πŸ“ Model exists:", os.path.exists(m_path))
59
- print("πŸ“ Index exists:", os.path.exists(i_path))
60
-
61
- if not os.path.exists(m_path):
62
- return "❌ Model file missing", 404
63
-
64
- idx = i_path if os.path.exists(i_path) else None
65
-
66
- # 🎀 STEP 4: Voice Convert
67
- print("πŸš€ Starting RVC inference...")
68
- engine.infer(
69
- input_path=raw_wav,
70
- model_path=m_path,
71
- index_path=idx,
72
- output_path=out_file
73
- )
74
 
75
- print("βœ… Conversion done!")
 
 
76
 
77
- return send_file(out_file, mimetype="audio/wav")
 
78
 
79
- except Exception as e:
80
- print("❌ ERROR:", str(e))
81
- return str(e), 500
82
 
83
- finally:
84
- # 🧹 Cleanup
85
- for f in [raw_mp3, raw_wav]:
86
- if os.path.exists(f):
87
- os.remove(f)
88
 
89
- gc.collect()
90
- if torch.cuda.is_available():
91
- torch.cuda.empty_cache()
92
 
93
  if __name__ == "__main__":
94
  app.run(host="0.0.0.0", port=7860)
 
 
 
 
1
  from flask import Flask, request, send_file
 
2
  from gtts import gTTS
3
+ import os
4
 
5
  app = Flask(__name__)
6
 
7
+ @app.route("/")
 
 
 
 
 
 
 
 
 
 
8
  def home():
9
+ return "TTS API Running"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ @app.route("/tts", methods=["POST"])
12
+ def tts():
13
+ text = request.json.get("text")
14
 
15
+ if not text:
16
+ return "No text", 400
17
 
18
+ file = "out.mp3"
 
 
19
 
20
+ tts = gTTS(text=text, lang='hi')
21
+ tts.save(file)
 
 
 
22
 
23
+ return send_file(file, mimetype="audio/mpeg")
 
 
24
 
25
  if __name__ == "__main__":
26
  app.run(host="0.0.0.0", port=7860)