Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,9 @@ from relik.inference.serve.frontend.utils import get_random_color
|
|
14 |
from relik.retriever.pytorch_modules.model import GoldenRetriever
|
15 |
from relik.retriever.indexers.inmemory import InMemoryDocumentIndex
|
16 |
from relik.inference.data.objects import TaskType
|
|
|
|
|
|
|
17 |
|
18 |
LOGO = """
|
19 |
<div style="text-align: center; display: flex; flex-direction: column; align-items: center;">
|
@@ -122,17 +125,49 @@ relik_out: RelikOutput = relik("Michael Jordan was one of the best players in th
|
|
122 |
For more information, please refer to the [source code](https://github.com/SapienzaNLP/relik/).
|
123 |
"""
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
wikipedia_retriever = GoldenRetriever("relik-ie/encoder-e5-base-v2-wikipedia", device="cuda")
|
126 |
wikipedia_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-base-v2-wikipedia-index", index_precision="bf16", device="cuda")
|
127 |
|
128 |
wikidata_retriever = GoldenRetriever("relik-ie/encoder-e5-small-v2-wikipedia-relations", device="cuda")
|
129 |
wikidata_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-small-v2-wikipedia-relations-index", index_precision="bf16", device="cuda")
|
130 |
|
|
|
|
|
|
|
|
|
131 |
relik_available_models = [
|
132 |
"relik-ie/relik-reader-small-cie-wikipedia",
|
133 |
"sapienzanlp/relik-entity-linking-large",
|
134 |
"relik-ie/relik-entity-linking-large-robust",
|
135 |
-
"relik-ie/relik-relation-extraction-
|
|
|
136 |
]
|
137 |
|
138 |
relik_models = {
|
@@ -157,8 +192,22 @@ relik_models = {
|
|
157 |
},
|
158 |
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
|
159 |
),
|
160 |
-
"relik-ie/relik-relation-extraction-
|
161 |
-
"relik-ie/relik-relation-extraction-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
index=wikidata_index,
|
163 |
device="cuda",
|
164 |
retriever=wikidata_retriever,
|
|
|
14 |
from relik.retriever.pytorch_modules.model import GoldenRetriever
|
15 |
from relik.retriever.indexers.inmemory import InMemoryDocumentIndex
|
16 |
from relik.inference.data.objects import TaskType
|
17 |
+
from relik.retriever.pytorch_modules import RetrievedSample
|
18 |
+
from relik.retriever.indexers.document import Document, DocumentStore
|
19 |
+
from relik.retriever.indexers.base import BaseDocumentIndex
|
20 |
|
21 |
LOGO = """
|
22 |
<div style="text-align: center; display: flex; flex-direction: column; align-items: center;">
|
|
|
125 |
For more information, please refer to the [source code](https://github.com/SapienzaNLP/relik/).
|
126 |
"""
|
127 |
|
128 |
+
class GoldenSillyRetriever(GoldenRetriever):
|
129 |
+
def __init__(self, documents: List[str], *args, **kwargs):
|
130 |
+
self.documents = DocumentStore([Document(doc) for doc in documents])
|
131 |
+
self.document_index = BaseDocumentIndex(self.documents)
|
132 |
+
def retrieve(self,
|
133 |
+
text: Optional[Union[str, List[str]]] = None,
|
134 |
+
k: int = 100,
|
135 |
+
*args,
|
136 |
+
**kwargs,
|
137 |
+
) -> List[List[RetrievedSample]]:
|
138 |
+
if isinstance(text, str):
|
139 |
+
text = [text]
|
140 |
+
elif text is None:
|
141 |
+
text = []
|
142 |
+
return [
|
143 |
+
[RetrievedSample(score=1.0, document=doc) for doc in self.documents[:k]]
|
144 |
+
for _ in text
|
145 |
+
]
|
146 |
+
def index(self):
|
147 |
+
pass
|
148 |
+
def eval(self):
|
149 |
+
pass
|
150 |
+
def save_pretrained(self):
|
151 |
+
pass
|
152 |
+
def to(self, device):
|
153 |
+
pass
|
154 |
+
|
155 |
wikipedia_retriever = GoldenRetriever("relik-ie/encoder-e5-base-v2-wikipedia", device="cuda")
|
156 |
wikipedia_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-base-v2-wikipedia-index", index_precision="bf16", device="cuda")
|
157 |
|
158 |
wikidata_retriever = GoldenRetriever("relik-ie/encoder-e5-small-v2-wikipedia-relations", device="cuda")
|
159 |
wikidata_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-small-v2-wikipedia-relations-index", index_precision="bf16", device="cuda")
|
160 |
|
161 |
+
ner_type_retriever = GoldenSillyRetriever(
|
162 |
+
documents=['media', 'disease', 'miscellaneous', 'event', 'person', 'location', 'time', 'celestial', 'organization', 'concept']
|
163 |
+
)
|
164 |
+
|
165 |
relik_available_models = [
|
166 |
"relik-ie/relik-reader-small-cie-wikipedia",
|
167 |
"sapienzanlp/relik-entity-linking-large",
|
168 |
"relik-ie/relik-entity-linking-large-robust",
|
169 |
+
"relik-ie/relik-relation-extraction-small-wikipedia-ner",
|
170 |
+
"relik-ie/relik-relation-extraction-small-wikipedia",
|
171 |
]
|
172 |
|
173 |
relik_models = {
|
|
|
192 |
},
|
193 |
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
|
194 |
),
|
195 |
+
"relik-ie/relik-relation-extraction-small-wikipedia-ner": Relik.from_pretrained(
|
196 |
+
"relik-ie/relik-relation-extraction-small-wikipedia-ner",
|
197 |
+
index=wikidata_index,
|
198 |
+
device="cuda",
|
199 |
+
use_nme=True,
|
200 |
+
retriever={
|
201 |
+
TaskType.SPAN: ner_type_retriever,
|
202 |
+
TaskType.TRIPLET: wikidata_retriever,
|
203 |
+
},
|
204 |
+
index={
|
205 |
+
TaskType.SPAN: ner_type_retriever.document_index,
|
206 |
+
TaskType.TRIPLET: wikidata_index,
|
207 |
+
}
|
208 |
+
),
|
209 |
+
"relik-ie/relik-relation-extraction-small-wikipedia": Relik.from_pretrained(
|
210 |
+
"relik-ie/relik-relation-extraction-small-wikipedia",
|
211 |
index=wikidata_index,
|
212 |
device="cuda",
|
213 |
retriever=wikidata_retriever,
|