import gradio as gr import os from datetime import datetime from config import STATIONS from supabase_utils import get_supabase_client from api_utils import get_tide_data def view_tide_data_handler(station_id, date): """Handler to fetch and display tide data for a specific date.""" if not station_id or not date: return None, None, "관측소와 날짜를 선택해주세요." date_str = date.strftime('%Y-%m-%d') try: result = get_tide_data(station_id, start_date=date_str, include_extremes=True, return_plot=True) if result['data'].empty: return None, None, f"{date_str}에 대한 데이터가 없습니다." extremes_df = result.get('extremes', None) return result['plot'], extremes_df, f"{date_str}의 조위 데이터 조회 완료" except Exception as e: return None, None, f"오류 발생: {e}" def create_ui(prediction_handler, chatbot_handler): """Gradio UI를 생성하고 반환합니다.""" with gr.Blocks(title="통합 조위 예측 시스템", theme=gr.themes.Soft()) as demo: gr.Markdown("# 🌊 통합 조위 예측 시스템 with Gemini") # 연결 상태 표시 client = get_supabase_client() supabase_status = "🟢 연결됨" if client else "🔴 연결 안됨 (환경변수 확인 필요)" gemini_status = "🟢 연결됨" if os.getenv("GEMINI_API_KEY") else "🔴 연결 안됨 (환경변수 확인 필요)" gr.Markdown(f"**Supabase 상태**: {supabase_status} | **Gemini 상태**: {gemini_status}") with gr.Tabs(): with gr.TabItem("통합 조위 예측"): with gr.Row(): with gr.Column(scale=1): station_id_input = gr.Dropdown(STATIONS, label="관측소 선택", value="DT_0001") input_csv = gr.File(label="과거 데이터 업로드 (.csv)") predict_btn = gr.Button("예측 실행", variant="primary") with gr.Column(scale=3): output_plot = gr.Plot(label="예측 결과 시각화") output_df = gr.DataFrame(label="예측 결과 데이터") output_log = gr.Textbox(label="실행 로그", lines=5, interactive=False) with gr.TabItem("데이터 조회 및 분석"): gr.Markdown("### 특정 날짜의 조위 데이터 조회") with gr.Row(): with gr.Column(scale=1): view_station_input = gr.Dropdown(STATIONS, label="관측소 선택", value="DT_0001") view_date_input = gr.DatePicker(label="날짜 선택", value=datetime.now().strftime("%Y-%m-%d")) view_btn = gr.Button("조회 실행", variant="primary") with gr.Column(scale=3): view_output_plot = gr.Plot(label="조위 그래프") view_output_extremes = gr.DataFrame(label="만조 및 간조 정보") view_log = gr.Textbox(label="실행 로그", lines=2, interactive=False) with gr.TabItem("AI 조위 챗봇"): gr.ChatInterface( fn=chatbot_handler, title="AI 조위 챗봇", description="조위에 대해 궁금한 점을 물어보세요. (예: '인천 오늘 현재 조위 알려줘')", #examples=[] ) predict_btn.click( fn=prediction_handler, inputs=[station_id_input, input_csv], outputs=[output_plot, output_df, output_log] ) view_btn.click( fn=view_tide_data_handler, inputs=[view_station_input, view_date_input], outputs=[view_output_plot, view_output_extremes, view_log] ) return demo