Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,25 @@
|
|
| 1 |
import io
|
| 2 |
-
import
|
| 3 |
-
import
|
|
|
|
| 4 |
from flask import Flask, request, send_file
|
| 5 |
-
from chatterbox.tts import ChatterboxTTS
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
@app.route('/tts', methods=['POST'])
|
| 15 |
def tts_api():
|
|
@@ -17,21 +27,33 @@ def tts_api():
|
|
| 17 |
if not text:
|
| 18 |
return "Error: 'text' parameter is missing", 400
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
return send_file(
|
| 29 |
-
|
| 30 |
mimetype='audio/wav',
|
| 31 |
as_attachment=True,
|
| 32 |
-
download_name='
|
| 33 |
)
|
| 34 |
|
| 35 |
@app.route('/')
|
| 36 |
def home():
|
| 37 |
-
return "🎙️
|
|
|
|
| 1 |
import io
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
from flask import Flask, request, send_file
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# हिंदी के लिए Piper मॉडल डाउनलोड करें (पहली बार चलने पर)
|
| 10 |
+
MODEL_URL = "https://huggingface.co/rhasspy/piper-voices/resolve/main/hi/hi_IN/harshit/low/hi_IN-harshit-low.onnx"
|
| 11 |
+
MODEL_JSON_URL = "https://huggingface.co/rhasspy/piper-voices/resolve/main/hi/hi_IN/harshit/low/hi_IN-harshit-low.onnx.json"
|
| 12 |
+
MODEL_PATH = "/tmp/hi_IN-harshit-low.onnx"
|
| 13 |
+
MODEL_JSON_PATH = "/tmp/hi_IN-harshit-low.onnx.json"
|
| 14 |
+
|
| 15 |
+
def download_model():
|
| 16 |
+
if not os.path.exists(MODEL_PATH):
|
| 17 |
+
print("⏳ मॉडल डाउनलोड हो रहा है...")
|
| 18 |
+
subprocess.run(["wget", "-q", "-O", MODEL_PATH, MODEL_URL])
|
| 19 |
+
subprocess.run(["wget", "-q", "-O", MODEL_JSON_PATH, MODEL_JSON_URL])
|
| 20 |
+
print("✅ मॉडल तैयार है!")
|
| 21 |
+
|
| 22 |
+
download_model()
|
| 23 |
|
| 24 |
@app.route('/tts', methods=['POST'])
|
| 25 |
def tts_api():
|
|
|
|
| 27 |
if not text:
|
| 28 |
return "Error: 'text' parameter is missing", 400
|
| 29 |
|
| 30 |
+
# Piper TTS को कमांड लाइन से चलाएं
|
| 31 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
| 32 |
+
output_file = f.name
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
cmd = [
|
| 36 |
+
"piper",
|
| 37 |
+
"-m", MODEL_PATH,
|
| 38 |
+
"-c", MODEL_JSON_PATH,
|
| 39 |
+
"--output_file", output_file
|
| 40 |
+
]
|
| 41 |
+
proc = subprocess.run(cmd, input=text.encode('utf-8'), capture_output=True)
|
| 42 |
+
if proc.returncode != 0:
|
| 43 |
+
return f"Piper error: {proc.stderr.decode()}", 500
|
| 44 |
+
|
| 45 |
+
with open(output_file, 'rb') as audio_file:
|
| 46 |
+
audio_data = audio_file.read()
|
| 47 |
+
finally:
|
| 48 |
+
os.unlink(output_file)
|
| 49 |
|
| 50 |
return send_file(
|
| 51 |
+
io.BytesIO(audio_data),
|
| 52 |
mimetype='audio/wav',
|
| 53 |
as_attachment=True,
|
| 54 |
+
download_name='output.wav'
|
| 55 |
)
|
| 56 |
|
| 57 |
@app.route('/')
|
| 58 |
def home():
|
| 59 |
+
return "🎙️ Piper TTS हिंदी API चल रही है!"
|