diff --git a/dsLightRag/Routes/SunoRoute.py b/dsLightRag/Routes/SunoRoute.py index 583b580f..64dde133 100644 --- a/dsLightRag/Routes/SunoRoute.py +++ b/dsLightRag/Routes/SunoRoute.py @@ -3,6 +3,8 @@ import json import time import datetime import requests +import uuid # 新增:导入uuid模块 +import os # 新增:导入os模块 from typing import Optional import fastapi @@ -201,12 +203,53 @@ async def check_task_status(task_id: str = Query(..., description="音乐生成 task_info["audio_url"] = audio_url logger.info("音乐生成已完成!") logger.info(f"音频URL: {audio_url}") + + # 新增:下载音频文件并上传到OBS + try: + # 使用UUID生成唯一文件名 + unique_id = uuid.uuid4() + object_key = f"HuangHai/JiMeng/{unique_id}.mp3" + + # 临时文件保存路径 + temp_file_path = os.path.join(os.path.dirname(__file__), f"{unique_id}.mp3") + + # 下载URL内容到临时文件 + logger.info(f"开始下载URL内容: {audio_url}") + with requests.get(audio_url, stream=True, timeout=3600) as r: + r.raise_for_status() + with open(temp_file_path, 'wb') as f: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + logger.info(f"URL内容下载完成,保存至: {temp_file_path}") + + # 上传文件到OBS + from Util.ObsUtil import ObsUploader # 导入ObsUploader + obs_uploader = ObsUploader() + logger.info(f"开始上传文件到OBS: {temp_file_path}\n对象键: {object_key}") + + # 正确处理元组返回值 + success, response_info = obs_uploader.upload_file( + file_path=temp_file_path, + object_key=object_key + ) + + if success: + # 构造OBS URL + from Config.Config import OBS_SERVER, OBS_BUCKET + obs_url = f"https://{OBS_BUCKET}.{OBS_SERVER}/{object_key}" + logger.info(f"文件上传成功,OBS URL: {obs_url}") + task_info["obs_url"] = obs_url + task_info["object_key"] = object_key + else: + error_msg = f"上传失败: {str(response_info)}" + logger.error(error_msg) + finally: + # 清理临时文件 + if os.path.exists(temp_file_path): + os.remove(temp_file_path) + logger.info(f"临时文件已删除: {temp_file_path}") + # 新增结束 - # 下载音频文件 - file_name = f"suno_music_{int(time.time())}.mp3" - save_path = music_generator.base_path / file_name - if music_generator.download_audio(audio_url, str(save_path)): - task_info["local_path"] = str(save_path) else: logger.warning("音乐生成已完成,但未找到音频URL!") @@ -225,7 +268,7 @@ async def check_task_status(task_id: str = Query(..., description="音乐生成 "status": status, "title": title, "audio_url": audio_url if status == "complete" else None, - "local_path": task_info.get("local_path") if status == "complete" else None, + "obs_url": task_info.get("obs_url") if status == "complete" else None, # 新增:返回OBS URL "error_message": task_info.get("error_message") if status == "failed" else None } } diff --git a/dsLightRag/Routes/__pycache__/SunoRoute.cpython-310.pyc b/dsLightRag/Routes/__pycache__/SunoRoute.cpython-310.pyc index cb158920..b4462c6f 100644 Binary files a/dsLightRag/Routes/__pycache__/SunoRoute.cpython-310.pyc and b/dsLightRag/Routes/__pycache__/SunoRoute.cpython-310.pyc differ diff --git a/dsLightRag/static/Suno/index.html b/dsLightRag/static/Suno/index.html index 52cc3714..824f8bd9 100644 --- a/dsLightRag/static/Suno/index.html +++ b/dsLightRag/static/Suno/index.html @@ -441,11 +441,11 @@ generateMusicBtn.disabled = false; generateMusicBtn.innerHTML = ' 生成音乐'; - if (data.id) { + if (data.data && data.data.task_id) { // 跳转到进度页面 - window.location.href = `/static/Suno/music_progress.html?task_id=${data.id}`; + window.location.href = `/static/Suno/music_progress.html?task_id=${data.data.task_id}`; } else { - alert('生成音乐任务失败: ' + (data.error || '未知错误')); + alert('生成音乐任务失败: ' + (data.error || data.message || '未知错误')); } }) .catch(error => { diff --git a/dsLightRag/static/Suno/music_progress.html b/dsLightRag/static/Suno/music_progress.html index df236d1c..91d3fc03 100644 --- a/dsLightRag/static/Suno/music_progress.html +++ b/dsLightRag/static/Suno/music_progress.html @@ -76,9 +76,12 @@ const progressFill = document.getElementById('progressFill'); const progressText = document.getElementById('progressText'); - if (data.status === 'error') { + // 正确获取数据部分 + const responseData = data.data || data; + + if (responseData.status === 'error') { clearInterval(progressInterval); - statusMessage.innerHTML = ` 生成失败: ${data.error || '未知错误'}`; + statusMessage.innerHTML = ` 生成失败: ${responseData.error || responseData.error_message || '未知错误'}`; statusMessage.style.backgroundColor = '#ffebee'; statusMessage.style.borderColor = '#ef9a9a'; return; @@ -87,22 +90,24 @@ statusMessage.innerHTML = ` ${data.message || '正在生成音乐...'}`; // 更新进度条 - if (data.progress) { - progress = Math.min(data.progress, 100); - } else if (data.status === 'completed') { + if (responseData.progress) { + progress = Math.min(responseData.progress, 100); + } else if (responseData.status === 'completed') { progress = 100; - } else if (data.status === 'processing') { - // 如果没有具体进度,模拟进度增加 - if (progress < 90) { - progress += 1; + } else if (responseData.status === 'processing') { + // 改进模拟进度增加逻辑 + if (progress < 95) { + progress += 0.5; + } else if (progress < 100) { + progress += 0.1; } } progressFill.style.width = `${progress}%`; - progressText.textContent = `${progress}%`; + progressText.textContent = `${Math.round(progress)}%`; // 任务完成 - if (data.status === 'completed') { + if (responseData.status === 'completed') { clearInterval(progressInterval); statusMessage.innerHTML = ' 音乐生成完成!'; statusMessage.style.backgroundColor = '#e8f5e9'; @@ -113,9 +118,9 @@ playerContainer.style.display = 'block'; // 设置音乐标题和源 - document.getElementById('musicTitle').textContent = data.title || '生成的音乐'; + document.getElementById('musicTitle').textContent = responseData.title || '生成的音乐'; const audioSource = document.getElementById('audioSource'); - audioSource.src = data.audio_url || ''; + audioSource.src = responseData.audio_url || ''; document.getElementById('audioPlayer').load(); } })