ChandimaPrabath commited on
Commit
d690791
1 Parent(s): fad2e64
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -14,10 +14,17 @@ CORS(app)
14
  logging.basicConfig(level=logging.DEBUG)
15
  logger = logging.getLogger(__name__)
16
 
17
- def synthesize_speech(text, sentence_silence, length_scale):
 
 
 
 
 
 
 
18
  logger.debug("Downloading model and config files...")
19
- model_path = hf_hub_download(repo_id="csukuangfj/vits-piper-en_US-lessac-medium", filename="en_US-lessac-medium.onnx")
20
- config_path = hf_hub_download(repo_id="csukuangfj/vits-piper-en_US-lessac-medium", filename="en_US-lessac-medium.onnx.json")
21
 
22
  logger.debug("Loading PiperVoice model...")
23
  voice = PiperVoice.load(model_path, config_path)
@@ -53,11 +60,15 @@ def index():
53
  <body>
54
  <h1>TTS Server is Running</h1>
55
  <p>Use the <code>/tts</code> endpoint to synthesize speech.</p>
56
- <p>Send a POST request with JSON data containing the <code>text</code>, <code>sentence_silence</code>, and <code>length_scale</code> parameters.</p>
57
  </body>
58
  </html>
59
  '''
60
 
 
 
 
 
61
  @app.route('/tts', methods=['POST'])
62
  def tts():
63
  data = request.json
@@ -65,17 +76,28 @@ def tts():
65
  logger.error("No data received in request.")
66
  return jsonify({"error": "No data provided"}), 400
67
 
 
68
  text = data.get('text', '')
69
  sentence_silence = float(data.get('sentence_silence', 0.1))
70
  length_scale = float(data.get('length_scale', 1.0))
71
 
 
 
 
 
72
  if not text:
73
  logger.error("No text provided in request.")
74
  return jsonify({"error": "Text parameter is required"}), 400
75
 
76
- logger.info(f"Received request: text={text}, sentence_silence={sentence_silence}, length_scale={length_scale}")
 
 
 
 
 
 
77
  try:
78
- audio_buffer = synthesize_speech(text, sentence_silence, length_scale)
79
  except Exception as e:
80
  logger.exception("Error during speech synthesis.")
81
  return jsonify({"error": str(e)}), 500
 
14
  logging.basicConfig(level=logging.DEBUG)
15
  logger = logging.getLogger(__name__)
16
 
17
+ # Available models list
18
+ available_models = [
19
+ {"repo_id": "csukuangfj/vits-piper-en_US-lessac-medium", "filename": "en_US-lessac-medium.onnx"},
20
+ {"repo_id": "csukuangfj/vits-piper-en_US-hfc_female-medium", "filename": "en_US-hfc_female-medium.onnx"},
21
+ {"repo_id": "csukuangfj/vits-piper-en_GB-southern_english_female-medium", "filename": "en_GB-southern_english_female-medium.onnx"}
22
+ ]
23
+
24
+ def synthesize_speech(repo_id, model_filename, text, sentence_silence, length_scale):
25
  logger.debug("Downloading model and config files...")
26
+ model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
27
+ config_path = hf_hub_download(repo_id=repo_id, filename=f"{model_filename}.json")
28
 
29
  logger.debug("Loading PiperVoice model...")
30
  voice = PiperVoice.load(model_path, config_path)
 
60
  <body>
61
  <h1>TTS Server is Running</h1>
62
  <p>Use the <code>/tts</code> endpoint to synthesize speech.</p>
63
+ <p>Send a POST request with JSON data containing the <code>model</code>, <code>text</code>, <code>sentence_silence</code>, and <code>length_scale</code> parameters.</p>
64
  </body>
65
  </html>
66
  '''
67
 
68
+ @app.route('/models')
69
+ def models():
70
+ return jsonify(available_models)
71
+
72
  @app.route('/tts', methods=['POST'])
73
  def tts():
74
  data = request.json
 
76
  logger.error("No data received in request.")
77
  return jsonify({"error": "No data provided"}), 400
78
 
79
+ model = data.get('model', '')
80
  text = data.get('text', '')
81
  sentence_silence = float(data.get('sentence_silence', 0.1))
82
  length_scale = float(data.get('length_scale', 1.0))
83
 
84
+ if not model:
85
+ logger.error("No model provided in request.")
86
+ return jsonify({"error": "Model parameter is required"}), 400
87
+
88
  if not text:
89
  logger.error("No text provided in request.")
90
  return jsonify({"error": "Text parameter is required"}), 400
91
 
92
+ # Find the model in the available models list
93
+ selected_model = next((m for m in available_models if m["filename"] == model), None)
94
+ if not selected_model:
95
+ logger.error(f"Model {model} not found.")
96
+ return jsonify({"error": f"Model {model} not found"}), 404
97
+
98
+ logger.info(f"Received request: model={model}, text={text}, sentence_silence={sentence_silence}, length_scale={length_scale}")
99
  try:
100
+ audio_buffer = synthesize_speech(selected_model['repo_id'], selected_model['filename'], text, sentence_silence, length_scale)
101
  except Exception as e:
102
  logger.exception("Error during speech synthesis.")
103
  return jsonify({"error": str(e)}), 500