File size: 4,376 Bytes
1761368
 
b95b531
5b714a0
b95b531
1530a12
1761368
b95b531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1530a12
b95b531
 
 
1530a12
b95b531
 
 
 
e6fa510
ae12180
b95b531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b714a0
b95b531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import json
import gradio as gr
import requests
from datetime import datetime

# =========================
# ⚙️ НАСТРОЙКИ
# =========================
HF_TOKEN = os.getenv("HF_TOKEN") or "hf_your_token_here"

PRIMARY_MODEL = "google/gemma-2-2b-it"
FALLBACK_MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1"
ROUTER_URL = "https://router.huggingface.co"

HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}


# =========================
# 🧩 ПОЛЕЗНЫЕ ФУНКЦИИ
# =========================

def check_token():
    """Проверка валидности токена Hugging Face."""
    try:
        res = requests.get("https://router.huggingface.co/status", headers=HEADERS, timeout=8)
        if res.status_code == 200:
            return True
        else:
            print(f"⚠️ Токен Hugging Face невалиден ({res.status_code})")
            return False
    except Exception as e:
        print(f"Ошибка при проверке токена: {e}")
        return False


def send_request(model: str, prompt: str):
    """Отправка запроса к модели через Router API."""
    payload = {"model": model, "inputs": prompt, "options": {"use_cache": True}}
    try:
        start = time.time()
        response = requests.post(ROUTER_URL, headers=HEADERS, json=payload, timeout=60)
        latency = time.time() - start

        if response.status_code == 200:
            data = response.json()
            # Универсальный парсинг ответа
            if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
                text = data[0]["generated_text"]
            elif isinstance(data, dict) and "generated_text" in data:
                text = data["generated_text"]
            else:
                text = str(data)
            return text.strip(), latency, model, None
        else:
            return None, latency, model, f"Ошибка API {response.status_code}: {response.text}"
    except Exception as e:
        return None, 0, model, str(e)


def generate_text(prompt: str):
    """Основная функция: попытка через основную модель → fallback при ошибке."""
    if not check_token():
        return "❌ Токен Hugging Face недействителен. Проверьте переменную HF_TOKEN."

    # 1️⃣ Основная модель
    output, latency, used_model, error = send_request(PRIMARY_MODEL, prompt)
    if output:
        return render_output(output, used_model, latency, success=True)
    
    # 2️⃣ Fallback при ошибке
    output_fb, latency_fb, model_fb, error_fb = send_request(FALLBACK_MODEL, prompt)
    if output_fb:
        return render_output(output_fb, model_fb, latency_fb, success=True, fallback=True)
    else:
        return f"❌ Ошибка при выполнении запроса:\n- {error}\n- Fallback: {error_fb}"


def render_output(text, model, latency, success=False, fallback=False):
    """Форматированный вывод результата."""
    emoji = "✅" if success else "⚠️"
    fb_text = " (через fallback)" if fallback else ""
    return (
        f"{emoji} **Модель:** `{model}`{fb_text}\n"
        f"⏱ **Время отклика:** {latency:.2f} сек\n\n"
        f"🧠 **Ответ:**\n{text.strip()}"
    )


# =========================
# 🧭 GRADIO UI
# =========================

with gr.Blocks(title="🤖 Eroha AgentAPI v5.7 — Stable Router Edition") as demo:
    gr.Markdown("## 🧠 Eroha AgentAPI v5.7 — Stable Router Edition\n"
                "Поддержка Router API + AutoFallback + Token Validation 🌐")

    with gr.Row():
        prompt = gr.Textbox(label="Введите запрос", placeholder="Например: 'Расскажи историю про ИИ, который научился понимать чувства.'", lines=3)
    output = gr.Markdown(label="Ответ")

    btn = gr.Button("🚀 Отправить", variant="primary")
    btn.click(generate_text, inputs=prompt, outputs=output)

    gr.Markdown("---")
    gr.Markdown("🧩 **Eroha Router Core v5.7** | Автоопределение моделей + безопасный fallback")

demo.launch(server_name="0.0.0.0", server_port=7860)