#!/usr/bin/env python3 """ HuggingFace Spaces 极简版本 专门针对HF Spaces的限制进行优化 """ import gradio as gr import requests import json import logging import time # 配置日志 logging.basicConfig(level=logging.INFO) logger = logging.getLogger("toolbaz-hf-lite") # 全局变量 HEALTH_CHECK_URL = "https://www.google.com" # 简单的健康检查 SERVICE_STATUS = "❓ 检查中..." def check_service_status(): """检查服务状态""" global SERVICE_STATUS try: response = requests.get(HEALTH_CHECK_URL, timeout=5) if response.status_code == 200: SERVICE_STATUS = "🟢 网络连接正常" else: SERVICE_STATUS = "🟡 网络连接异常" except Exception as e: SERVICE_STATUS = f"🔴 网络不可用: {str(e)[:50]}" logger.error(f"网络检查失败: {e}") return SERVICE_STATUS def simulate_api_response(message: str, model: str): """模拟API响应(因为真实API可能在HF上无法工作)""" time.sleep(1) # 模拟网络延迟 responses = { "toolbaz-v4.5-fast": f"[模拟响应] toolbaz-v4.5-fast 对 '{message}' 的回复:这是一个模拟的AI响应,因为HuggingFace Spaces可能无法访问外部Toolbaz API。在实际部署中,这里会是真实的AI回复。", "gemini-2.5-flash": f"[模拟响应] Gemini 对 '{message}' 的回复:作为Google的AI模型,我会提供高质量的回答。但这是在HF Spaces上的模拟响应。", "gpt-5": f"[模拟响应] GPT-5 对 '{message}' 的回复:我是先进的AI助手。注意这是模拟响应,真实功能需要在自有服务器上部署。" } return responses.get(model, "未知模型的模拟响应") def chat_fn(message: str, history: list, model: str): """聊天函数""" if not message.strip(): return "", history # 添加用户消息 history.append([message, None]) # 生成响应(模拟或真实) try: # 在HF上优先使用模拟响应 response = simulate_api_response(message, model) # 尝试真实API(可能会失败) try: real_response = requests.post( "http://localhost:8000/v1/chat/completions", headers={"Authorization": "Bearer 1"}, json={"model": model, "messages": [{"role": "user", "content": message}]}, timeout=10 ) if real_response.status_code == 200: response = real_response.json().get("choices", [{}])[0].get("message", {}).get("content", response) except: pass # 使用模拟响应 except Exception as e: response = f"❌ 请求失败: {str(e)}" logger.error(f"聊天失败: {e}") # 更新历史记录 history[-1][1] = response return "", history def create_demo(): """创建Gradio演示界面""" # 预先检查状态 initial_status = check_service_status() with gr.Blocks( title="Toolbaz-2API Lite", theme=gr.themes.Soft(), css=""" .warning-box { background-color: #fff3cd; border: 1px solid #ffeaa7; padding: 15px; border-radius: 5px; margin-bottom: 20px; } .status-box { padding: 10px; border-radius: 5px; margin: 10px 0; text-align: center; font-weight: bold; } """ ) as demo: gr.Markdown(""" # 🤖 Toolbaz-2API on HuggingFace Spaces > **⚠️ 重要提醒**:这是一个适配版本,由于HuggingFace Spaces的限制,可能无法访问真实的Toolbaz API。 > > **🚀 完整功能部署**:推荐使用自己的VPS或云服务器。 """) with gr.Row(): with gr.Column(scale=2): # 警告信息 gr.HTML("""