Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,91 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def λΉ¨λ_μν_νμΈ():
|
| 5 |
url = 'https://blynk.cloud/external/api/get?token=F44y9xVfUZZXCqxDGzHpYzGC7ih8yAdI&V0'
|
| 6 |
try:
|
| 7 |
response = requests.get(url)
|
| 8 |
-
response.raise_for_status()
|
| 9 |
-
humidity_str = response.text.strip()
|
| 10 |
-
humidity = float(humidity_str)
|
| 11 |
|
| 12 |
if humidity <= 55:
|
| 13 |
-
return "
|
| 14 |
else:
|
| 15 |
-
return "
|
| 16 |
|
| 17 |
except requests.exceptions.RequestException as e:
|
| 18 |
-
return f"μμ² μ€λ₯ λ°μ: {e}" #
|
| 19 |
except ValueError:
|
| 20 |
-
return "μ€λ₯: Blynk μλ΅μ΄ μ«μκ° μλλλ€." #
|
| 21 |
|
|
|
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
-
gr.Markdown("
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os # νμΌ μ‘΄μ¬ νμΈμ μν΄ import
|
| 6 |
|
| 7 |
+
# μ΄λ―Έμ§ νμΌ μ μ₯ κ²½λ‘
|
| 8 |
+
SUN_IMAGE_PATH = "sun.png"
|
| 9 |
+
WATER_DROP_IMAGE_PATH = "water_drop.png"
|
| 10 |
+
|
| 11 |
+
# νμ μ΄λ―Έμ§ μμ± ν¨μ
|
| 12 |
+
def create_sun_image(filepath=SUN_IMAGE_PATH):
|
| 13 |
+
fig, ax = plt.subplots(figsize=(6, 6), facecolor='none') # λ°°κ²½ ν¬λͺ
νκ²
|
| 14 |
+
ax.set_aspect('equal')
|
| 15 |
+
ax.axis('off')
|
| 16 |
+
|
| 17 |
+
sun_circle = plt.Circle((0, 0), radius=0.8, color='yellow', edgecolor='black')
|
| 18 |
+
ax.add_patch(sun_circle)
|
| 19 |
+
|
| 20 |
+
num_rays = 20
|
| 21 |
+
ray_length = 0.5
|
| 22 |
+
|
| 23 |
+
for i in range(num_rays):
|
| 24 |
+
angle = 2 * np.pi * i / num_rays
|
| 25 |
+
x_start = 0.8 * np.cos(angle)
|
| 26 |
+
y_start = 0.8 * np.sin(angle)
|
| 27 |
+
x_end = (0.8 + ray_length) * np.cos(angle)
|
| 28 |
+
y_end = (0.8 + ray_length) * np.sin(angle)
|
| 29 |
+
ax.plot([x_start, x_end], [y_start, y_end], color='yellow', linewidth=2)
|
| 30 |
+
|
| 31 |
+
ax.set_xlim([-1.5, 1.5])
|
| 32 |
+
ax.set_ylim([-1.5, 1.5])
|
| 33 |
+
|
| 34 |
+
fig.savefig(filepath, transparent=True, bbox_inches='tight', pad_inches=0) # ν¬λͺ
λ°°κ²½, μ¬λ°± μ΅μννμ¬ μ μ₯
|
| 35 |
+
plt.close(fig) # Figure λ«μμ λ©λͺ¨λ¦¬ κ΄λ¦¬
|
| 36 |
+
|
| 37 |
+
# λ¬Όλ°©μΈ μ΄λ―Έμ§ μμ± ν¨μ
|
| 38 |
+
def create_water_drop_image(filepath=WATER_DROP_IMAGE_PATH):
|
| 39 |
+
fig, ax = plt.subplots(figsize=(5, 5), facecolor='none') # λ°°κ²½ ν¬λͺ
νκ²
|
| 40 |
+
ax.set_aspect('equal')
|
| 41 |
+
ax.axis('off')
|
| 42 |
+
|
| 43 |
+
water_drop = plt.Circle((0.5, 0.5), 0.4, color='skyblue', alpha=0.8)
|
| 44 |
+
ax.add_patch(water_drop)
|
| 45 |
+
|
| 46 |
+
highlight = plt.Circle((0.6, 0.6), 0.15, color='white', alpha=0.6)
|
| 47 |
+
ax.add_patch(highlight)
|
| 48 |
+
|
| 49 |
+
falling_drop = plt.Circle((0.5, 0.9), 0.05, color='skyblue', alpha=0.6)
|
| 50 |
+
ax.add_patch(falling_drop)
|
| 51 |
+
|
| 52 |
+
plt.xlim(0, 1)
|
| 53 |
+
plt.ylim(0, 1)
|
| 54 |
+
|
| 55 |
+
fig.savefig(filepath, transparent=True, bbox_inches='tight', pad_inches=0) # ν¬λͺ
λ°°κ²½, μ¬λ°± μ΅μννμ¬ μ μ₯
|
| 56 |
+
plt.close(fig) # Figure λ«μμ λ©λͺ¨λ¦¬ κ΄λ¦¬
|
| 57 |
+
|
| 58 |
+
# λΉ¨λ μν νμΈ λ° μ΄λ―Έμ§/ν
μ€νΈ λ°ν ν¨μ
|
| 59 |
def λΉ¨λ_μν_νμΈ():
|
| 60 |
url = 'https://blynk.cloud/external/api/get?token=F44y9xVfUZZXCqxDGzHpYzGC7ih8yAdI&V0'
|
| 61 |
try:
|
| 62 |
response = requests.get(url)
|
| 63 |
+
response.raise_for_status()
|
| 64 |
+
humidity_str = response.text.strip()
|
| 65 |
+
humidity = float(humidity_str)
|
| 66 |
|
| 67 |
if humidity <= 55:
|
| 68 |
+
return SUN_IMAGE_PATH, "<font size='6'><b>λΉ¨λκ° λ€ λ§λμ΅λλ€!</b></font>" # νμ μ΄λ―Έμ§ κ²½λ‘, λ©μμ§ λ°ν
|
| 69 |
else:
|
| 70 |
+
return WATER_DROP_IMAGE_PATH, "<font size='6' color='red'><b>μμ§ λΉ¨λκ° λ§λ₯΄μ§ μμμ΅λλ€!</b></font>" # λ¬Όλ°©μΈ μ΄λ―Έμ§ κ²½λ‘, λ©μμ§ λ°ν
|
| 71 |
|
| 72 |
except requests.exceptions.RequestException as e:
|
| 73 |
+
return None, f"μμ² μ€λ₯ λ°μ: {e}" # μ΄λ―Έμ§ μμ, μ€λ₯ λ©μμ§ λ°ν
|
| 74 |
except ValueError:
|
| 75 |
+
return None, "μ€λ₯: Blynk μλ΅μ΄ μ«μκ° μλλλ€." # μ΄λ―Έμ§ μμ, μ€λ₯ λ©μμ§ λ°ν
|
| 76 |
|
| 77 |
+
# Gradio UI μμ±
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown("## λΉ¨λ 건쑰 μν νμΈ")
|
| 80 |
+
image_output = gr.Image(interactive=False) # μ΄λ―Έμ§ μΆλ ₯ μ»΄ν¬λνΈ (interactive=False: νΈμ§ λΆκ°)
|
| 81 |
+
text_output = gr.Markdown() # ν
μ€νΈ μΆλ ₯ μ»΄ν¬λνΈ
|
| 82 |
+
|
| 83 |
+
# μ΄λ―Έμ§ νμΌμ΄ μλ κ²½μ° μμ± (μ΅μ΄ μ€ν μ)
|
| 84 |
+
if not os.path.exists(SUN_IMAGE_PATH):
|
| 85 |
+
create_sun_image()
|
| 86 |
+
if not os.path.exists(WATER_DROP_IMAGE_PATH):
|
| 87 |
+
create_water_drop_image()
|
| 88 |
+
|
| 89 |
+
demo.load(λΉ¨λ_μν_νμΈ, outputs=[image_output, text_output]) # νμ΄μ§ λ‘λ© μ μλ μ€ν, μ΄λ―Έμ§μ ν
μ€νΈ μΆλ ₯
|
| 90 |
|
| 91 |
demo.launch()
|