3v324v23 commited on
Commit
f3f2da6
·
1 Parent(s): fab1107

Correctly add audio files with Git LFS

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +22 -0
  3. app.py +64 -0
  4. requirements.txt +5 -0
  5. voices/-1 +1 -0
  6. voices/liang.wav +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.wav filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile
2
+ # 使用官方的 Python 基础镜像
3
+ FROM python:3.11-slim
4
+
5
+ # 将工作目录设置为 /app
6
+ WORKDIR /app
7
+
8
+ # 将依赖文件复制到镜像中
9
+ COPY requirements.txt .
10
+
11
+ # 安装依赖
12
+ RUN pip install --no-cache-dir -r requirements.txt
13
+
14
+ # 将你项目中的所有文件(app.py, voices/ 文件夹等)复制到镜像中
15
+ COPY . .
16
+
17
+ # 暴露端口 7860 (Hugging Face Spaces 的标准端口)
18
+ EXPOSE 7860
19
+
20
+ # 运行 Gunicorn 服务器的命令
21
+ # 它会启动 1 个工作进程来运行你的 app.py 中的 'app' 对象
22
+ CMD ["gunicorn", "--workers", "1", "--bind", "0.0.0.0:7860", "app:app"]
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import shutil
4
+ from flask import Flask, request, jsonify, send_file
5
+ from gradio_client import Client, file as gradio_file
6
+
7
+ app = Flask(__name__)
8
+
9
+ VOICES_DIR = "voices"
10
+ if not os.path.exists(VOICES_DIR):
11
+ os.makedirs(VOICES_DIR)
12
+
13
+ SPACE_URL = "https://indexteam-indextts-2-demo.hf.space/"
14
+ gradio_client = None
15
+
16
+ try:
17
+ print("正在连接到 Gradio 服务...")
18
+ gradio_client = Client(SPACE_URL)
19
+ print("Gradio 客户端已准备就绪。")
20
+ except Exception as e:
21
+ print(f"错误:无法连接到 Gradio 服务: {e}")
22
+
23
+ def process_tts_request(text: str, voice_reference_path: str) -> str:
24
+ params = [
25
+ 'Same as the voice reference', gradio_file(voice_reference_path), text,
26
+ None, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
27
+ "", False, 120, True, 0.8, 30, 0.8, 0.0, 3, 10.0, 1500
28
+ ]
29
+ result = gradio_client.predict(*params, api_name="/gen_single")
30
+ if isinstance(result, dict) and 'value' in result:
31
+ temp_audio_path = result.get('value')
32
+ if temp_audio_path and os.path.exists(temp_audio_path):
33
+ return temp_audio_path
34
+ raise ValueError(f"从 Gradio API 返回了预料之外的格式: {result}")
35
+
36
+ @app.route('/v1/audio/speech', methods=['POST'])
37
+ def openai_style_tts_endpoint():
38
+ if gradio_client is None:
39
+ return jsonify({"error": "服务未就绪"}), 503
40
+ try:
41
+ data = request.get_json()
42
+ input_text, voice_filename = data['input'], data['voice']
43
+ except Exception:
44
+ return jsonify({"error": "无效请求格式"}), 400
45
+
46
+ voice_path = os.path.join(VOICES_DIR, voice_filename)
47
+ if not os.path.exists(voice_path):
48
+ return jsonify({"error": f"Voice '{voice_filename}' 不存在。"}), 404
49
+
50
+ temp_file = None
51
+ try:
52
+ print(f"收到请求: input='{input_text[:30]}...', voice='{voice_filename}'")
53
+ temp_file = process_tts_request(input_text, voice_path)
54
+ return send_file(temp_file, mimetype="audio/wav", as_attachment=True, download_name="speech.wav")
55
+ except Exception as e:
56
+ print(f"处理请求时出错: {e}")
57
+ return jsonify({"error": {"message": "内部服务器错误", "details": str(e)}}), 500
58
+ finally:
59
+ if temp_file and os.path.exists(temp_file):
60
+ os.remove(temp_file)
61
+
62
+ # 这段代码是为了在本地测试,部署到Hugging Face时 Gunicorn 会直接调用 app 对象
63
+ if __name__ == '__main__':
64
+ app.run(host='0.0.0.0', port=5000)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # requirements.txt
2
+ Flask
3
+ gradio_client
4
+ requests
5
+ gunicorn # 我们用它来在服务器上运行 Flask
voices/-1 ADDED
@@ -0,0 +1 @@
 
 
1
+ voices
voices/liang.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b373a88f49f2d0d8d9670956d83e155f649d83c8b0ab4b8bcd6ed934b8806c1
3
+ size 395564