File size: 3,578 Bytes
a00224f d387fc4 a00224f 8ced811 d387fc4 a00224f 8ced811 a00224f d387fc4 a00224f d387fc4 a00224f d387fc4 a00224f d387fc4 a00224f d387fc4 a00224f d387fc4 a00224f d387fc4 a00224f |
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 |
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() |