Spaces:
Sleeping
Sleeping
| import warnings | |
| from dotenv import load_dotenv | |
| from fastapi import FastAPI | |
| import gradio as gr | |
| 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 엔드포인트를 정의합니다. | |
| # 이 코드가 /api/tide_level 과 같은 경로 요청을 처리합니다. | |
| def health(): | |
| """API 및 데이터베이스 상태를 확인합니다.""" | |
| 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) | |
| 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. 기존의 Gradio UI를 생성합니다. | |
| # create_ui 함수는 그대로 사용됩니다. | |
| warnings.filterwarnings('ignore') | |
| demo = create_ui( | |
| prediction_handler=single_prediction, | |
| chatbot_handler=process_chatbot_query_with_llm | |
| ) | |
| # 4. FastAPI 앱에 Gradio UI를 마운트(연결)합니다. | |
| # path="/"는 기본 주소로 접속했을 때 Gradio UI가 보이도록 설정합니다. | |
| app = gr.mount_gradio_app(app, demo, path="/") | |
| # 참고: 이제 demo.launch()는 사용하지 않습니다. | |
| # Hugging Face Spaces는 'app'이라는 이름의 FastAPI 객체를 자동으로 찾아 실행해 줍니다. |