Spaces:
Sleeping
Sleeping
# app.py | |
import warnings | |
from dotenv import load_dotenv | |
from fastapi import FastAPI | |
import gradio as gr | |
import uvicorn | |
from typing import Optional, List | |
# .env 파일에서 환경 변수 로드 | |
load_dotenv() | |
# --- 모듈에서 핸들러 및 UI 생성 함수 가져오기 --- | |
from prediction import single_prediction | |
from chatbot import process_chatbot_query_with_llm | |
from ui import create_ui | |
# --- API 유틸리티 함수 가져오기 --- | |
from api_utils import ( | |
api_get_tide_level, | |
api_get_tide_series, | |
api_get_extremes_info, | |
api_check_tide_alert, | |
api_compare_stations, | |
api_health_check | |
) | |
# 1. FastAPI 애플리케이션 생성 | |
app = FastAPI( | |
title="실시간 조위 예측 API", | |
description="TimeXer 모델과 조화 분석을 이용한 조위 예측 API입니다.", | |
version="1.0.0", | |
) | |
# 2. FastAPI API 엔드포인트 정의 | |
def health(): | |
return api_health_check() | |
def get_tide_level(station_id: str, target_time: Optional[str] = None): | |
return api_get_tide_level(station_id, target_time) | |
# ... (다른 모든 @app.get 경로들) ... | |
def get_tide_series(station_id: str, start_time: Optional[str] = None, end_time: Optional[str] = None, interval_minutes: int = 60): | |
return api_get_tide_series(station_id, start_time, end_time, interval_minutes) | |
def get_extremes(station_id: str, date: Optional[str] = None, include_secondary: bool = False): | |
return api_get_extremes_info(station_id, date, include_secondary) | |
def check_alert(station_id: str, hours_ahead: int = 24, warning_level: float = 700.0, danger_level: float = 750.0): | |
return api_check_tide_alert(station_id, hours_ahead, warning_level, danger_level) | |
def compare_stations(station_ids: List[str], target_time: Optional[str] = None): | |
return api_compare_stations(station_ids, target_time) | |
# ---------------------------------------------------- | |
# ▼▼▼ 여기에 새로운 코드 추가 ▼▼▼ | |
# ---------------------------------------------------- | |
# 3. UI 내부 테스트 버튼과 실제 함수를 연결할 핸들러 딕셔너리 생성 | |
api_handlers_for_ui = { | |
"health": api_health_check, | |
"tide_level": api_get_tide_level, | |
"tide_series": api_get_tide_series, | |
"extremes": api_get_extremes_info, | |
"alert": api_check_tide_alert, | |
"compare": api_compare_stations | |
} | |
# ---------------------------------------------------- | |
# --- Gradio UI 생성 --- | |
warnings.filterwarnings('ignore') | |
# 4. create_ui 호출 시, 위에서 만든 핸들러 딕셔너리를 전달 | |
demo = create_ui( | |
prediction_handler=single_prediction, | |
chatbot_handler=process_chatbot_query_with_llm, | |
api_handlers=api_handlers_for_ui # <--- 이 인자가 추가되었습니다! | |
) | |
# --- FastAPI 앱에 Gradio UI 마운트 --- | |
app = gr.mount_gradio_app(app, demo, path="/") | |
# --- 로컬에서 직접 실행할 때를 위한 실행 블록 --- | |
if __name__ == "__main__": | |
print("로컬 개발 서버를 시작합니다. http://127.0.0.1:7860 에서 확인하세요.") | |
uvicorn.run( | |
"app:app", # <--- 이 부분을 수정! | |
host="0.0.0.0", | |
port=7860, | |
reload=True # 이 옵션을 정상적으로 사용하기 위함입니다. | |
) |