seawolf2357 commited on
Commit
14f460f
ยท
verified ยท
1 Parent(s): 843229e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,29 +1,34 @@
1
  import gradio as gr
2
- import requests
3
- import threading
4
- import time
5
 
6
- # ์™ธ๋ถ€ URL์— ์ ‘์†ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
7
- def check_connection(url):
8
  try:
9
- response = requests.get(url)
10
- status = f"URL: {url} ์ƒํƒœ ์ฝ”๋“œ: {response.status_code}, ์ ‘์† ์ƒํƒœ: {'์ ‘์† ์„ฑ๊ณต' if response.status_code == 200 else '์ ‘์† ์‹คํŒจ'}"
 
 
 
11
  except Exception as e:
12
  status = f"URL: {url} ์ ‘์† ์‹คํŒจ: {str(e)}"
13
- print(status)
14
- return status
15
 
16
- # ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ•ด์ง„ ์ฃผ๊ธฐ๋กœ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
17
- def start_timer(url, interval):
18
- threading.Timer(interval, start_timer, [url, interval]).start()
19
- check_connection(url)
 
 
 
 
20
 
21
  # ํƒ€์ด๋จธ ์‹œ์ž‘ ํ•จ์ˆ˜๋ฅผ Gradio์˜ ์ž…๋ ฅ๊ณผ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
22
  def setup_timer(interval, *urls):
23
  interval_seconds = interval * 60 # ๋ถ„์„ ์ดˆ๋กœ ๋ณ€ํ™˜
24
- for url in urls:
25
- if url: # URL์ด ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ํƒ€์ด๋จธ ์‹œ์ž‘
26
- start_timer(url, interval_seconds)
27
  return "ํƒ€์ด๋จธ๊ฐ€ ์„ค์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
28
 
29
  # Gradio UI ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
@@ -79,9 +84,11 @@ app = gr.Interface(
79
  "https://seawolf2357-kai-commu-1253181449008320553.hf.space",
80
  "https://seawolf2357-kai-commu-1253181487818211348.hf.space",
81
  "https://seawolf2357-kai-commu-1253181517593837568.hf.space",
82
- "https://seawolf2357-kai-commu-1253181557233946665.hf.space"
83
  ]
84
  ],
85
  cache_examples=False # ์บ์‹œ ๋น„ํ™œ์„ฑํ™”
86
  )
87
- app.launch()
 
 
 
1
  import gradio as gr
2
+ import aiohttp
3
+ import asyncio
 
4
 
5
+ # ๋น„๋™๊ธฐ์ ์œผ๋กœ ์™ธ๋ถ€ URL์— ์ ‘์†ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
6
+ async def check_connection(url):
7
  try:
8
+ async with aiohttp.ClientSession() as session:
9
+ async with session.get(url) as response:
10
+ status = f"URL: {url} ์ƒํƒœ ์ฝ”๋“œ: {response.status}, ์ ‘์† ์ƒํƒœ: {'์ ‘์† ์„ฑ๊ณต' if response.status == 200 else '์ ‘์† ์‹คํŒจ'}"
11
+ print(status)
12
+ return status
13
  except Exception as e:
14
  status = f"URL: {url} ์ ‘์† ์‹คํŒจ: {str(e)}"
15
+ print(status)
16
+ return status
17
 
18
+ # ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ•ด์ง„ ์ฃผ๊ธฐ๋กœ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
19
+ async def start_timer(urls, interval):
20
+ while True:
21
+ for url in urls:
22
+ if url: # URL์ด ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์ฒดํฌ
23
+ await check_connection(url)
24
+ await asyncio.sleep(1) # ๊ฐ URL ์ฒดํฌ ํ›„ 1์ดˆ ๋Œ€๊ธฐ
25
+ await asyncio.sleep(interval - len(urls)) # ๋ชจ๋“  URL ์ฒดํฌ ํ›„ interval์—์„œ ์ด๋ฏธ ์†Œ๋น„๋œ ์‹œ๊ฐ„์„ ๋บ€ ๋งŒํผ ๋Œ€๊ธฐ
26
 
27
  # ํƒ€์ด๋จธ ์‹œ์ž‘ ํ•จ์ˆ˜๋ฅผ Gradio์˜ ์ž…๋ ฅ๊ณผ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
28
  def setup_timer(interval, *urls):
29
  interval_seconds = interval * 60 # ๋ถ„์„ ์ดˆ๋กœ ๋ณ€ํ™˜
30
+ loop = asyncio.get_event_loop()
31
+ loop.create_task(start_timer(urls, interval_seconds))
 
32
  return "ํƒ€์ด๋จธ๊ฐ€ ์„ค์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
33
 
34
  # Gradio UI ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
 
84
  "https://seawolf2357-kai-commu-1253181449008320553.hf.space",
85
  "https://seawolf2357-kai-commu-1253181487818211348.hf.space",
86
  "https://seawolf2357-kai-commu-1253181517593837568.hf.space",
87
+ "https://seawolf2357-kai-commu-1253181557233946665.hf.space"
88
  ]
89
  ],
90
  cache_examples=False # ์บ์‹œ ๋น„ํ™œ์„ฑํ™”
91
  )
92
+
93
+ if __name__ == "__main__":
94
+ app.launch()