Singhp08 commited on
Commit
c25075b
·
verified ·
1 Parent(s): 300822f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -1,15 +1,25 @@
1
  import io
2
- import torch
3
- import soundfile as sf
 
4
  from flask import Flask, request, send_file
5
- from chatterbox.tts import ChatterboxTTS
6
 
7
  app = Flask(__name__)
8
 
9
- print("⏳ Chatterbox मॉडल लोड हो रहा है (पहली बार थोड़ा समय लगेगा)...")
10
- # CPU पर चलाने के लिए device="cpu" करें
11
- model = ChatterboxTTS.from_pretrained(device="cpu")
12
- print("✅ Chatterbox आपकी सेवा में तैयार है!")
 
 
 
 
 
 
 
 
 
 
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
- # ऑडियो को numpy array के रूप में जनरेट करें
21
- audio_array = model.generate(text)
22
-
23
- # ऑडियो को बफर में सेव करें
24
- audio_buffer = io.BytesIO()
25
- sf.write(audio_buffer, audio_array, model.sr, format='WAV')
26
- audio_buffer.seek(0)
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  return send_file(
29
- audio_buffer,
30
  mimetype='audio/wav',
31
  as_attachment=True,
32
- download_name='chatterbox_output.wav'
33
  )
34
 
35
  @app.route('/')
36
  def home():
37
- return "🎙️ Chatterbox TTS हिंदी API चल रही है!"
 
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 चल रही है!"