Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
-
|
| 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 "
|
| 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 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
print("β ERROR:", str(e))
|
| 81 |
-
return str(e), 500
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
for f in [raw_mp3, raw_wav]:
|
| 86 |
-
if os.path.exists(f):
|
| 87 |
-
os.remove(f)
|
| 88 |
|
| 89 |
-
|
| 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)
|