chu2-rvc-api / app /main.py
ionvop's picture
Update app/main.py
c634ab8 verified
from fastapi import FastAPI, Response
import asyncio
import hashlib
import os
from tts_with_rvc import TTS_RVC
app = FastAPI()
CACHE_DIR = "cache"
os.makedirs(CACHE_DIR, exist_ok=True)
@app.get("/")
def read_root():
return {"message": "Hello from FastAPI on Hugging Face Spaces 🚀"}
@app.get("/speak")
async def speak(text: str):
text_hash = hashlib.sha256(text.encode("utf-8")).hexdigest()
cache_path = os.path.join(CACHE_DIR, f"{text_hash}.wav")
if os.path.exists(cache_path):
with open(cache_path, "rb") as f:
return Response(content=f.read(), media_type="audio/wav")
tts = TTS_RVC(
model_path="app/models/chu2.pth",
voice="ja-JP-NanamiNeural",
device="cpu",
index_path="app/models/chu2.index",
f0_method="pm"
)
path = tts(
text=text,
pitch=6,
tts_rate=30
)
os.rename(path, cache_path)
with open(cache_path, "rb") as f:
audio_bytes = f.read()
return Response(content=audio_bytes, media_type="audio/wav")