Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,8 @@ PROMPT_TEMPLATE = """
|
|
| 25 |
# Глобальная переменная для статуса
|
| 26 |
status_message = "Инициализация..."
|
| 27 |
|
| 28 |
-
def initialize_chroma
|
|
|
|
| 29 |
global status_message
|
| 30 |
if not os.path.exists(CHROMA_PATH):
|
| 31 |
status_message = "Создание базы данных Chroma..."
|
|
@@ -83,9 +84,9 @@ def save_to_chroma(chunks: list[Document]):
|
|
| 83 |
if os.path.exists(CHROMA_PATH):
|
| 84 |
shutil.rmtree(CHROMA_PATH)
|
| 85 |
embeddings = HuggingFaceEmbeddings(
|
| 86 |
-
model_name="sentence-transformers/paraphrase-multilingual-
|
| 87 |
-
model_kwargs={'device': 'cpu'},
|
| 88 |
-
encode_kwargs={'normalize_embeddings':
|
| 89 |
)
|
| 90 |
Chroma.from_documents(
|
| 91 |
chunks,
|
|
@@ -97,8 +98,15 @@ def process_query(query_text: str, db):
|
|
| 97 |
results = db.similarity_search_with_relevance_scores(query_text, k=3)
|
| 98 |
global status_message
|
| 99 |
status_message += f"\nНайдено {len(results)} результатов с релевантностью: {[round(score, 2) for _, score in results]}"
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _ in results])
|
| 103 |
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
|
| 104 |
prompt = prompt_template.format(context=context_text, question=query_text)
|
|
|
|
| 25 |
# Глобальная переменная для статуса
|
| 26 |
status_message = "Инициализация..."
|
| 27 |
|
| 28 |
+
def initialize_chroma
|
| 29 |
+
():
|
| 30 |
global status_message
|
| 31 |
if not os.path.exists(CHROMA_PATH):
|
| 32 |
status_message = "Создание базы данных Chroma..."
|
|
|
|
| 84 |
if os.path.exists(CHROMA_PATH):
|
| 85 |
shutil.rmtree(CHROMA_PATH)
|
| 86 |
embeddings = HuggingFaceEmbeddings(
|
| 87 |
+
model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
|
| 88 |
+
model_kwargs={'device': 'cpu', 'trust_remote_code': True},
|
| 89 |
+
encode_kwargs={'normalize_embeddings': False} # Экспериментируйте с этим
|
| 90 |
)
|
| 91 |
Chroma.from_documents(
|
| 92 |
chunks,
|
|
|
|
| 98 |
results = db.similarity_search_with_relevance_scores(query_text, k=3)
|
| 99 |
global status_message
|
| 100 |
status_message += f"\nНайдено {len(results)} результатов с релевантностью: {[round(score, 2) for _, score in results]}"
|
| 101 |
+
|
| 102 |
+
if not results:
|
| 103 |
+
return "Не найдено подходящих результатов.", []
|
| 104 |
+
|
| 105 |
+
if results[0][1] < 0.5:
|
| 106 |
+
response = "Нет высококачественных совпадений. Возможные варианты:\n"
|
| 107 |
+
response += "\n".join([f"- {doc.page_content[:50]}..." for doc, _ in results])
|
| 108 |
+
return response, [doc.metadata.get("source") for doc, _ in results]
|
| 109 |
+
|
| 110 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _ in results])
|
| 111 |
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
|
| 112 |
prompt = prompt_template.format(context=context_text, question=query_text)
|