Coco-18 commited on
Commit
fe51424
·
verified ·
1 Parent(s): 8cb6f0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
 
4
  # Set ALL cache directories before importing any model libraries
@@ -39,15 +38,22 @@ for lang, path in MODELS.items():
39
  print(f"{lang.capitalize()} model loaded successfully!")
40
  except Exception as e:
41
  print(f"Error loading {lang} model: {str(e)}")
 
 
42
 
43
  SAMPLE_RATE = 16000 # Default sample rate
44
  OUTPUT_DIR = "/tmp/" # Use /tmp for Hugging Face Spaces (limited storage)
45
 
 
 
 
 
 
46
  @app.route("/tts", methods=["POST"])
47
  def generate_tts():
48
  """ API endpoint to generate speech based on the selected language. """
49
  data = request.get_json()
50
- text_input = data.get("text", "")
51
  language = data.get("language", "kapampangan").lower()
52
 
53
  if language not in MODELS:
@@ -56,29 +62,36 @@ def generate_tts():
56
  if not text_input:
57
  return jsonify({"error": "No text provided"}), 400
58
 
 
 
 
59
  print(f"Generating speech for: '{text_input}' in {language}")
60
 
61
- # Select the correct model and processor
62
- model = loaded_models[language]
63
- processor = loaded_processors[language]
 
64
 
65
- # Tokenize input text
66
- inputs = processor(text_input, return_tensors="pt")
67
 
68
- # Generate audio
69
- with torch.no_grad():
70
- output = model.generate(**inputs)
71
 
72
- waveform = output.cpu().numpy().flatten()
73
 
74
- # Save as WAV file
75
- output_filename = os.path.join(OUTPUT_DIR, f"{language}_output.wav")
76
- sf.write(output_filename, waveform, SAMPLE_RATE)
77
 
78
- return jsonify({
79
- "message": "TTS audio generated",
80
- "file_url": f"/static/{language}_output.wav"
81
- })
 
 
 
82
 
83
  @app.route("/static/<filename>")
84
  def serve_audio(filename):
@@ -90,3 +103,4 @@ def serve_audio(filename):
90
 
91
  if __name__ == "__main__":
92
  app.run(host="0.0.0.0", port=7860, debug=True)
 
 
 
1
  import os
2
 
3
  # Set ALL cache directories before importing any model libraries
 
38
  print(f"{lang.capitalize()} model loaded successfully!")
39
  except Exception as e:
40
  print(f"Error loading {lang} model: {str(e)}")
41
+ loaded_models[lang] = None # Mark as unavailable
42
+ loaded_processors[lang] = None
43
 
44
  SAMPLE_RATE = 16000 # Default sample rate
45
  OUTPUT_DIR = "/tmp/" # Use /tmp for Hugging Face Spaces (limited storage)
46
 
47
+ @app.route("/", methods=["GET"])
48
+ def home():
49
+ """ Root route to check if the server is running """
50
+ return jsonify({"message": "TTS API is running. Use /tts to generate speech."})
51
+
52
  @app.route("/tts", methods=["POST"])
53
  def generate_tts():
54
  """ API endpoint to generate speech based on the selected language. """
55
  data = request.get_json()
56
+ text_input = data.get("text", "").strip()
57
  language = data.get("language", "kapampangan").lower()
58
 
59
  if language not in MODELS:
 
62
  if not text_input:
63
  return jsonify({"error": "No text provided"}), 400
64
 
65
+ if loaded_models[language] is None:
66
+ return jsonify({"error": f"Model for {language} failed to load"}), 500
67
+
68
  print(f"Generating speech for: '{text_input}' in {language}")
69
 
70
+ try:
71
+ # Select the correct model and processor
72
+ model = loaded_models[language]
73
+ processor = loaded_processors[language]
74
 
75
+ # Tokenize input text
76
+ inputs = processor(text_input, return_tensors="pt")
77
 
78
+ # Generate audio
79
+ with torch.no_grad():
80
+ output = model.generate(**inputs)
81
 
82
+ waveform = output.cpu().numpy().flatten()
83
 
84
+ # Save as WAV file
85
+ output_filename = os.path.join(OUTPUT_DIR, f"{language}_output.wav")
86
+ sf.write(output_filename, waveform, SAMPLE_RATE)
87
 
88
+ return jsonify({
89
+ "message": "TTS audio generated",
90
+ "file_url": f"/static/{language}_output.wav"
91
+ })
92
+ except Exception as e:
93
+ print(f"Error generating TTS: {str(e)}")
94
+ return jsonify({"error": "Internal server error"}), 500
95
 
96
  @app.route("/static/<filename>")
97
  def serve_audio(filename):
 
103
 
104
  if __name__ == "__main__":
105
  app.run(host="0.0.0.0", port=7860, debug=True)
106
+