Download app.py from shegy/k: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/spaces/shegy/k/resolve/main/app.py
- Command line
-
hf download hf://spaces/shegy/k/app.py
-
curl -L -o app.py https://huggingface.co/spaces/shegy/k/resolve/main/app.py
2.56 kB
| import logging | |
| from fastapi import FastAPI | |
| import aiohttp | |
| import asyncio | |
| from fastapi.responses import JSONResponse | |
| from datetime import datetime | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI() | |
| # 定义 URL 列表 | |
| urls = [ | |
| 'https://asfag654-sss.hf.space', | |
| 'https://asfag654-g.hf.space', | |
| 'https://asfag654-kk.hf.space', | |
| 'https://asfag654-z.hf.space', | |
| 'https://asfag654-t.hf.space', | |
| 'https://shegy-k.hf.space', | |
| 'https://shegy-bo.hf.space' | |
| ] | |
| # 初始化状态 | |
| status = {url: {'status_code': None, 'message': ''} for url in urls} | |
| log_messages = [] | |
| lock = asyncio.Lock() | |
| async def probe(): | |
| """ | |
| 定期探测 URL 的可用性并记录状态 | |
| """ | |
| async with aiohttp.ClientSession() as session: | |
| while True: | |
| tasks = [] | |
| for url in urls: | |
| tasks.append(check_url(session, url)) | |
| await asyncio.gather(*tasks) | |
| await asyncio.sleep(13600) # 轮询间隔调整为 4 小时 | |
| async def check_url(session, url): | |
| """ | |
| 异步检查单个 URL 的状态 | |
| """ | |
| global status, log_messages | |
| try: | |
| async with session.get(url) as response: | |
| async with lock: | |
| status[url]['status_code'] = response.status | |
| if response.status == 200: | |
| status[url]['message'] = 'Service is up and running!' | |
| log_message = f"{datetime.now().strftime('%m-%d %H:%M')} - {url} is up." | |
| else: | |
| status[url]['message'] = 'Service is down or returned an error.' | |
| log_message = f"{datetime.now().strftime('%m-%d %H:%M')} - {url} returned {response.status}." | |
| log_messages.append(log_message) | |
| logger.info(log_message) | |
| except Exception as e: | |
| async with lock: | |
| status[url]['message'] = f'An error occurred: {e}' | |
| log_message = f'{datetime.now().isoformat()} - Error checking {url}: {e}' | |
| log_messages.append(log_message) | |
| logger.error(log_message) | |
| async def start_probe(): | |
| """ | |
| FastAPI 启动事件,启动探测任务 | |
| """ | |
| asyncio.create_task(probe()) | |
| async def get_status(): | |
| """ | |
| 获取当前状态和日志 | |
| """ | |
| async with lock: | |
| response_data = [{"time": log.split(" - ")[0], "message": log.split(" - ")[1]} for log in log_messages[-50:]] | |
| return JSONResponse(content=response_data) |