Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💄 适配更多的mysql,同时优化了并发问题,代码重构 #46

Merged
merged 60 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0885d99
💄fix format
Yanyutin753 Jun 19, 2024
3b49ccf
Merge branch 'back' of https://github.com/Yanyutin753/Suno2openai int…
Yanyutin753 Jun 19, 2024
4a35408
💄 refac project
Yanyutin753 Jun 19, 2024
e019a41
💄 fix format
Yanyutin753 Jun 19, 2024
0ad34c7
🎖️ 适配mysql 5.7
Yanyutin753 Jun 19, 2024
ad961bb
Update docker-compose.yml
Yanyutin753 Jun 19, 2024
0f9aca3
Update requirements.txt
Yanyutin753 Jun 19, 2024
20fd2ec
💄 fix format
Yanyutin753 Jun 19, 2024
f584798
💄 fix format
Yanyutin753 Jun 19, 2024
0689690
Update docker-compose.yml
Yanyutin753 Jun 19, 2024
b90b30e
🤗 fix better
Yanyutin753 Jun 19, 2024
4375ea4
💄 fix format
Yanyutin753 Jun 19, 2024
672c1e6
💄 fix format
Yanyutin753 Jun 20, 2024
3958b2a
💄 fix format
Yanyutin753 Jun 20, 2024
588f452
💄 fix format
Yanyutin753 Jun 20, 2024
ff17ab1
💄 fix format
Yanyutin753 Jun 20, 2024
28241d3
💄 fix format
Yanyutin753 Jun 20, 2024
9c1ca54
🤗 fix bug
Yanyutin753 Jun 20, 2024
193c67a
💄 适配更多的mysql
Yanyutin753 Jun 20, 2024
e76a5de
💄 fix format
Yanyutin753 Jun 20, 2024
c2381da
Merge pull request #23 from wlhtea/main
Yanyutin753 Jun 20, 2024
1da93ff
🤖 新增版本号方便维护项目
Yanyutin753 Jun 20, 2024
3c4a499
Merge branch 'back' of https://github.com/Yanyutin753/Suno2openai int…
Yanyutin753 Jun 20, 2024
c78ec86
🤗 fix raise bug
Yanyutin753 Jun 20, 2024
dae5bce
🤗 fix raise bug
Yanyutin753 Jun 20, 2024
068fa39
💄重构suno.py,修改成异步函数
Yanyutin753 Jun 20, 2024
ab467c5
💄重构suno.py,修改成异步函数
Yanyutin753 Jun 20, 2024
9760ad5
Revert "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
ee84140
Revert "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
2bccfb8
Reapply "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
b85d826
Reapply "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
f046127
💄fix format
Yanyutin753 Jun 20, 2024
637bde2
💄fix format
Yanyutin753 Jun 20, 2024
cee0a5e
Merge pull request #24 from wlhtea/main
Yanyutin753 Jun 20, 2024
848b599
💄fix format
Yanyutin753 Jun 20, 2024
fc901ed
💄fix format
Yanyutin753 Jun 20, 2024
cc5477c
Merge branch 'back' of https://github.com/Yanyutin753/Suno2openai int…
Yanyutin753 Jun 20, 2024
92be4d2
🤗 fix raise bug
Yanyutin753 Jun 20, 2024
b6d80e2
🤗 fix raise bug
Yanyutin753 Jun 20, 2024
ad594a5
💄 fix format
Yanyutin753 Jun 20, 2024
628286f
💄 fix format
Yanyutin753 Jun 20, 2024
9d30117
💄重构suno.py,修改成异步函数
Yanyutin753 Jun 20, 2024
7df314e
💄重构suno.py,修改成异步函数
Yanyutin753 Jun 20, 2024
3656c0c
Revert "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
4985f30
Revert "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
4b17a94
Reapply "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
be9b482
Reapply "💄重构suno.py,修改成异步函数"
Yanyutin753 Jun 20, 2024
a5f0fa3
💄fix format
Yanyutin753 Jun 20, 2024
d42fd95
💄fix format
Yanyutin753 Jun 20, 2024
9852533
💄fix format
Yanyutin753 Jun 20, 2024
0171702
💄fix format
Yanyutin753 Jun 20, 2024
77c33e9
💄fix format
Yanyutin753 Jun 20, 2024
df20c7f
🤖 fix format
Yanyutin753 Jun 20, 2024
03324b5
💄 fix format
Yanyutin753 Jun 20, 2024
3ca0d5e
🤖 新增最长等待时间变量`MAX_TIME`
Yanyutin753 Jun 20, 2024
6e1be5f
💄 fix format
Yanyutin753 Jun 21, 2024
e9914d5
🤖 新增代理环境变量PROXY,优化代码
Yanyutin753 Jun 21, 2024
89b76f4
💄 fix format
Yanyutin753 Jun 21, 2024
ad62f34
🤗 fix format
Yanyutin753 Jun 22, 2024
7a52958
👀 更新默认配置
Yanyutin753 Jun 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reapply "💄重构suno.py,修改成异步函数"
This reverts commit 1aa4494.
  • Loading branch information
Yanyutin753 committed Jun 20, 2024
commit 4b17a94fedb09c6384cec8d6e7f232996c377d05
58 changes: 39 additions & 19 deletions data/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from starlette.responses import StreamingResponse, JSONResponse

from data.cookie import suno_auth
from suno.suno import SongsGen
from util.config import RETRIES
from util.logger import logger
from util.tool import get_clips_ids, check_status_complete, deleteSongID, calculate_token_costs
Expand Down Expand Up @@ -45,23 +44,24 @@ async def generate_data(db_manager, chat_user_message, chat_id, timeStamp, Model

for try_count in range(RETRIES):
cookie = None
song_gen = None
try:
cookie = str(await db_manager.get_request_cookie()).strip()
if cookie is None:
raise RuntimeError("没有可用的cookie")
else:
song_gen = SongsGen(cookie)
remaining_count = song_gen.get_limit_left()
if remaining_count == -1:
await db_manager.delete_cookies(cookie)
raise RuntimeError("该账号剩余次数为 -1,无法使用")
# song_gen = SongsGen(cookie)
# remaining_count = await song_gen.get_limit_left()
# if remaining_count == -1:
# await db_manager.delete_cookies(cookie)
# raise RuntimeError("该账号剩余次数为 -1,无法使用")

# 测试并发集
# yield f"""data:""" + ' ' + f"""{json.dumps({"id": f"chatcmpl-{chat_id}", "object":
# "chat.completion.chunk", "model": ModelVersion, "created": timeStamp, "choices": [{"index": 0,
# "delta": {"content": str(cookie)}, "finish_reason": None}]})}\n\n"""
# yield f"""data:""" + ' ' + f"""[DONE]\n\n"""
# return
yield f"""data:""" + ' ' + f"""{json.dumps({"id": f"chatcmpl-{chat_id}", "object":
"chat.completion.chunk", "model": ModelVersion, "created": timeStamp, "choices": [{"index": 0,
"delta": {"content": str(cookie)}, "finish_reason": None}]})}\n\n"""
yield f"""data:""" + ' ' + f"""[DONE]\n\n"""
return

_return_ids = False
_return_tags = False
Expand All @@ -71,14 +71,15 @@ async def generate_data(db_manager, chat_user_message, chat_id, timeStamp, Model
_return_video_url = False
_return_audio_url = False
_return_Forever_url = False
token, sid = song_gen.get_auth_token(w=1)

token, sid = await song_gen.get_auth_token(w=1)

suno_auth.set_session_id(sid)
suno_auth.load_cookie(cookie)

response = await generate_music(data=data, token=token)
# await asyncio.sleep(3)
clip_ids = get_clips_ids(response)
clip_ids = await get_clips_ids(response)
song_id_1 = clip_ids[0]
song_id_2 = clip_ids[1]

Expand All @@ -87,7 +88,9 @@ async def generate_data(db_manager, chat_user_message, chat_id, timeStamp, Model
for clip_id in clip_ids:
count = 0
while True:
token, sid = SongsGen(cookie).get_auth_token(w=1)
token, sid = await song_gen.get_auth_token(w=1)
now_data = None
more_information_ = None
try:
now_data = await get_feed(ids=clip_id, token=token)
more_information_ = now_data[0]['metadata']
Expand All @@ -99,7 +102,6 @@ async def generate_data(db_manager, chat_user_message, chat_id, timeStamp, Model
if not _return_Forever_url:
try:
if check_status_complete(now_data):
await deleteSongID(db_manager, cookie)
Aideo_Markdown_Conetent = (f""
f"\n### 🎷 CDN音乐链接\n"
f"- **🎧 音乐1️⃣**:{'https://cdn1.suno.ai/' + clip_id + '.mp3'} \n"
Expand All @@ -117,7 +119,7 @@ async def generate_data(db_manager, chat_user_message, chat_id, timeStamp, Model
_return_Forever_url = True
break
except Exception as e:
logger.info('CDN音乐链接出错', e)
logger.info(f'CDN音乐链接出错:{e}')
pass

if not _return_ids:
Expand Down Expand Up @@ -195,18 +197,22 @@ async def generate_data(db_manager, chat_user_message, chat_id, timeStamp, Model

yield f"""data:""" + ' ' + f"""[DONE]\n\n"""
break

except Exception as e:
if cookie is not None:
await deleteSongID(db_manager, cookie)
if try_count < RETRIES - 1:
logger.error(f"第 {try_count + 1} 次尝试歌曲失败,错误为:{str(e)},重试中......")
continue
else:
yield f"""data:""" + ' ' + f"""{json.dumps({"id": f"chatcmpl-{chat_id}", "object": "chat.completion.chunk", "model": ModelVersion, "created": timeStamp, "choices": [{"index": 0, "delta": {"content": str("生成歌曲失败: 请打开日志或数据库查看报错信息......")}, "finish_reason": None}]})}\n\n"""
yield f"""data:""" + ' ' + f"""[DONE]\n\n"""
finally:
if song_gen is not None:
await song_gen.close_session()
if cookie is not None:
await deleteSongID(db_manager, cookie)


# 返回消息
# 返回消息,使用协程
async def response_async(db_manager, data, content_all, chat_id, timeStamp, last_user_content, headers):
if not data.stream:
try:
Expand Down Expand Up @@ -256,3 +262,17 @@ async def response_async(db_manager, data, content_all, chat_id, timeStamp, last
return StreamingResponse(data_generator, headers=headers, media_type="text/event-stream")
except Exception as e:
return JSONResponse(status_code=500, content={"detail": f"生成流式响应时出错: {str(e)}"})


# 线程用于请求
def request_chat(db_manager, data, content_all, chat_id, timeStamp, last_user_content, headers):
loop = asyncio.new_event_loop()
try:
asyncio.set_event_loop(loop)
result = loop.run_until_complete(
response_async(db_manager, data, content_all, chat_id, timeStamp, last_user_content, headers))
except Exception as e:
raise HTTPException(status_code=500, detail=f"请求聊天时出错: {str(e)}")
finally:
loop.close()
return result
12 changes: 12 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
process_cookie = process_cookies.processCookies(SQL_IP, int(SQL_DK), USER_NAME, SQL_PASSWORD, SQL_NAME)


# executor = ThreadPoolExecutor(max_workers=300, thread_name_prefix="Music_thread")


# 刷新cookies函数
async def cron_refresh_cookies():
try:
Expand Down Expand Up @@ -174,10 +177,19 @@ async def get_last_user_message(data: schemas.Data, authorization: str = Header(
}

try:
# 协程处理
return await response_async(db_manager, data, content_all, chat_id, timeStamp, last_user_content, headers)
except HTTPException as http_exc:
raise http_exc

# 线程处理
# try:
# future = executor.submit(request_chat, db_manager, data, content_all, chat_id, timeStamp,
# last_user_content, headers)
# return future.result()
# except Exception as e:
# raise HTTPException(status_code=500, detail=str(e))


# 授权检查
async def verify_auth_header(authorization: str = Header(...)):
Expand Down
Loading