# app.py import os import shutil from flask import Flask, request, jsonify, send_file from gradio_client import Client, file as gradio_file app = Flask(__name__) VOICES_DIR = "voices" if not os.path.exists(VOICES_DIR): os.makedirs(VOICES_DIR) SPACE_URL = "https://indexteam-indextts-2-demo.hf.space/" gradio_client = None try: print("正在连接到 Gradio 服务...") gradio_client = Client(SPACE_URL) print("Gradio 客户端已准备就绪。") except Exception as e: print(f"错误:无法连接到 Gradio 服务: {e}") def process_tts_request(text: str, voice_reference_path: str) -> str: params = [ 'Same as the voice reference', gradio_file(voice_reference_path), text, None, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, "", False, 120, True, 0.8, 30, 0.8, 0.0, 3, 10.0, 1500 ] result = gradio_client.predict(*params, api_name="/gen_single") if isinstance(result, dict) and 'value' in result: temp_audio_path = result.get('value') if temp_audio_path and os.path.exists(temp_audio_path): return temp_audio_path raise ValueError(f"从 Gradio API 返回了预料之外的格式: {result}") @app.route('/v1/audio/speech', methods=['POST']) def openai_style_tts_endpoint(): if gradio_client is None: return jsonify({"error": "服务未就绪"}), 503 try: data = request.get_json() input_text, voice_filename = data['input'], data['voice'] except Exception: return jsonify({"error": "无效请求格式"}), 400 voice_path = os.path.join(VOICES_DIR, voice_filename) if not os.path.exists(voice_path): return jsonify({"error": f"Voice '{voice_filename}' 不存在。"}), 404 temp_file = None try: print(f"收到请求: input='{input_text[:30]}...', voice='{voice_filename}'") temp_file = process_tts_request(input_text, voice_path) return send_file(temp_file, mimetype="audio/wav", as_attachment=True, download_name="speech.wav") except Exception as e: print(f"处理请求时出错: {e}") return jsonify({"error": {"message": "内部服务器错误", "details": str(e)}}), 500 finally: if temp_file and os.path.exists(temp_file): os.remove(temp_file) # 这段代码是为了在本地测试,部署到Hugging Face时 Gunicorn 会直接调用 app 对象 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)