import gradio as gr import requests import matplotlib.pyplot as plt import numpy as np import os # 파일 존재 확인을 위해 import import os os.environ["aaa"] = os.environ.get("bbb") ccc= os.environ["aaa"] # 이미지 파일 저장 경로 SUN_IMAGE_PATH = "sun.png" WATER_DROP_IMAGE_PATH = "water_drop.png" # 태양 이미지 생성 함수 def create_sun_image(filepath=SUN_IMAGE_PATH): fig, ax = plt.subplots(figsize=(6, 6), facecolor='none') # 배경 투명하게 ax.set_aspect('equal') ax.axis('off') sun_circle = plt.Circle((0, 0), radius=0.8, color='yellow', edgecolor='black') ax.add_patch(sun_circle) num_rays = 20 ray_length = 0.5 for i in range(num_rays): angle = 2 * np.pi * i / num_rays x_start = 0.8 * np.cos(angle) y_start = 0.8 * np.sin(angle) x_end = (0.8 + ray_length) * np.cos(angle) y_end = (0.8 + ray_length) * np.sin(angle) ax.plot([x_start, x_end], [y_start, y_end], color='yellow', linewidth=2) ax.set_xlim([-1.5, 1.5]) ax.set_ylim([-1.5, 1.5]) fig.savefig(filepath, transparent=True, bbox_inches='tight', pad_inches=0) # 투명 배경, 여백 최소화하여 저장 plt.close(fig) # Figure 닫아서 메모리 관리 # 물방울 이미지 생성 함수 def create_water_drop_image(filepath=WATER_DROP_IMAGE_PATH): fig, ax = plt.subplots(figsize=(5, 5), facecolor='none') # 배경 투명하게 ax.set_aspect('equal') ax.axis('off') water_drop = plt.Circle((0.5, 0.5), 0.4, color='skyblue', alpha=0.8) ax.add_patch(water_drop) highlight = plt.Circle((0.6, 0.6), 0.15, color='white', alpha=0.6) ax.add_patch(highlight) falling_drop = plt.Circle((0.5, 0.9), 0.05, color='skyblue', alpha=0.6) ax.add_patch(falling_drop) plt.xlim(0, 1) plt.ylim(0, 1) fig.savefig(filepath, transparent=True, bbox_inches='tight', pad_inches=0) # 투명 배경, 여백 최소화하여 저장 plt.close(fig) # Figure 닫아서 메모리 관리 # 빨래 상태 확인 및 이미지/텍스트 반환 함수 def 빨래_상태_확인(): url = ccc try: response = requests.get(url) response.raise_for_status() humidity_str = response.text.strip() humidity = float(humidity_str) if humidity <= 55: return SUN_IMAGE_PATH, "빨래가 다 말랐습니다!" # 태양 이미지 경로, 메시지 반환 else: return WATER_DROP_IMAGE_PATH, "아직 빨래가 마르지 않았습니다!" # 물방울 이미지 경로, 메시지 반환 except requests.exceptions.RequestException as e: return None, f"요청 오류 발생: {e}" # 이미지 없음, 오류 메시지 반환 except ValueError: return None, "오류: Blynk 응답이 숫자가 아닙니다." # 이미지 없음, 오류 메시지 반환 # Gradio UI 생성 with gr.Blocks() as demo: gr.Markdown("## 빨래 건조 상태 확인") image_output = gr.Image(interactive=False) # 이미지 출력 컴포넌트 (interactive=False: 편집 불가) text_output = gr.Markdown() # 텍스트 출력 컴포넌트 # 이미지 파일이 없는 경우 생성 (최초 실행 시) if not os.path.exists(SUN_IMAGE_PATH): create_sun_image() if not os.path.exists(WATER_DROP_IMAGE_PATH): create_water_drop_image() demo.load(빨래_상태_확인, outputs=[image_output, text_output]) # 페이지 로딩 시 자동 실행, 이미지와 텍스트 출력 demo.launch()