Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,19 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
from core.intelligence import update_memory, summarize_context
|
| 4 |
from core.selfcheck import evaluate_answer, improve_answer
|
|
|
|
| 5 |
|
| 6 |
HF_API_URL = "https://api-inference.huggingface.co/models/microsoft/phi-3.5-mini"
|
| 7 |
-
HEADERS = {"Authorization": "Bearer hf_your_token"} # если токен не
|
|
|
|
| 8 |
|
| 9 |
def generate_response(user_input):
|
| 10 |
-
"""Основная логика
|
| 11 |
try:
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
| 13 |
payload = {"inputs": user_input, "parameters": {"max_new_tokens": 500, "temperature": 0.7}}
|
| 14 |
response = requests.post(HF_API_URL, headers=HEADERS, json=payload)
|
| 15 |
|
|
@@ -17,40 +22,36 @@ def generate_response(user_input):
|
|
| 17 |
return f"⚠️ Ошибка API ({response.status_code}): {response.text}"
|
| 18 |
|
| 19 |
model_output = response.json()
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
output = improve_answer(output)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
update_memory(user_input,
|
| 28 |
context = summarize_context()
|
| 29 |
|
| 30 |
-
# === Этап 3: Формируем итоговый вывод ===
|
| 31 |
analysis_summary = (
|
| 32 |
f"🧠 Самоанализ ответа: {check['result']}\n"
|
| 33 |
+ ("Причины: " + "; ".join(check["feedback"]) if check["feedback"] else "")
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
f"{output}\n\n"
|
| 38 |
-
f"{analysis_summary}\n\n"
|
| 39 |
-
f"{context}"
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
return final_output
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
return f"❌ Ошибка: {str(e)}"
|
| 46 |
|
| 47 |
|
| 48 |
# === Интерфейс Gradio ===
|
| 49 |
-
with gr.Blocks(title="Eroha AgentAPI v4.
|
| 50 |
-
gr.Markdown("# 🤖 Eroha AgentAPI v4.
|
| 51 |
-
gr.Markdown("
|
| 52 |
|
| 53 |
-
user_input = gr.Textbox(label="Введите вопрос", placeholder="Например:
|
| 54 |
output_box = gr.Textbox(label="Ответ", lines=15)
|
| 55 |
submit_btn = gr.Button("🚀 Сгенерировать ответ")
|
| 56 |
|
|
|
|
| 2 |
import requests
|
| 3 |
from core.intelligence import update_memory, summarize_context
|
| 4 |
from core.selfcheck import evaluate_answer, improve_answer
|
| 5 |
+
from core.learning import analyze_user_input, adapt_answer
|
| 6 |
|
| 7 |
HF_API_URL = "https://api-inference.huggingface.co/models/microsoft/phi-3.5-mini"
|
| 8 |
+
HEADERS = {"Authorization": "Bearer hf_your_token"} # можно удалить, если токен не используется
|
| 9 |
+
|
| 10 |
|
| 11 |
def generate_response(user_input):
|
| 12 |
+
"""Основная логика запроса и самообучения"""
|
| 13 |
try:
|
| 14 |
+
# Анализируем стиль пользователя
|
| 15 |
+
prefs = analyze_user_input(user_input)
|
| 16 |
+
|
| 17 |
+
# Отправляем запрос в модель
|
| 18 |
payload = {"inputs": user_input, "parameters": {"max_new_tokens": 500, "temperature": 0.7}}
|
| 19 |
response = requests.post(HF_API_URL, headers=HEADERS, json=payload)
|
| 20 |
|
|
|
|
| 22 |
return f"⚠️ Ошибка API ({response.status_code}): {response.text}"
|
| 23 |
|
| 24 |
model_output = response.json()
|
| 25 |
+
base_output = model_output[0]["generated_text"] if isinstance(model_output, list) else model_output
|
| 26 |
+
|
| 27 |
+
# Самоанализ и улучшение
|
| 28 |
+
check = evaluate_answer(base_output)
|
| 29 |
+
improved_output = improve_answer(base_output)
|
| 30 |
|
| 31 |
+
# Адаптация под стиль пользователя
|
| 32 |
+
adapted_output = adapt_answer(improved_output)
|
|
|
|
| 33 |
|
| 34 |
+
# Обновляем память
|
| 35 |
+
update_memory(user_input, adapted_output)
|
| 36 |
context = summarize_context()
|
| 37 |
|
|
|
|
| 38 |
analysis_summary = (
|
| 39 |
f"🧠 Самоанализ ответа: {check['result']}\n"
|
| 40 |
+ ("Причины: " + "; ".join(check["feedback"]) if check["feedback"] else "")
|
| 41 |
)
|
| 42 |
|
| 43 |
+
return f"{adapted_output}\n\n{analysis_summary}\n\n{context}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
return f"❌ Ошибка: {str(e)}"
|
| 47 |
|
| 48 |
|
| 49 |
# === Интерфейс Gradio ===
|
| 50 |
+
with gr.Blocks(title="Eroha AgentAPI v4.2 — Adaptive Self-Learning") as demo:
|
| 51 |
+
gr.Markdown("# 🤖 Eroha AgentAPI v4.2 — Самообучающийся Интеллект")
|
| 52 |
+
gr.Markdown("Агент анализирует, адаптируется и запоминает твой стиль общения 🧠")
|
| 53 |
|
| 54 |
+
user_input = gr.Textbox(label="Введите вопрос", placeholder="Например: объясни как школьнику, что такое квант")
|
| 55 |
output_box = gr.Textbox(label="Ответ", lines=15)
|
| 56 |
submit_btn = gr.Button("🚀 Сгенерировать ответ")
|
| 57 |
|