File size: 3,208 Bytes
301ef28
613de59
e430143
 
 
301ef28
e430143
613de59
301ef28
e430143
613de59
 
 
626a68f
e430143
3c69d81
 
 
 
 
 
 
b201c0c
 
e430143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 과 같은 경로 요청을 처리합니다.
@app.get("/api/health", tags=["Status"])
def health():
    """API 및 데이터베이스 상태를 확인합니다."""
    return api_health_check()

@app.get("/api/tide_level", tags=["Tide Predictions"])
def get_tide_level(station_id: str, target_time: Optional[str] = None):
    """특정 관측소의 특정 시간 조위를 조회합니다."""
    return api_get_tide_level(station_id, target_time)

@app.get("/api/tide_series", tags=["Tide Predictions"])
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)

@app.get("/api/extremes", tags=["Tide Analysis"])
def get_extremes(station_id: str, date: Optional[str] = None, include_secondary: bool = False):
    """특정 날짜의 만조/간조 정보를 조회합니다."""
    return api_get_extremes_info(station_id, date, include_secondary)

@app.get("/api/alert", tags=["Tide Analysis"])
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)

@app.get("/api/compare", tags=["Tide Analysis"])
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 객체를 자동으로 찾아 실행해 줍니다.