MikuAI/api.py

72 lines
2.2 KiB
Python
Raw Normal View History

2024-03-31 21:35:19 +00:00
from fastapi import FastAPI, File, Query, Response, UploadFile
2024-03-31 19:52:44 +00:00
from fastapi.encoders import jsonable_encoder
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
from pydantic import BaseModel
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()
class Message(BaseModel):
role: str
content: str
@app.post("/")
async def root(token: str,
messages: List[Message],
response: Response,
max_new_tokens: Optional[int] = 64,
temperature: Optional[float] = 0.9,
repetition_penalty: Optional[float] = 1.2):
if not hmac.compare_digest(token, TOKEN):
response.status_code = 401
return {"error": "Bad token"}
dict_in = jsonable_encoder(messages)
output = model.inference(dict_in, max_new_tokens=max_new_tokens, temperature=temperature, repetition_penalty=repetition_penalty)
return {"raw": output}
@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)
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"}
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"}