Spaces:
Runtime error
Runtime error
Jorge Henao
commited on
Commit
Β·
991cc91
1
Parent(s):
3a70faa
refactor single app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,53 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
query = ExtractiveProposalQueries(es_host = Config.es_host, es_index = Config.proposals_index,
|
4 |
es_user = Config.es_user, es_password = Config.es_password,
|
|
|
1 |
import gradio as gr
|
2 |
+
from haystack.nodes import BM25Retriever, FARMReader
|
3 |
+
from haystack.document_stores import ElasticsearchDocumentStore
|
4 |
+
from haystack.pipelines import ExtractiveQAPipeline
|
5 |
+
from abc import ABC, abstractmethod
|
6 |
+
|
7 |
+
import certifi
|
8 |
+
ca_certs=certifi.where()
|
9 |
+
|
10 |
+
class Config():
|
11 |
+
es_host = "ask2democracy.es.us-central1.gcp.cloud.es.io"
|
12 |
+
es_user = "elastic"
|
13 |
+
es_password = "siKAHmmk2flwEaKNqQVZwp49"
|
14 |
+
proposals_index = "petrolfo"
|
15 |
+
#reader_model_name_or_path = "deepset/roberta-base-squad2"
|
16 |
+
reader_model_name_or_path = "deepset/xlm-roberta-large-squad2"
|
17 |
+
use_gpu = True
|
18 |
+
|
19 |
+
|
20 |
+
class DocumentQueries(ABC):
|
21 |
+
|
22 |
+
@abstractmethod
|
23 |
+
def search_by_query(self, query : str, retriever_top_k: int, reader_top_k: int, es_index: str):
|
24 |
+
pass
|
25 |
+
|
26 |
+
class ExtractiveProposalQueries(DocumentQueries):
|
27 |
+
|
28 |
+
def __init__(self, es_host: str, es_index: str, es_user, es_password, reader_name_or_path: str, use_gpu = False) -> None:
|
29 |
+
reader = FARMReader(model_name_or_path = reader_name_or_path, use_gpu = use_gpu, num_processes=1)
|
30 |
+
self._initialize_pipeline(es_host, es_index, es_user, es_password, reader = reader)
|
31 |
+
|
32 |
+
|
33 |
+
def _initialize_pipeline(self, es_host, es_index, es_user, es_password, reader = None):
|
34 |
+
if reader is not None:
|
35 |
+
self.reader = reader
|
36 |
+
self.es_host = es_host
|
37 |
+
self.es_user = es_user
|
38 |
+
self.es_password = es_password
|
39 |
+
self.document_store = ElasticsearchDocumentStore(host = es_host, username=es_user, password=es_password, index = es_index, port = 443, scheme='https', verify_certs=True, ca_certs=ca_certs)
|
40 |
+
self.retriever = BM25Retriever(document_store = self.document_store)
|
41 |
+
self.pipe = ExtractiveQAPipeline(self.reader, self.retriever)
|
42 |
+
|
43 |
+
def search_by_query(self, query : str, retriever_top_k: int, reader_top_k: int, es_index: str = None) :
|
44 |
+
if es_index is not None:
|
45 |
+
self._initialize_pipeline(self.es_host, es_index, self.es_user, self.es_password)
|
46 |
+
params = {"Retriever": {"top_k": retriever_top_k}, "Reader": {"top_k": reader_top_k}}
|
47 |
+
prediction = self.pipe.run( query = query, params = params)
|
48 |
+
return prediction["answers"]
|
49 |
+
|
50 |
+
|
51 |
|
52 |
query = ExtractiveProposalQueries(es_host = Config.es_host, es_index = Config.proposals_index,
|
53 |
es_user = Config.es_user, es_password = Config.es_password,
|