Spaces:
Build error
Build error
app.py sem reader do haystack
Browse files
app.py
CHANGED
@@ -5,45 +5,26 @@ from haystack.utils import fetch_archive_from_http
|
|
5 |
import os
|
6 |
from haystack.pipelines.standard_pipelines import TextIndexingPipeline
|
7 |
from haystack.nodes import BM25Retriever
|
8 |
-
from
|
9 |
-
from haystack.pipelines import ExtractiveQAPipeline
|
10 |
-
from haystack.utils import print_answers
|
11 |
-
from pydantic import BaseModel
|
12 |
-
from typing import Any
|
13 |
-
import pandas as pd
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
#Criando o objeto documentStore
|
19 |
|
|
|
20 |
document_store = InMemoryDocumentStore(use_bm25=True)
|
21 |
|
22 |
-
#Exportando os dados necessários sobre o Game Of Thrones
|
23 |
-
|
24 |
doc_dir = "data/build_your_first_question_answering_system"
|
25 |
-
|
26 |
fetch_archive_from_http(
|
27 |
url="https://s3.eu-central-1.amazonaws.com/deepset.ai-farm-qa/datasets/documents/wiki_gameofthrones_txt1.zip",
|
28 |
output_dir=doc_dir,
|
29 |
)
|
30 |
|
31 |
-
|
32 |
files_to_index = [doc_dir + "/" + f for f in os.listdir(doc_dir)]
|
33 |
indexing_pipeline = TextIndexingPipeline(document_store)
|
34 |
indexing_pipeline.run_batch(file_paths=files_to_index)
|
35 |
|
36 |
retriever = BM25Retriever(document_store=document_store)
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
pipe = ExtractiveQAPipeline(reader, retriever)
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
image = Image.open('comida.jpg')
|
49 |
st.image(image)
|
@@ -52,19 +33,25 @@ e respotas a perguntas de vários domínios sobre esportes para perguntas aberta
|
|
52 |
|
53 |
st.subheader('QASports',divider='rainbow')
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
res
|
62 |
-
)
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
if st.button('Buscar Resposta'):
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
|
|
5 |
import os
|
6 |
from haystack.pipelines.standard_pipelines import TextIndexingPipeline
|
7 |
from haystack.nodes import BM25Retriever
|
8 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Criando o objeto documentStore
|
11 |
document_store = InMemoryDocumentStore(use_bm25=True)
|
12 |
|
13 |
+
# Exportando os dados necessários sobre o Game Of Thrones
|
|
|
14 |
doc_dir = "data/build_your_first_question_answering_system"
|
|
|
15 |
fetch_archive_from_http(
|
16 |
url="https://s3.eu-central-1.amazonaws.com/deepset.ai-farm-qa/datasets/documents/wiki_gameofthrones_txt1.zip",
|
17 |
output_dir=doc_dir,
|
18 |
)
|
19 |
|
|
|
20 |
files_to_index = [doc_dir + "/" + f for f in os.listdir(doc_dir)]
|
21 |
indexing_pipeline = TextIndexingPipeline(document_store)
|
22 |
indexing_pipeline.run_batch(file_paths=files_to_index)
|
23 |
|
24 |
retriever = BM25Retriever(document_store=document_store)
|
25 |
|
26 |
+
# Utilizando um pipeline da biblioteca Transformers
|
27 |
+
pipe = pipeline("question-answering", model="deepset/roberta-base-squad2", tokenizer="deepset/roberta-base-squad2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
image = Image.open('comida.jpg')
|
30 |
st.image(image)
|
|
|
33 |
|
34 |
st.subheader('QASports',divider='rainbow')
|
35 |
|
36 |
+
user_input = None
|
37 |
+
if not user_input:
|
38 |
+
user_input = st.text_input("Por favor, digite uma pergunta.")
|
39 |
+
|
40 |
+
if user_input:
|
41 |
+
res = retriever.retrieve(user_input, top_k=5) # Recupera os top 5 documentos relevantes
|
42 |
+
if res:
|
43 |
+
st.write(f"Foram encontrados {len(res)} documentos relevantes.")
|
44 |
+
for document in res:
|
45 |
+
prediction = pipe(question=user_input, context=document.content)
|
46 |
+
context = document.content
|
47 |
+
confidence = prediction["score"]
|
48 |
+
answer = prediction["answer"]
|
49 |
+
st.write("Pergunta:", user_input)
|
50 |
+
st.write("Resposta:", answer)
|
51 |
+
st.write("Confiança:", confidence)
|
52 |
+
st.write("Contexto:", context)
|
53 |
+
st.write("-" * 50)
|
54 |
|
55 |
if st.button('Buscar Resposta'):
|
56 |
+
st.write(prediction["answer"])
|
|
|
|
|
57 |
|
search.py
DELETED
@@ -1,36 +0,0 @@
|
|
1 |
-
from haystack.document_stores import InMemoryDocumentStore
|
2 |
-
from haystack.utils import fetch_archive_from_http
|
3 |
-
import os
|
4 |
-
from haystack.pipelines.standard_pipelines import TextIndexingPipeline
|
5 |
-
from haystack.nodes import BM25Retriever
|
6 |
-
from haystack.nodes import FARMReader
|
7 |
-
from haystack.pipelines import ExtractiveQAPipeline
|
8 |
-
from haystack.utils import print_answers
|
9 |
-
|
10 |
-
#Criando o objeto documentStore
|
11 |
-
|
12 |
-
document_store = InMemoryDocumentStore(use_bm25=True)
|
13 |
-
|
14 |
-
#Exportando os dados necessários sobre o Game Of Thrones
|
15 |
-
|
16 |
-
doc_dir = "data/build_your_first_question_answering_system"
|
17 |
-
|
18 |
-
fetch_archive_from_http(
|
19 |
-
url="https://s3.eu-central-1.amazonaws.com/deepset.ai-farm-qa/datasets/documents/wiki_gameofthrones_txt1.zip",
|
20 |
-
output_dir=doc_dir,
|
21 |
-
)
|
22 |
-
|
23 |
-
files_to_index = [doc_dir + "/" + f for f in os.listdir(doc_dir)]
|
24 |
-
indexing_pipeline = TextIndexingPipeline(document_store)
|
25 |
-
indexing_pipeline.run_batch(file_paths=files_to_index)
|
26 |
-
|
27 |
-
retriever = BM25Retriever(document_store=document_store)
|
28 |
-
|
29 |
-
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=True)
|
30 |
-
|
31 |
-
|
32 |
-
pipe = ExtractiveQAPipeline(reader, retriever)
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|