|
|
|
|
| import json
|
| import logging
|
| from fastapi import Depends, FastAPI, HTTPException, Request, status
|
| from fastapi.middleware.cors import CORSMiddleware
|
|
|
| import schemas
|
| from deps import get_token, reset_account_status
|
| from utils import generate_lyrics, generate_music, get_feed, get_lyrics, get_credits
|
| from cookie import suno_auths
|
|
|
| app = FastAPI()
|
|
|
| app.add_middleware(
|
| CORSMiddleware,
|
| allow_origins=["*"],
|
| allow_credentials=True,
|
| allow_methods=["*"],
|
| allow_headers=["*"],
|
| )
|
|
|
|
|
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
| @app.on_event("startup")
|
| async def startup_event():
|
|
|
| reset_account_status()
|
| logging.info("所有账号状态已重置")
|
|
|
| @app.get("/")
|
| async def get_root():
|
| return schemas.Response()
|
|
|
| @app.post("/generate")
|
| async def generate(
|
| data: schemas.CustomModeGenerateParam, token: str = Depends(get_token)
|
| ):
|
| try:
|
| resp = await generate_music(data.dict(), token)
|
| return resp
|
| except Exception as e:
|
| logging.error(f"生成音乐失败: {str(e)}")
|
| raise HTTPException(
|
| detail=str(e), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
|
| )
|
|
|
| @app.post("/generate/description-mode")
|
| async def generate_with_song_description(
|
| data: schemas.DescriptionModeGenerateParam, token: str = Depends(get_token)
|
| ):
|
| try:
|
| resp = await generate_music(data.dict(), token)
|
| return resp
|
| except Exception as e:
|
| logging.error(f"根据描述生成音乐失败: {str(e)}")
|
| raise HTTPException(
|
| detail=str(e), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
|
| )
|
|
|
| @app.get("/feed/{aid}")
|
| async def fetch_feed(aid: str, token: str = Depends(get_token)):
|
| try:
|
| resp = await get_feed(aid, token)
|
| return resp
|
| except Exception as e:
|
| logging.error(f"获取feed失败: {str(e)}")
|
| raise HTTPException(
|
| detail=str(e), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
|
| )
|
|
|
| @app.post("/generate/lyrics/")
|
| async def generate_lyrics_post(request: Request, token: str = Depends(get_token)):
|
| req = await request.json()
|
| prompt = req.get("prompt")
|
| if prompt is None:
|
| raise HTTPException(
|
| detail="prompt is required", status_code=status.HTTP_400_BAD_REQUEST
|
| )
|
|
|
| try:
|
| resp = await generate_lyrics(prompt, token)
|
| return resp
|
| except Exception as e:
|
| logging.error(f"生成歌词失败: {str(e)}")
|
| raise HTTPException(
|
| detail=str(e), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
|
| )
|
|
|
| @app.get("/lyrics/{lid}")
|
| async def fetch_lyrics(lid: str, token: str = Depends(get_token)):
|
| try:
|
| resp = await get_lyrics(lid, token)
|
| return resp
|
| except Exception as e:
|
| logging.error(f"获取歌词失败: {str(e)}")
|
| raise HTTPException(
|
| detail=str(e), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
|
| )
|
|
|
| @app.get("/get_credits")
|
| async def fetch_credits(token: str = Depends(get_token)):
|
| try:
|
| resp = await get_credits(token)
|
| return resp
|
| except Exception as e:
|
| logging.error(f"获取积分失败: {str(e)}")
|
| raise HTTPException(
|
| detail=str(e), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
|
| )
|
|
|
| @app.get("/account_status")
|
| async def get_account_status():
|
| status = {}
|
| for i, suno_auth in suno_auths.items():
|
| status[i] = {
|
| "session_id": suno_auth.get_session_id(),
|
| "is_disabled": suno_auth.is_disabled
|
| }
|
| return status
|
|
|
| if __name__ == "__main__":
|
| import uvicorn
|
| uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|