Create script.py
Browse files
script.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from haystack.document_stores import FAISSDocumentStore
|
2 |
+
document_store = FAISSDocumentStore(faiss_index_factory_str="Flat", embedding_dim=1024)
|
3 |
+
|
4 |
+
# Загрузка и обработка документов
|
5 |
+
from haystack.utils import clean_wiki_text, convert_files_to_docs
|
6 |
+
doc_dir = "test"
|
7 |
+
docs = convert_files_to_docs(dir_path=doc_dir, clean_func=clean_wiki_text, split_paragraphs=True)
|
8 |
+
document_store.write_documents(docs)
|
9 |
+
|
10 |
+
from haystack.nodes import EmbeddingRetriever
|
11 |
+
# Использование модели, поддерживающей русский язык
|
12 |
+
retriever = EmbeddingRetriever(
|
13 |
+
document_store=document_store,
|
14 |
+
embedding_model="AlexKay/xlm-roberta-large-qa-multilingual-finedtuned-ru"
|
15 |
+
)
|
16 |
+
document_store.update_embeddings(retriever)
|
17 |
+
|
18 |
+
from haystack.nodes import FARMReader
|
19 |
+
# Использование модели для русского языка
|
20 |
+
reader = FARMReader(model_name_or_path="AlexKay/xlm-roberta-large-qa-multilingual-finedtuned-ru", use_gpu=False)
|
21 |
+
|
22 |
+
from haystack.pipelines import ExtractiveQAPipeline
|
23 |
+
pipe = ExtractiveQAPipeline(reader, retriever)
|
24 |
+
|
25 |
+
# Запрос на русском языке
|
26 |
+
query = "директор Института электроэнергетики и электроники?"
|
27 |
+
prediction = pipe.run(
|
28 |
+
query=query, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 10}}
|
29 |
+
)
|
30 |
+
print(prediction)
|