61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
import os
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import FileResponse
|
|
from dotenv import load_dotenv
|
|
import uvicorn
|
|
|
|
from models import RealEstateQuery, ParsedRealEstate
|
|
from openai_parser import OpenAIParser
|
|
|
|
load_dotenv()
|
|
|
|
app = FastAPI(title="부동산 검색 API")
|
|
|
|
# CORS 설정
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# OpenAI 파서 초기화
|
|
try:
|
|
parser = OpenAIParser()
|
|
except ValueError as e:
|
|
print(f"Warning: {e}")
|
|
parser = None
|
|
|
|
@app.get("/")
|
|
async def serve_index():
|
|
"""메인 페이지 제공"""
|
|
return FileResponse("../frontend/index.html")
|
|
|
|
@app.post("/api/parse", response_model=ParsedRealEstate)
|
|
async def parse_real_estate(query: RealEstateQuery):
|
|
"""자연어 입력을 파싱하여 부동산 정보 추출"""
|
|
if not parser:
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail="OpenAI API key not configured. Please set OPENAI_API_KEY in .env file"
|
|
)
|
|
|
|
try:
|
|
result = await parser.parse_real_estate_query(query.text)
|
|
print(result)
|
|
return result
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
# 정적 파일 서빙 (CSS, JS)
|
|
app.mount("/static", StaticFiles(directory="../frontend"), name="static")
|
|
|
|
if __name__ == "__main__":
|
|
host = os.getenv("HOST", "0.0.0.0")
|
|
port = int(os.getenv("PORT", 20001))
|
|
|
|
print(f"Starting server at http://localhost:{port}")
|
|
uvicorn.run(app, host=host, port=port, reload=True) |