python+html调用阿里cosyvoice音色库文本设计添加预览试听管理删除代码
代码语言:python
所属分类:其他
代码描述:python+html调用阿里cosyvoice音色库文本设计添加预览试听管理删除代码,本地文本描述生成音色并试听,还能删除管理。
代码标签: python html 调用 阿里 cosyvoice 音色库 文本 设计 添加 预览 试听 管理
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
import os
import base64
import requests
from flask import Flask, request, jsonify, render_template_string, send_from_directory
app = Flask(__name__)
API_KEY ="sk-"
if not API_KEY:
raise ValueError("❌ 未找到环境变量 DASHSCOPE_API_KEY,请先配置!")
BASE_URL = "https://dashscope.aliyuncs.com/api/v1/services/audio/tts/customization"
AUDIO_CACHE_DIR = "audio_cache"
# 确保音频缓存目录存在
if not os.path.exists(AUDIO_CACHE_DIR):
os.makedirs(AUDIO_CACHE_DIR)
def call_dashscope_api(model_type, input_data, parameters=None, timeout=30):
"""统一封装 DashScope API 调用"""
model_name = "voice-enrollment" if model_type == 'cosyvoice' else "qwen-voice-design"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {"model": model_name, "input": input_data}
if parameters:
payload["parameters"] = parameters
try:
resp = requests.post(BASE_URL, headers=headers, json=payload, timeout=timeout)
return resp.json(), resp.status_code
except requests.exceptions.Timeout:
return {"code": "Timeout", "message": "请求超时"}, 504
except Exception as e:
return {"code": "NetworkError", "message": str(e)}, 500
@app.route('/')
def index():
return render_template_string(HTML_TEMPLATE)
@app.route('/api/voices', methods=['GET'])
def get_voices():
model_type = request.args.get('model_type', 'cosyvoice')
page = int(request.args.get('page', 0))
prefix = request.args.get('prefix', '')
action = "list_voice" if model_type == 'cosyvoice' else "list"
input_data = {"action": action, "page_index": page, "page_size": 10}
if prefix and model_type == 'cosyvoice':
input_data["prefix"] = prefix
data, status = call_dashscope_api(model_type, input_data)
return jsonify(data), status
@app.route('/api/voice/detail', methods=['GET'])
def get_detail():
model_type = request.args.get('model_type', 'cosyvoice')
identifier = request.args.get('identifier', '')
action = "query_voice" if model_type == 'cosyvoice' else "query"
key = "voice_id" if model_type == 'cosyvoice' else "voice"
input_data = {"action": action, key: identifier}
data, status = call_dashscope_api(model_type, input_data)
return jsonify(data), status
@app.route('/api/voice/delete', methods=['POST'])
def delete_voice():
data = request.json
model_type = data.get('model_type', 'cosyvoice')
identifier = data.get('identifier', '')
action = "delete_voice" if model_type == 'cosyvoice' else "delete"
key = "voice_id" if model_type == 'cosyvoice' else "voice"
input_data = {"action": action, key: identifier}
data, status = call_dashscope_api(model_type, input_data)
# 删除成功时,同时清理本地缓存的音频文件
if status == 200 and "code" not in data:
for ext in ['wav', 'mp3', 'pcm', 'aac']:
filepath = os.path.join(AUDIO_CACHE_DIR, f"{identifier}.{ext}")
if os.path.exists(filepath):
os.remove(filepath)
return jsonify(data), status
@app.route('/api/voice/create', methods=['POST'])
def create_voice():
"""创建音色并保存预览音频到本地"""
data = request.json
voice_prompt = data.get('voice_prompt')
preview_text = data.get('preview_text')
if not voice_prompt or not preview_text:
return jsonify({"code": "InvalidParams", "message": "声音描述和预览文本不能为空&.........完整代码请登录后点击上方下载按钮下载查看














网友评论0