Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,18 @@
|
|
| 1 |
import warnings
|
| 2 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
-
#
|
| 8 |
from prediction import single_prediction
|
| 9 |
from chatbot import process_chatbot_query_with_llm
|
| 10 |
from ui import create_ui
|
| 11 |
|
| 12 |
-
#
|
| 13 |
from api_utils import (
|
| 14 |
api_get_tide_level,
|
| 15 |
api_get_tide_series,
|
|
@@ -19,32 +22,57 @@ from api_utils import (
|
|
| 19 |
api_health_check
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import warnings
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from typing import Optional, List
|
| 6 |
|
| 7 |
+
# .env 파일에서 환경 변수 로드
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
+
# --- 모듈에서 핸들러 및 UI 생성 함수 가져오기 ---
|
| 11 |
from prediction import single_prediction
|
| 12 |
from chatbot import process_chatbot_query_with_llm
|
| 13 |
from ui import create_ui
|
| 14 |
|
| 15 |
+
# --- API 유틸리티 함수 가져오기 ---
|
| 16 |
from api_utils import (
|
| 17 |
api_get_tide_level,
|
| 18 |
api_get_tide_series,
|
|
|
|
| 22 |
api_health_check
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# 1. FastAPI 애플리케이션을 먼저 생성합니다.
|
| 26 |
+
app = FastAPI(
|
| 27 |
+
title="실시간 조위 예측 API",
|
| 28 |
+
description="TimeXer 모델과 조화 분석을 이용한 조위 예측 API입니다.",
|
| 29 |
+
version="1.0.0",
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# 2. FastAPI를 사용하여 각 API 엔드포인트를 정의합니다.
|
| 33 |
+
# 이 코드가 /api/tide_level 과 같은 경로 요청을 처리합니다.
|
| 34 |
+
@app.get("/api/health", tags=["Status"])
|
| 35 |
+
def health():
|
| 36 |
+
"""API 및 데이터베이스 상태를 확인합니다."""
|
| 37 |
+
return api_health_check()
|
| 38 |
+
|
| 39 |
+
@app.get("/api/tide_level", tags=["Tide Predictions"])
|
| 40 |
+
def get_tide_level(station_id: str, target_time: Optional[str] = None):
|
| 41 |
+
"""특정 관측소의 특정 시간 조위를 조회합니다."""
|
| 42 |
+
return api_get_tide_level(station_id, target_time)
|
| 43 |
+
|
| 44 |
+
@app.get("/api/tide_series", tags=["Tide Predictions"])
|
| 45 |
+
def get_tide_series(station_id: str, start_time: Optional[str] = None, end_time: Optional[str] = None, interval_minutes: int = 60):
|
| 46 |
+
"""지정된 기간의 시계열 조위 데이터를 조회합니다."""
|
| 47 |
+
return api_get_tide_series(station_id, start_time, end_time, interval_minutes)
|
| 48 |
+
|
| 49 |
+
@app.get("/api/extremes", tags=["Tide Analysis"])
|
| 50 |
+
def get_extremes(station_id: str, date: Optional[str] = None, include_secondary: bool = False):
|
| 51 |
+
"""특정 날짜의 만조/간조 정보를 조회합니다."""
|
| 52 |
+
return api_get_extremes_info(station_id, date, include_secondary)
|
| 53 |
+
|
| 54 |
+
@app.get("/api/alert", tags=["Tide Analysis"])
|
| 55 |
+
def check_alert(station_id: str, hours_ahead: int = 24, warning_level: float = 700.0, danger_level: float = 750.0):
|
| 56 |
+
"""설정된 위험 수위 도달 여부를 확인합니다."""
|
| 57 |
+
return api_check_tide_alert(station_id, hours_ahead, warning_level, danger_level)
|
| 58 |
+
|
| 59 |
+
@app.get("/api/compare", tags=["Tide Analysis"])
|
| 60 |
+
def compare_stations(station_ids: List[str], target_time: Optional[str] = None):
|
| 61 |
+
"""여러 관측소의 조위를 동시에 비교합니다."""
|
| 62 |
+
return api_compare_stations(station_ids, target_time)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# 3. 기존의 Gradio UI를 생성합니다.
|
| 66 |
+
# create_ui 함수는 그대로 사용됩니다.
|
| 67 |
+
warnings.filterwarnings('ignore')
|
| 68 |
+
demo = create_ui(
|
| 69 |
+
prediction_handler=single_prediction,
|
| 70 |
+
chatbot_handler=process_chatbot_query_with_llm
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# 4. FastAPI 앱에 Gradio UI를 마운트(연결)합니다.
|
| 74 |
+
# path="/"는 기본 주소로 접속했을 때 Gradio UI가 보이도록 설정합니다.
|
| 75 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 76 |
+
|
| 77 |
+
# 참고: 이제 demo.launch()는 사용하지 않습니다.
|
| 78 |
+
# Hugging Face Spaces는 'app'이라는 이름의 FastAPI 객체를 자동으로 찾아 실행해 줍니다.
|