MikuAI/api.py

66 lines
1.9 KiB
Python
Raw Normal View History

2025-01-18 01:58:50 -08:00
from fastapi import FastAPI, File, Query, Response
2024-03-31 19:52:44 +00:00
from fastapi.responses import FileResponse
2024-05-05 01:16:35 -07:00
from config import EDGETTS_VOICE, TOKEN
import edge_tts
2024-03-31 19:52:44 +00:00
import hmac
import model
2024-03-31 21:35:19 +00:00
from typing import Annotated, List, Optional
2024-03-31 19:52:44 +00:00
import tempfile
from rvc.main import song_cover_pipeline
app = FastAPI()
@app.post("/")
async def root(token: str,
2025-01-18 01:58:50 -08:00
messages: List[model.DiscordMessage],
2024-03-31 19:52:44 +00:00
response: Response,
2025-01-18 01:58:50 -08:00
max_new_tokens: Optional[int] = 128,
temperature: Optional[float] = 0.9):
2024-03-31 19:52:44 +00:00
if not hmac.compare_digest(token, TOKEN):
response.status_code = 401
return {"error": "Bad token"}
2025-01-18 01:58:50 -08:00
return model.inference(messages, max_new_tokens=max_new_tokens, temperature=temperature)
2024-03-31 19:52:44 +00:00
@app.post("/rvc")
async def rvc(token: str,
2024-03-31 21:35:19 +00:00
file: Annotated[bytes, File()],
2024-03-31 19:52:44 +00:00
response: Response,
2024-03-31 21:35:19 +00:00
pitch_change_oct: Annotated[int, Query()] = 1,
pitch_change_sem: Annotated[int, Query()] = 0):
2024-03-31 19:52:44 +00:00
if not hmac.compare_digest(token, TOKEN):
response.status_code = 401
return {"error": "Bad token"}
with tempfile.NamedTemporaryFile() as tmp:
2024-03-31 21:35:19 +00:00
tmp.write(file)
2024-03-31 19:52:44 +00:00
ai_vocals_path = song_cover_pipeline(tmp.name, pitch_change_oct, voice_model='miku', pitch_change_sem=pitch_change_sem)
return FileResponse(ai_vocals_path)
2025-01-18 01:58:50 -08:00
2024-05-05 01:16:35 -07:00
@app.post("/tts")
async def tts(token: str,
text: str,
response: Response,
pitch_change_oct: Annotated[int, Query()] = 1,
pitch_change_sem: Annotated[int, Query()] = 0):
if not hmac.compare_digest(token, TOKEN):
response.status_code = 401
return {"error": "Bad token"}
2025-01-18 01:58:50 -08:00
2024-05-05 01:16:35 -07:00
with tempfile.NamedTemporaryFile() as tmp:
communicate = edge_tts.Communicate(text, EDGETTS_VOICE)
await communicate.save(tmp.name)
ai_vocals_path = song_cover_pipeline(tmp.name, pitch_change_oct, voice_model='miku', pitch_change_sem=pitch_change_sem)
return FileResponse(ai_vocals_path)
2024-03-31 19:52:44 +00:00
@app.get("/ping")
def ping():
return {"message": "pong"}