"""POST /api/evaluate.dspy — 歌曲评估""" import json import os import tempfile from songrate.evaluator import evaluate_song # 获取参数 scene = params_kw.get('scene', 'pop') filepath = params_kw.get('filepath', '') # 如果没有 filepath,检查是否有上传文件 if not filepath: # 支持 multipart/form-data 上传 file_content = params_kw.get('file_content', '') filename = params_kw.get('filename', '') if file_content and filename: ext = os.path.splitext(filename)[1] or '.mp3' filepath = os.path.join(tempfile.gettempdir(), f'songrate_{os.getpid()}{ext}') with open(filepath, 'wb') as f: f.write(file_content if isinstance(file_content, bytes) else file_content.encode()) cleanup = True else: return json.dumps({"error": "缺少 filepath 或 file_content+filename 参数"}, ensure_ascii=False) else: cleanup = False try: result = evaluate_song(filepath, scene) return json.dumps(result, ensure_ascii=False) finally: if cleanup and os.path.exists(filepath): os.remove(filepath)