Spaces:
Sleeping
Sleeping
Commit
·
56751ff
1
Parent(s):
efd54b4
new index
Browse files- app.py +49 -10
- index_retriever.py +2 -8
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import sys
|
|
| 4 |
import logging
|
| 5 |
from config import *
|
| 6 |
from documents_prep import DocumentsPreparation
|
| 7 |
-
|
| 8 |
from chat_handler import ChatHandler
|
| 9 |
|
| 10 |
REPO_ID = "MrSimple01/AIEXP_RAG_FILES"
|
|
@@ -14,7 +14,6 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
doc_prep = None
|
| 17 |
-
index_retriever = None
|
| 18 |
chat_handler = None
|
| 19 |
|
| 20 |
def log_message(message):
|
|
@@ -42,7 +41,7 @@ def initialize_system():
|
|
| 42 |
log_message("Не удалось инициализировать модели")
|
| 43 |
return False
|
| 44 |
|
| 45 |
-
chat_handler = ChatHandler(
|
| 46 |
|
| 47 |
log_message("Система успешно инициализирована")
|
| 48 |
return True
|
|
@@ -54,17 +53,57 @@ def initialize_system():
|
|
| 54 |
def handle_question(question):
|
| 55 |
if chat_handler is None:
|
| 56 |
return "Система не инициализирована", ""
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def handle_model_switch(model_name):
|
| 60 |
-
|
| 61 |
-
return
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def get_current_model_status():
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def get_chat_history_html():
|
| 70 |
if chat_handler is None:
|
|
|
|
| 4 |
import logging
|
| 5 |
from config import *
|
| 6 |
from documents_prep import DocumentsPreparation
|
| 7 |
+
import index_retriever
|
| 8 |
from chat_handler import ChatHandler
|
| 9 |
|
| 10 |
REPO_ID = "MrSimple01/AIEXP_RAG_FILES"
|
|
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
doc_prep = None
|
|
|
|
| 17 |
chat_handler = None
|
| 18 |
|
| 19 |
def log_message(message):
|
|
|
|
| 41 |
log_message("Не удалось инициализировать модели")
|
| 42 |
return False
|
| 43 |
|
| 44 |
+
chat_handler = ChatHandler(None)
|
| 45 |
|
| 46 |
log_message("Система успешно инициализирована")
|
| 47 |
return True
|
|
|
|
| 53 |
def handle_question(question):
|
| 54 |
if chat_handler is None:
|
| 55 |
return "Система не инициализирована", ""
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
answer = index_retriever.query(question)
|
| 59 |
+
sources = get_sources_for_question(question)
|
| 60 |
+
|
| 61 |
+
chat_handler.add_to_history(question, answer)
|
| 62 |
+
|
| 63 |
+
return answer, sources
|
| 64 |
+
except Exception as e:
|
| 65 |
+
error_msg = f"Ошибка обработки вопроса: {str(e)}"
|
| 66 |
+
log_message(error_msg)
|
| 67 |
+
return error_msg, ""
|
| 68 |
+
|
| 69 |
+
def get_sources_for_question(question):
|
| 70 |
+
try:
|
| 71 |
+
nodes = index_retriever.retrieve_nodes(question)
|
| 72 |
+
if not nodes:
|
| 73 |
+
return "<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; text-align: center;'>Источники не найдены</div>"
|
| 74 |
+
|
| 75 |
+
sources_html = "<div style='background-color: #2d3748; color: white; padding: 15px; border-radius: 10px;'>"
|
| 76 |
+
sources_html += "<h3 style='color: #4fd1c7; margin-top: 0;'>📚 Источники:</h3>"
|
| 77 |
+
|
| 78 |
+
for i, node in enumerate(nodes[:5], 1):
|
| 79 |
+
source_text = node.text[:200] + "..." if len(node.text) > 200 else node.text
|
| 80 |
+
sources_html += f"<div style='margin: 10px 0; padding: 10px; background-color: #4a5568; border-radius: 5px;'>"
|
| 81 |
+
sources_html += f"<strong>Источник {i}:</strong><br>"
|
| 82 |
+
sources_html += f"<small>{source_text}</small>"
|
| 83 |
+
sources_html += "</div>"
|
| 84 |
+
|
| 85 |
+
sources_html += "</div>"
|
| 86 |
+
return sources_html
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
log_message(f"Ошибка получения источников: {str(e)}")
|
| 90 |
+
return "<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; text-align: center;'>Ошибка загрузки источников</div>"
|
| 91 |
|
| 92 |
def handle_model_switch(model_name):
|
| 93 |
+
try:
|
| 94 |
+
return index_retriever.switch_model(model_name)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
error_msg = f"Ошибка переключения модели: {str(e)}"
|
| 97 |
+
log_message(error_msg)
|
| 98 |
+
return f"❌ {error_msg}"
|
| 99 |
|
| 100 |
def get_current_model_status():
|
| 101 |
+
try:
|
| 102 |
+
if not index_retriever.is_initialized():
|
| 103 |
+
return "Система не инициализирована"
|
| 104 |
+
return f"Текущая модель: {index_retriever.get_current_model()}"
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return "Ошибка получения статуса модели"
|
| 107 |
|
| 108 |
def get_chat_history_html():
|
| 109 |
if chat_handler is None:
|
index_retriever.py
CHANGED
|
@@ -78,7 +78,7 @@ def initialize_models(documents):
|
|
| 78 |
return False
|
| 79 |
|
| 80 |
def create_query_engine():
|
| 81 |
-
global query_engine
|
| 82 |
|
| 83 |
try:
|
| 84 |
log_message(f"Применяется промпт: {PROMPT_SIMPLE_POISK[:100]}...")
|
|
@@ -118,8 +118,6 @@ def create_query_engine():
|
|
| 118 |
raise
|
| 119 |
|
| 120 |
def query(question):
|
| 121 |
-
global query_engine, current_model
|
| 122 |
-
|
| 123 |
if query_engine is None:
|
| 124 |
log_message("❌ Query engine не инициализирован")
|
| 125 |
return "❌ Система не инициализирована"
|
|
@@ -141,7 +139,7 @@ def query(question):
|
|
| 141 |
return f"❌ {error_msg}"
|
| 142 |
|
| 143 |
def switch_model(model_name):
|
| 144 |
-
global current_model
|
| 145 |
|
| 146 |
try:
|
| 147 |
log_message(f"Переключение на модель: {model_name}")
|
|
@@ -163,8 +161,6 @@ def switch_model(model_name):
|
|
| 163 |
return f"❌ {error_msg}"
|
| 164 |
|
| 165 |
def rerank_nodes(query_text, nodes, top_k=10):
|
| 166 |
-
global reranker
|
| 167 |
-
|
| 168 |
if not nodes or not reranker:
|
| 169 |
return nodes[:top_k]
|
| 170 |
|
|
@@ -189,8 +185,6 @@ def rerank_nodes(query_text, nodes, top_k=10):
|
|
| 189 |
return nodes[:top_k]
|
| 190 |
|
| 191 |
def retrieve_nodes(question):
|
| 192 |
-
global query_engine
|
| 193 |
-
|
| 194 |
if query_engine is None:
|
| 195 |
return []
|
| 196 |
|
|
|
|
| 78 |
return False
|
| 79 |
|
| 80 |
def create_query_engine():
|
| 81 |
+
global query_engine
|
| 82 |
|
| 83 |
try:
|
| 84 |
log_message(f"Применяется промпт: {PROMPT_SIMPLE_POISK[:100]}...")
|
|
|
|
| 118 |
raise
|
| 119 |
|
| 120 |
def query(question):
|
|
|
|
|
|
|
| 121 |
if query_engine is None:
|
| 122 |
log_message("❌ Query engine не инициализирован")
|
| 123 |
return "❌ Система не инициализирована"
|
|
|
|
| 139 |
return f"❌ {error_msg}"
|
| 140 |
|
| 141 |
def switch_model(model_name):
|
| 142 |
+
global current_model
|
| 143 |
|
| 144 |
try:
|
| 145 |
log_message(f"Переключение на модель: {model_name}")
|
|
|
|
| 161 |
return f"❌ {error_msg}"
|
| 162 |
|
| 163 |
def rerank_nodes(query_text, nodes, top_k=10):
|
|
|
|
|
|
|
| 164 |
if not nodes or not reranker:
|
| 165 |
return nodes[:top_k]
|
| 166 |
|
|
|
|
| 185 |
return nodes[:top_k]
|
| 186 |
|
| 187 |
def retrieve_nodes(question):
|
|
|
|
|
|
|
| 188 |
if query_engine is None:
|
| 189 |
return []
|
| 190 |
|