Yermek68 commited on
Commit
c487e9b
·
verified ·
1 Parent(s): b4ca792

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -37
app.py CHANGED
@@ -3,9 +3,9 @@ import time
3
  import gradio as gr
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
6
- from io import BytesIO
7
  import base64
8
- from huggingface_hub import InferenceClient
 
9
 
10
  from core.dashboard import ErohaDashboard
11
  from core.intelligence import update_memory, summarize_context
@@ -19,46 +19,59 @@ from core.model_selector import choose_model
19
  HF_TOKEN = os.getenv("HF_TOKEN", "")
20
  LOG_FILE = "logs/history.csv"
21
  os.makedirs("logs", exist_ok=True)
22
-
23
  dashboard = ErohaDashboard()
24
 
25
  # ==============================
26
- # 🔍 Определение типа подключения
27
  # ==============================
28
- def create_inference_client(model_id):
29
  """
30
- Автоматически выбирает нужный тип API (Router / Direct / Local)
31
  """
32
- # Router API
33
- if "router.huggingface.co" in model_id or model_id.lower() == "router":
34
- print("🔁 Router API Mode активирован")
35
- return InferenceClient(base_url="https://router.huggingface.co", token=HF_TOKEN)
36
-
37
- # Local API
38
- elif model_id.startswith("http://") or model_id.startswith("https://"):
39
- print("🖥️ Local/Custom API Mode активирован:", model_id)
40
- return InferenceClient(base_url=model_id, token=HF_TOKEN)
41
-
42
- # Direct Model
43
- else:
44
- print("⚙️ Direct Model Mode активирован:", model_id)
45
- return InferenceClient(model=model_id, token=HF_TOKEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # ==============================
48
- # 🧠 Генерация ответа
49
  # ==============================
50
  def generate_response(user_input):
51
  try:
52
  start = time.time()
53
  prefs = analyze_user_input(user_input)
54
  model_id = choose_model(user_input)
55
- client = create_inference_client(model_id)
56
 
57
- result = client.text_generation(
58
- user_input,
59
- max_new_tokens=600,
60
- temperature=0.7,
61
- )
62
 
63
  check = evaluate_answer(result)
64
  improved = improve_answer(result)
@@ -82,7 +95,7 @@ def generate_response(user_input):
82
  summary = (
83
  f"🧠 **Модель:** `{model_id}`\n"
84
  f"⚡ **Время отклика:** {response_time} сек\n"
85
- f"🔍 **Качество:** {check['result']}\n"
86
  f"{'; '.join(check['feedback']) if check['feedback'] else '✅ Всё отлично'}\n\n"
87
  f"{context}"
88
  )
@@ -97,11 +110,10 @@ def generate_response(user_input):
97
  # ==============================
98
  def generate_chart():
99
  if not os.path.exists(LOG_FILE):
100
- return "⚠️ Нет данных для отображения."
101
  df = pd.read_csv(LOG_FILE)
102
  if df.empty:
103
  return "⚠️ История пуста."
104
-
105
  df["time"] = pd.to_datetime(df["time"])
106
  df = df.tail(50)
107
 
@@ -109,9 +121,8 @@ def generate_chart():
109
  plt.plot(df["time"], df["response_time"], marker="o", linewidth=1.5)
110
  plt.title("⚡ Скорость отклика моделей (последние 50 запросов)")
111
  plt.xlabel("Время")
112
- plt.ylabel("Время отклика (сек)")
113
  plt.grid(True)
114
-
115
  buffer = BytesIO()
116
  plt.savefig(buffer, format="png", bbox_inches="tight")
117
  buffer.seek(0)
@@ -125,14 +136,14 @@ def show_dashboard():
125
  return metrics_text, df, chart_html
126
 
127
  # ==============================
128
- # 🎨 Gradio Interface
129
  # ==============================
130
- with gr.Blocks(title="Eroha AgentAPI v5.5Adaptive AutoRouter", theme="soft") as app:
131
- gr.Markdown("# 🤖 Eroha AgentAPI v5.5 — Guru Edition (AutoRouter Adaptive Core)")
132
- gr.Markdown("**Интеллект + самообучение + аналитика + автоопределение API** 🧩")
133
 
134
  with gr.Tab("💬 Agent Chat"):
135
- user_input = gr.Textbox(label="Введите запрос", placeholder="Например: объясни, как работает нейронная сеть, используя метафоры.")
136
  output_box = gr.Textbox(label="Ответ", lines=15)
137
  submit_btn = gr.Button("🚀 Отправить")
138
  submit_btn.click(fn=generate_response, inputs=user_input, outputs=output_box)
 
3
  import gradio as gr
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
 
6
  import base64
7
+ from io import BytesIO
8
+ import requests
9
 
10
  from core.dashboard import ErohaDashboard
11
  from core.intelligence import update_memory, summarize_context
 
19
  HF_TOKEN = os.getenv("HF_TOKEN", "")
20
  LOG_FILE = "logs/history.csv"
21
  os.makedirs("logs", exist_ok=True)
 
22
  dashboard = ErohaDashboard()
23
 
24
  # ==============================
25
+ # 🔁 Автоматический Router с Failover
26
  # ==============================
27
+ def query_huggingface_router(prompt, model_id):
28
  """
29
+ Универсальный запрос к Router API с fallback на прямую модель.
30
  """
31
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
32
+ payload = {
33
+ "inputs": prompt,
34
+ "parameters": {"max_new_tokens": 600, "temperature": 0.7},
35
+ }
36
+
37
+ try:
38
+ # 1️⃣ Основной Router API
39
+ response = requests.post(
40
+ "https://router.huggingface.co/models/" + model_id,
41
+ headers=headers,
42
+ json=payload,
43
+ timeout=30,
44
+ )
45
+ if response.status_code == 200:
46
+ return response.json()[0]["generated_text"]
47
+ elif response.status_code == 410:
48
+ raise RuntimeError("Old endpoint rejected (410)")
49
+ else:
50
+ raise RuntimeError(f"Router error: {response.status_code}")
51
+
52
+ except Exception as e:
53
+ print("⚠️ Router недоступен, выполняется fallback:", str(e))
54
+ # 2️⃣ Fallback: прямое обращение к конкретной модели
55
+ fallback_url = f"https://huggingface.co/api/models/{model_id}"
56
+ try:
57
+ response = requests.post(fallback_url, headers=headers, json=payload, timeout=30)
58
+ if response.status_code == 200:
59
+ return response.json()[0]["generated_text"]
60
+ else:
61
+ raise RuntimeError(f"Fallback error: {response.status_code}")
62
+ except Exception as e2:
63
+ return f"❌ Ошибка при обращении к API: {str(e2)}"
64
 
65
  # ==============================
66
+ # 🧠 Основная логика
67
  # ==============================
68
  def generate_response(user_input):
69
  try:
70
  start = time.time()
71
  prefs = analyze_user_input(user_input)
72
  model_id = choose_model(user_input)
 
73
 
74
+ result = query_huggingface_router(user_input, model_id)
 
 
 
 
75
 
76
  check = evaluate_answer(result)
77
  improved = improve_answer(result)
 
95
  summary = (
96
  f"🧠 **Модель:** `{model_id}`\n"
97
  f"⚡ **Время отклика:** {response_time} сек\n"
98
+ f"🔍 **Проверка качества:** {check['result']}\n"
99
  f"{'; '.join(check['feedback']) if check['feedback'] else '✅ Всё отлично'}\n\n"
100
  f"{context}"
101
  )
 
110
  # ==============================
111
  def generate_chart():
112
  if not os.path.exists(LOG_FILE):
113
+ return "⚠️ Нет данных для графика."
114
  df = pd.read_csv(LOG_FILE)
115
  if df.empty:
116
  return "⚠️ История пуста."
 
117
  df["time"] = pd.to_datetime(df["time"])
118
  df = df.tail(50)
119
 
 
121
  plt.plot(df["time"], df["response_time"], marker="o", linewidth=1.5)
122
  plt.title("⚡ Скорость отклика моделей (последние 50 запросов)")
123
  plt.xlabel("Время")
124
+ plt.ylabel("Секунды")
125
  plt.grid(True)
 
126
  buffer = BytesIO()
127
  plt.savefig(buffer, format="png", bbox_inches="tight")
128
  buffer.seek(0)
 
136
  return metrics_text, df, chart_html
137
 
138
  # ==============================
139
+ # 🎨 Интерфейс Gradio
140
  # ==============================
141
+ with gr.Blocks(title="Eroha AgentAPI v5.6AutoRouter Failover Core", theme="soft") as app:
142
+ gr.Markdown("# 🤖 Eroha AgentAPI v5.6 — Guru Edition (AutoRouter + Failover Core)")
143
+ gr.Markdown("**Автоматический Router + fallback + визуальная аналитика** ⚙️")
144
 
145
  with gr.Tab("💬 Agent Chat"):
146
+ user_input = gr.Textbox(label="Введите запрос", placeholder="Например: придумай философскую сказку об ИИ.")
147
  output_box = gr.Textbox(label="Ответ", lines=15)
148
  submit_btn = gr.Button("🚀 Отправить")
149
  submit_btn.click(fn=generate_response, inputs=user_input, outputs=output_box)