Download app.py from bluece/ttkkk: direct link, hf CLI and curl.
- Browser
- Download file 5.3 kB
-
https://huggingface.co/spaces/bluece/ttkkk/resolve/main/app.py
- Command line
-
hf download hf://spaces/bluece/ttkkk/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bluece/ttkkk/resolve/main/app.py
5.3 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| CosyVoice WebSocket 服务器的 Gradio 界面 | |
| 提供 WebSocket 服务器的 Web 界面,支持音色克隆和文本转语音功能 | |
| """ | |
| import os | |
| import sys | |
| import gradio as gr | |
| import subprocess | |
| import threading | |
| import time | |
| import logging | |
| from pathlib import Path | |
| # 配置日志 | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger("cosyvoice-app") | |
| # WebSocket 服务器进程 | |
| websocket_server_process = None | |
| def start_websocket_server(): | |
| """启动 WebSocket 服务器""" | |
| global websocket_server_process | |
| # 如果已经有进程在运行,先停止 | |
| if websocket_server_process is not None: | |
| try: | |
| websocket_server_process.terminate() | |
| websocket_server_process.wait(timeout=5) | |
| except Exception as e: | |
| logger.error(f"停止现有服务器进程时出错: {e}") | |
| # 启动新的 WebSocket 服务器进程 | |
| try: | |
| script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tts_websocket_server.py") | |
| websocket_server_process = subprocess.Popen([sys.executable, script_path]) | |
| logger.info("WebSocket 服务器已启动") | |
| return "WebSocket 服务器已启动,监听在 ws://0.0.0.0:7860/ws" | |
| except Exception as e: | |
| logger.error(f"启动 WebSocket 服务器时出错: {e}") | |
| return f"启动 WebSocket 服务器时出错: {e}" | |
| def stop_websocket_server(): | |
| """停止 WebSocket 服务器""" | |
| global websocket_server_process | |
| if websocket_server_process is not None: | |
| try: | |
| websocket_server_process.terminate() | |
| websocket_server_process.wait(timeout=5) | |
| websocket_server_process = None | |
| logger.info("WebSocket 服务器已停止") | |
| return "WebSocket 服务器已停止" | |
| except Exception as e: | |
| logger.error(f"停止 WebSocket 服务器时出错: {e}") | |
| return f"停止 WebSocket 服务器时出错: {e}" | |
| else: | |
| return "WebSocket 服务器未运行" | |
| def create_gradio_interface(): | |
| """创建 Gradio 界面""" | |
| with gr.Blocks(title="CosyVoice WebSocket 服务") as demo: | |
| gr.Markdown(""" | |
| # CosyVoice WebSocket 服务 | |
| 这是一个基于 CosyVoice 的 WebSocket 服务,提供文本转语音和音色克隆功能。 | |
| ## 功能特点 | |
| - **文本转语音**: 将文本转换为自然流畅的语音 | |
| - **音色克隆**: 上传参考音频,克隆特定说话人的声音 | |
| - **WebSocket API**: 提供 WebSocket 接口,方便集成到其他应用 | |
| ## 使用方法 | |
| 1. 点击下面的"启动服务器"按钮 | |
| 2. 使用 WebSocket 客户端连接到服务(默认地址:`wss://bluece-ttkkk.hf.space/ws`) | |
| 3. 使用下面的测试客户端进行测试 | |
| """) | |
| with gr.Row(): | |
| start_btn = gr.Button("启动服务器") | |
| stop_btn = gr.Button("停止服务器") | |
| status_output = gr.Textbox(label="服务器状态", value="服务器未启动") | |
| gr.Markdown(""" | |
| ## WebSocket API 说明 | |
| ### 1. 获取可用音色列表 | |
| ```json | |
| { | |
| "action": "list_voices" | |
| } | |
| ``` | |
| ### 2. 文本转语音 | |
| ```json | |
| { | |
| "action": "tts", | |
| "text": "要转换的文本", | |
| "voice_id": "voice_12345678" | |
| } | |
| ``` | |
| ### 3. 保存新音色 | |
| ```json | |
| { | |
| "action": "save_voice", | |
| "audio_data": "base64编码的音频数据", | |
| "reference_text": "参考音频中说的文字", | |
| "voice_name": "音色名称" | |
| } | |
| ``` | |
| ## 测试客户端 | |
| 您可以使用下面的测试客户端来测试 WebSocket 服务: | |
| """) | |
| with gr.Row(): | |
| client_link = gr.HTML(""" | |
| <div style="text-align: center; margin: 20px 0;"> | |
| <a href="/client.html" target="_blank" style="display: inline-block; padding: 10px 20px; background-color: #3498db; color: white; text-decoration: none; border-radius: 4px; font-weight: bold;"> | |
| 打开测试客户端 | |
| </a> | |
| </div> | |
| """) | |
| # 设置按钮事件 | |
| start_btn.click(start_websocket_server, inputs=[], outputs=[status_output]) | |
| stop_btn.click(stop_websocket_server, inputs=[], outputs=[status_output]) | |
| # 在启动时自动启动服务器 | |
| demo.load(start_websocket_server, inputs=[], outputs=[status_output]) | |
| return demo | |
| # 创建 Gradio 界面 | |
| demo = create_gradio_interface() | |
| # 添加静态文件 | |
| static_dir = os.path.dirname(os.path.abspath(__file__)) | |
| demo.launch(server_name="0.0.0.0", share=False, server_port=7860, debug=True, favicon_path=None, root_path="", allowed_paths=[static_dir]) | |
| # 如果是直接运行此脚本 | |
| if __name__ == "__main__": | |
| # 确保服务器在程序退出时停止 | |
| try: | |
| while True: | |
| time.sleep(1) | |
| except KeyboardInterrupt: | |
| stop_websocket_server() | |
| sys.exit(0) |