from fastapi import FastAPI, Request, Form from fastapi.responses import HTMLResponse from pydantic import BaseModel import yaml from model.llama3 import LLaMA3 # ---------------- 설정 로드 ---------------- with open("config/llama3.yaml", "r") as f: config = yaml.safe_load(f) # ---------------- 모델 초기화 ---------------- llama3 = LLaMA3(config) # ---------------- FastAPI 앱 ---------------- app = FastAPI( title="Korean Pronunciation Correction API", description="FastAPI + LLaMA3 기반 발음 교정 서버", version="1.0.0" ) # ---------------- 입력 모델 ---------------- class InputData(BaseModel): user_input: str correct_input: str # ---------------- API: JSON POST ---------------- @app.post("/generate") async def generate_correction(data: InputData): result = llama3.generate(data.user_input, data.correct_input) return {"result": result} # ---------------- HTML UI ---------------- @app.get("/", response_class=HTMLResponse) async def form_ui(): return """ Korean Pronunciation Correction

🗣️ 발음 교정 테스트







""" # ---------------- 결과 렌더링 ---------------- from fastapi.responses import HTMLResponse from fastapi import Form, Request import traceback @app.post("/submit", response_class=HTMLResponse) async def handle_form(request: Request, user_input: str = Form(...), correct_input: str = Form(...)): try: result = llama3.generate(user_input, correct_input) except Exception as e: error_details = traceback.format_exc() return f""" 에러

❌ 서버 오류 발생

오류 메시지:

{str(e)}

에러 상세:

{error_details}

돌아가기 """ return f""" 결과

✅ 결과

입력된 발음: {user_input}

정답 발음: {correct_input}


🧠 모델 응답:

{result}

다시 시도하기 """ # ---------------- 헬스 체크 ---------------- @app.get("/health") async def health_check(): return { "status": "ok", "model": config["model"]["id"], "device": config["model"]["device"] }