Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import List, Optional, Union
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spacy
|
| 5 |
+
from spacy.tokens import Doc, Span
|
| 6 |
+
from relik import Relik
|
| 7 |
+
from relik.inference.data.objects import TaskType, RelikOutput
|
| 8 |
+
from relik.retriever.pytorch_modules import GoldenRetriever
|
| 9 |
+
from relik.retriever.indexers.inmemory import InMemoryDocumentIndex
|
| 10 |
+
from pyvis.network import Network
|
| 11 |
+
|
| 12 |
+
# RELIK Models Setup
|
| 13 |
+
wikipedia_retriever = GoldenRetriever("relik-ie/encoder-e5-base-v2-wikipedia", device="cuda")
|
| 14 |
+
wikipedia_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-base-v2-wikipedia-index", index_precision="bf16", device="cuda")
|
| 15 |
+
wikidata_retriever = GoldenRetriever("relik-ie/encoder-e5-small-v2-wikipedia-relations", device="cuda")
|
| 16 |
+
wikidata_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-small-v2-wikipedia-relations-index", index_precision="bf16", device="cuda")
|
| 17 |
+
|
| 18 |
+
relik_models = {
|
| 19 |
+
"sapienzanlp/relik-entity-linking-large": Relik.from_pretrained(
|
| 20 |
+
"sapienzanlp/relik-entity-linking-large", device="cuda", index=wikipedia_index, retriever=wikipedia_retriever,
|
| 21 |
+
reader_kwargs={"dataset_kwargs": {"use_nme": True}}
|
| 22 |
+
),
|
| 23 |
+
"relik-ie/relik-relation-extraction-small": Relik.from_pretrained(
|
| 24 |
+
"relik-ie/relik-relation-extraction-small", index=wikidata_index, device="cuda", retriever=wikidata_retriever
|
| 25 |
+
)
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def get_span_annotations(response, doc):
|
| 29 |
+
spans = []
|
| 30 |
+
for span in response.spans:
|
| 31 |
+
spans.append(Span(doc, span.start, span.end, span.label))
|
| 32 |
+
colors = {span.label_: '#ff5733' for span in spans} # Simple fixed color for demonstration
|
| 33 |
+
return spans, colors
|
| 34 |
+
|
| 35 |
+
def generate_graph(spans, response, colors):
|
| 36 |
+
g = Network(width="720px", height="600px", directed=True)
|
| 37 |
+
for ent in spans:
|
| 38 |
+
g.add_node(ent.text, label=ent.text, color=colors[ent.label_], size=15)
|
| 39 |
+
seen_rels = set()
|
| 40 |
+
for rel in response.triplets:
|
| 41 |
+
if (rel.subject.text, rel.object.text, rel.label) in seen_rels:
|
| 42 |
+
continue
|
| 43 |
+
g.add_edge(rel.subject.text, rel.object.text, label=rel.label)
|
| 44 |
+
seen_rels.add((rel.subject.text, rel.object.text, rel.label))
|
| 45 |
+
html = g.generate_html()
|
| 46 |
+
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" srcdoc='{html.replace("'", '"')}'></iframe>"""
|
| 47 |
+
|
| 48 |
+
def text_analysis(Text, Model, Relation_Threshold, Window_Size, Window_Stride):
|
| 49 |
+
if Model not in relik_models:
|
| 50 |
+
raise ValueError(f"Model {Model} not found.")
|
| 51 |
+
relik = relik_models[Model]
|
| 52 |
+
nlp = spacy.blank("xx")
|
| 53 |
+
annotated_text = relik(Text, annotation_type="word", relation_threshold=Relation_Threshold, window_size=Window_Size, window_stride=Window_Stride)
|
| 54 |
+
doc = Doc(nlp.vocab, words=[token.text for token in annotated_text.tokens])
|
| 55 |
+
spans, colors = get_span_annotations(annotated_text, doc)
|
| 56 |
+
doc.spans["sc"] = spans
|
| 57 |
+
display_el = spacy.displacy.render(doc, style="span", options={"colors": colors}).replace("\n", " ")
|
| 58 |
+
display_el = display_el.replace("border-radius: 0.35em;", "border-radius: 0.35em; white-space: nowrap;").replace("span style", "span id='el' style")
|
| 59 |
+
display_re = generate_graph(spans, annotated_text, colors) if annotated_text.triplets else ""
|
| 60 |
+
return display_el, display_re
|
| 61 |
+
|
| 62 |
+
theme = gr.themes.Base(primary_hue="rose", secondary_hue="rose", text_size="lg")
|
| 63 |
+
css = """
|
| 64 |
+
h1 { text-align: center; display: block; }
|
| 65 |
+
mark { color: black; }
|
| 66 |
+
#el { white-space: nowrap; }
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(fill_height=True, css=css, theme=theme) as demo:
|
| 70 |
+
gr.Markdown("# ReLiK with P-FAF Integration")
|
| 71 |
+
gr.Interface(
|
| 72 |
+
text_analysis,
|
| 73 |
+
[
|
| 74 |
+
gr.Textbox(label="Input Text", placeholder="Enter sentence here..."),
|
| 75 |
+
gr.Dropdown(list(relik_models.keys()), value="sapienzanlp/relik-entity-linking-large", label="Relik Model"),
|
| 76 |
+
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.5, label="Relation Threshold"),
|
| 77 |
+
gr.Slider(minimum=16, maximum=128, step=16, value=32, label="Window Size"),
|
| 78 |
+
gr.Slider(minimum=8, maximum=64, step=8, value=16, label="Window Stride")
|
| 79 |
+
],
|
| 80 |
+
[gr.HTML(label="Entities"), gr.HTML(label="Relations")],
|
| 81 |
+
examples=[
|
| 82 |
+
["Michael Jordan was one of the best players in the NBA."],
|
| 83 |
+
["Noam Chomsky is a renowned linguist and cognitive scientist."]
|
| 84 |
+
],
|
| 85 |
+
allow_flagging="never"
|
| 86 |
+
)
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch()
|