File size: 3,499 Bytes
4747898
 
301ef28
613de59
e430143
 
621229d
e430143
301ef28
e430143
613de59
301ef28
e430143
613de59
 
 
626a68f
e430143
3c69d81
 
 
 
 
 
 
b201c0c
 
4747898
e430143
 
 
 
 
 
4747898
e430143
 
 
 
 
 
 
 
4747898
e430143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4747898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb94029
e430143
4747898
e430143
 
4747898
 
e430143
 
4747898
eb94029
e430143
 
eb94029
 
 
 
 
863f056
eb94029
4747898
863f056
4747898
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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 엔드포인트 정의
@app.get("/api/health", tags=["Status"])
def health():
    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 경로들) ...
@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. 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   # 이 옵션을 정상적으로 사용하기 위함입니다.
    )