alwaysgood commited on
Commit
e430143
·
verified ·
1 Parent(s): 3c69d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -32
app.py CHANGED
@@ -1,15 +1,18 @@
1
  import warnings
2
  from dotenv import load_dotenv
 
 
 
3
 
4
- # Load environment variables from .env file
5
  load_dotenv()
6
 
7
- # Import handlers and UI creator from modules
8
  from prediction import single_prediction
9
  from chatbot import process_chatbot_query_with_llm
10
  from ui import create_ui
11
 
12
- # Import API utilities for direct access if needed
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
- if __name__ == "__main__":
23
- # Suppress warnings for a cleaner output
24
- warnings.filterwarnings('ignore')
25
-
26
- # Create the Gradio UI by passing the handlers to the UI generator
27
- demo = create_ui(
28
- prediction_handler=single_prediction,
29
- chatbot_handler=process_chatbot_query_with_llm
30
- )
31
-
32
- # Launch the application
33
- # share=True creates a public link
34
- # server_name="0.0.0.0" allows external connections
35
- # server_port=7860 is the default Hugging Face Spaces port
36
- demo.launch(
37
- share=False, # Set to True for public sharing
38
- server_name="0.0.0.0", # For Hugging Face Spaces
39
- server_port=7860 # Default HF Spaces port
40
- )
41
-
42
- # Alternative launch configurations:
43
- # For local development:
44
- # demo.launch(share=False)
45
-
46
- # For Hugging Face Spaces:
47
- # demo.launch(server_name="0.0.0.0", server_port=7860)
48
-
49
- # For public sharing with ngrok:
50
- # demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 객체를 자동으로 찾아 실행해 줍니다.