from fastapi import FastAPI from pydantic import BaseModel from fastapi.middleware.cors import CORSMiddleware import sys import os # enhanced_g2pk.py 경로 추가 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'model')) from enhanced_g2pk import EnhancedG2p app = FastAPI() # CORS 설정 - 허깅페이스 도메인만 허용 app.add_middleware( CORSMiddleware, allow_origins=["https://speako-frontend.hf.space"], # 배포된 프론트엔드만 허용 allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # G2P 인스턴스 생성 g2p = EnhancedG2p() class TextRequest(BaseModel): text: str @app.post("/g2pk") async def convert_to_g2pk(request: TextRequest): try: result = g2p(request.text) return {"result": result} except Exception as e: return {"error": str(e)} @app.get("/healthcheck") async def healthcheck(): return {"status": "ok"} if __name__ == "__main__": import uvicorn uvicorn.run("app:app", host="0.0.0.0", port=7860)