Spaces:
Runtime error
Runtime error
File size: 3,731 Bytes
5b714a0 2a95ad2 ae12180 2a95ad2 856a7f0 677740e 6539c45 3f976bd 6539c45 677740e ae12180 2a95ad2 e6fa510 ae12180 6539c45 677740e 71c9eb6 677740e 6539c45 71c9eb6 5b714a0 2a95ad2 697670a 71c9eb6 6539c45 677740e 6539c45 677740e 71c9eb6 e6fa510 6539c45 71c9eb6 856a7f0 6539c45 71c9eb6 2a95ad2 e6fa510 6539c45 ae12180 6539c45 71c9eb6 ae12180 71c9eb6 856a7f0 71c9eb6 5b714a0 2a95ad2 697670a ac8fce8 ae12180 6539c45 ae12180 6539c45 ae12180 6539c45 ae12180 8f26786 ae12180 5b8affe ae12180 8f26786 6539c45 |
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 |
import gradio as gr
import requests
import time
from core.dashboard import ErohaDashboard
from core.intelligence import update_memory, summarize_context
from core.selfcheck import evaluate_answer, improve_answer
from core.learning import analyze_user_input, adapt_answer
from core.model_selector import choose_model
# 🔐 Токен Hugging Face (если не нужен — просто удали строку)
HEADERS = {"Authorization": "Bearer hf_your_token"}
dashboard = ErohaDashboard()
def generate_response(user_input):
try:
start = time.time()
# 1️⃣ Определение модели
prefs = analyze_user_input(user_input)
model_id = choose_model(user_input)
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
# 2️⃣ Запрос к модели
payload = {"inputs": user_input, "parameters": {"max_new_tokens": 600, "temperature": 0.7}}
response = requests.post(api_url, headers=HEADERS, json=payload)
if response.status_code != 200:
return f"⚠️ Ошибка API ({response.status_code}): {response.text}"
result = response.json()
base_output = result[0]["generated_text"] if isinstance(result, list) else str(result)
# 3️⃣ Самоанализ и улучшение
check = evaluate_answer(base_output)
improved = improve_answer(base_output)
# 4️⃣ Адаптация под стиль пользователя
personalized = adapt_answer(improved)
# 5️⃣ Память и контекст
update_memory(user_input, personalized)
context = summarize_context()
# 6️⃣ Метрики и дашборд
response_time = round(time.time() - start, 2)
dashboard.log_request(model_id, prefs["category"], response_time)
# 7️⃣ Формирование результата
summary = (
f"🧠 Модель: `{model_id}`\n"
f"🧩 Тип запроса: {prefs['category']}\n"
f"⚡ Время отклика: {response_time} сек\n"
f"🔍 Самоанализ: {check['result']}\n"
f"{'; '.join(check['feedback']) if check['feedback'] else '✅ Всё отлично'}\n\n"
f"{context}"
)
return f"{personalized}\n\n{summary}"
except Exception as e:
return f"❌ Ошибка: {str(e)}"
def show_dashboard():
metrics_text, df = dashboard.dashboard_ui()
return metrics_text, df
# === Интерфейс ===
with gr.Blocks(title="Eroha AgentAPI v5.0 — Auto Model Switch", theme="soft") as app:
gr.Markdown("# 🤖 Eroha AgentAPI v5.0 — Guru Edition")
gr.Markdown("**Автоматический интеллект + самообучение + аналитика 🧠**")
with gr.Tab("💬 Agent Chat"):
user_input = gr.Textbox(
label="Введите запрос",
placeholder="Например: Объясни квантовую запутанность или напиши стих...",
)
output_box = gr.Textbox(label="Ответ", lines=15)
submit_btn = gr.Button("🚀 Отправить")
submit_btn.click(fn=generate_response, inputs=user_input, outputs=output_box)
with gr.Tab("📊 Dashboard"):
metrics = gr.Markdown(label="Общая статистика")
log_table = gr.Dataframe(headers=["time", "model", "type", "response_time"], label="История")
refresh = gr.Button("🔄 Обновить")
refresh.click(show_dashboard, outputs=[metrics, log_table])
app.launch(server_name="0.0.0.0", server_port=7860)
|