requests2 / app.py
yoon2566's picture
Update app.py
8ced811 verified
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, "<font size='6'><b>๋นจ๋ž˜๊ฐ€ ๋‹ค ๋ง๋ž์Šต๋‹ˆ๋‹ค!</b></font>" # ํƒœ์–‘ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ, ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
else:
return WATER_DROP_IMAGE_PATH, "<font size='6' color='red'><b>์•„์ง ๋นจ๋ž˜๊ฐ€ ๋งˆ๋ฅด์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค!</b></font>" # ๋ฌผ๋ฐฉ์šธ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ, ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
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()