yoon2566 commited on
Commit
d387fc4
Β·
verified Β·
1 Parent(s): a00224f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -11
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() # HTTP μ—λŸ¬ λ°œμƒ μ‹œ μ˜ˆμ™Έ 처리
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
- output_text = gr.Markdown() # κ²°κ³Ό ν…μŠ€νŠΈλ₯Ό ν‘œμ‹œν•  Markdown μ»΄ν¬λ„ŒνŠΈ
25
- check_button = gr.Button("빨래 μƒνƒœ 확인") # μƒνƒœ 확인 λ²„νŠΌ
26
- check_button.click(fn=빨래_μƒνƒœ_확인, outputs=output_text) # λ²„νŠΌ 클릭 μ‹œ 빨래_μƒνƒœ_확인 ν•¨μˆ˜ 호좜 및 κ²°κ³Ό 좜λ ₯
 
 
 
 
 
 
 
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()