Spaces:
Build error
Build error
File size: 10,365 Bytes
14f6b4f |
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
"""
HuggingFace Spaces 监控系统使用示例
演示如何使用监控系统的各种功能
"""
import asyncio
import logging
import os
from datetime import datetime
from typing import List
from config import ConfigManager, setup_logging, create_sample_config
from data_models import SpaceStatus, EventType, AlertLevel, AlertRule
from huggingface_client_v2 import HuggingFaceClient, RetryClient, WebhookHandler
from monitor_engine import MonitorEngine, HealthChecker
async def example_basic_monitoring():
基础监控示例
print("=" * 50)
print("基础监控示例")
print("=" * 50)
client = RetryClient(HuggingFaceClient())
try:
space_status = await client.get_space_status("meta-llama/Llama-2-7b-chat-hf")
print(f"Space 状态: {space_status.status.value}")
print(f"运行时阶段: {space_status.runtime.stage}")
print(f"运行时状态: {space_status.runtime.state}")
logs = await client.get_space_logs("meta-llama/Llama-2-7b-chat-hf", lines=10)
print(f"获取到 {len(logs.entries)} 条日志")
except Exception as e:
print(f"监控失败: {e}")
finally:
await client.client.close()
async def example_monitor_engine():
监控引擎示例
print("=" * 50)
print("监控引擎示例")
print("=" * 50)
engine = MonitorEngine()
def on_status_change(event):
print(f"状态变化事件: {event.space_id} - {event.message}")
def on_error(event):
print(f"错误事件: {event.space_id} - {event.message}")
engine.register_event_callback(EventType.STATUS_CHANGE, on_status_change)
engine.register_event_callback(EventType.ERROR_DETECTED, on_error)
try:
await engine.start()
await engine.add_space("meta-llama/Llama-2-7b-chat-hf")
alert_rule = AlertRule(
name="连续错误告警",
description="当 Space 连续 3 次检查失败时触发告警",
condition={"consecutive_errors": 3},
severity=AlertLevel.HIGH,
cooldown_minutes=30
)
await engine.add_alert_rule(alert_rule)
print("监控运行中,等待 30 秒...")
await asyncio.sleep(30)
stats = await engine.get_stats()
print(f"监控统计: {stats}")
except Exception as e:
print(f"监控引擎异常: {e}")
finally:
await engine.stop()
async def example_webhook_handling():
Webhook 处理示例
print("=" * 50)
print("Webhook 处理示例")
print("=" * 50)
client = HuggingFaceClient()
handler = WebhookHandler(client, secret="test-secret")
sample_webhook = {
"event": "space.status_updated",
"space": {
"id": "test-space",
"runtime": {
"stage": "RUNNING",
"state": "RUNNING"
}
}
}
headers = {
"X-Hub-Signature-256": "sha256=fake-signature"
}
try:
event = await handler.handle_webhook(sample_webhook, headers)
print(f"Webhook 事件处理完成: {event.event_type.value}")
except Exception as e:
print(f"Webhook 处理失败: {e}")
finally:
await client.close()
async def example_search_spaces():
搜索 Spaces 示例
print("=" * 50)
print("搜索 Spaces 示例")
print("=" * 50)
client = HuggingFaceClient()
try:
spaces = await client.search_spaces("text-generation", limit=5)
print(f"找到 {len(spaces)} 个 Spaces:")
for space in spaces:
print(f" - {space.space_id} by {space.author}")
print(f" SDK: {space.sdk}")
print(f" 描述: {space.description[:100]}...")
print()
except Exception as e:
print(f"搜索失败: {e}")
finally:
await client.close()
async def example_user_spaces():
获取用户 Spaces 示例
print("=" * 50)
print("获取用户 Spaces 示例")
print("=" * 50)
client = HuggingFaceClient()
try:
spaces = await client.get_user_spaces()
print(f"用户有 {len(spaces)} 个 Spaces:")
for space in spaces[:10]:
print(f" - {space.space_id}")
print(f" 状态: {space.last_modified}")
print()
except Exception as e:
print(f"获取用户 Spaces 失败: {e}")
finally:
await client.close()
async def example_health_check():
健康检查示例
print("=" * 50)
print("健康检查示例")
print("=" * 50)
engine = MonitorEngine()
health_checker = HealthChecker(engine)
try:
health_status = await health_checker.check_health()
print("健康检查结果:")
print(f" 总体状态: {health_status['status']}")
for check_name, check_result in health_status['checks'].items():
print(f" {check_name}: {check_result['status']}")
if 'details' in check_result:
print(f" 详情: {check_result['details']}")
except Exception as e:
print(f"健康检查失败: {e}")
async def example_configuration():
配置管理示例
print("=" * 50)
print("配置管理示例")
print("=" * 50)
print("1. 创建示例配置文件...")
create_sample_config("example_config.json")
print("2. 加载配置...")
config_manager = ConfigManager("example_config.json")
config = config_manager.get_config()
print(f"API 配置: {config.api.base_url}")
print(f"监控间隔: {config.monitoring.default_check_interval} 秒")
print(f"日志级别: {config.logging.level}")
print("3. 验证配置...")
errors = config_manager.validate_config()
if errors:
print("配置错误:")
for error in errors:
print(f" - {error}")
else:
print("配置验证通过")
print("4. 设置日志...")
setup_logging(config.logging)
logger = logging.getLogger(__name__)
logger.info("日志系统已初始化")
async def example_batch_monitoring():
批量监控示例
print("=" * 50)
print("批量监控示例")
print("=" * 50)
engine = MonitorEngine()
space_ids = [
"meta-llama/Llama-2-7b-chat-hf",
"stabilityai/stable-diffusion",
"microsoft/DialoGPT-medium"
]
try:
await engine.start()
print(f"添加 {len(space_ids)} 个 Spaces 到监控列表...")
for space_id in space_ids:
try:
await engine.add_space(space_id)
print(f" ✓ {space_id}")
except Exception as e:
print(f" ✗ {space_id}: {e}")
print("监控运行 60 秒...")
await asyncio.sleep(60)
monitored_spaces = await engine.get_monitored_spaces()
print(f"当前监控的 Spaces: {monitored_spaces}")
for space_id in monitored_spaces:
events = await engine.get_space_events(space_id, limit=5)
print(f"{space_id}: {len(events)} 个事件")
for event in events:
print(f" - {event.event_type.value}: {event.message}")
except Exception as e:
print(f"批量监控异常: {e}")
finally:
await engine.stop()
async def example_advanced_features():
高级功能示例
print("=" * 50)
print("高级功能示例")
print("=" * 50)
engine = MonitorEngine()
async def advanced_event_handler(event):
print(f"高级事件处理器: {event.event_type.value} - {event.space_id}")
if event.event_type == EventType.ERROR_DETECTED:
print(" 检测到错误,可以执行自动修复逻辑")
elif event.event_type == EventType.SPACE_STARTED:
print(" Space 启动,可以发送通知")
engine.register_event_callback(EventType.ERROR_DETECTED, advanced_event_handler)
engine.register_event_callback(EventType.SPACE_STARTED, advanced_event_handler)
try:
await engine.start()
await engine.add_space("meta-llama/Llama-2-7b-chat-hf")
custom_alert_rule = AlertRule(
name="自定义状态变化告警",
description="当 Space 从运行状态变为错误状态时触发",
condition={
"event_type": "status_change",
"from_status": "running",
"to_status": "error"
},
severity=AlertLevel.MEDIUM,
cooldown_minutes=15
)
await engine.add_alert_rule(custom_alert_rule)
print("运行高级功能演示 45 秒...")
await asyncio.sleep(45)
await engine.pause_monitoring()
print("监控已暂停 10 秒...")
await asyncio.sleep(10)
await engine.resume_monitoring()
print("监控已恢复")
await asyncio.sleep(10)
except Exception as e:
print(f"高级功能演示异常: {e}")
finally:
await engine.stop()
async def main():
print("HuggingFace Spaces 监控系统 - 使用示例")
print("=" * 60)
if not os.getenv("HF_TOKEN"):
print("警告: 未设置 HF_TOKEN 环境变量")
print("请设置有效的 HuggingFace 访问令牌以运行完整示例")
print()
examples = [
("配置管理", example_configuration),
("基础监控", example_basic_monitoring),
("搜索 Spaces", example_search_spaces),
("Webhook 处理", example_webhook_handling),
("健康检查", example_health_check),
("监控引擎", example_monitor_engine),
("批量监控", example_batch_monitoring),
("高级功能", example_advanced_features),
]
for name, example_func in examples:
print(f"\n运行示例: {name}")
try:
await example_func()
except Exception as e:
print(f"示例 {name} 执行失败: {e}")
print()
if __name__ == "__main__":
asyncio.run(main()) |