Initial commit
Browse files- __init__.py +0 -0
- app.py +125 -0
__init__.py
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
try:
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spacy
|
| 5 |
+
import glirel
|
| 6 |
+
except:
|
| 7 |
+
subprocess.run(["pip", "install", "gradio==4.31.5"])
|
| 8 |
+
subprocess.run(["pip", "install", "spacy"])
|
| 9 |
+
subprocess.run(["pip", "install", "glirel"])
|
| 10 |
+
subprocess.run(["pip", "install", "scipy==1.10.1"])
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
import spacy
|
| 14 |
+
spacy.load("en_core_web_sm")
|
| 15 |
+
except:
|
| 16 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 17 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_md"])
|
| 18 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_lg"])
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
from typing import Dict, Union
|
| 24 |
+
import gradio as gr
|
| 25 |
+
from glirel import GLiREL
|
| 26 |
+
import spacy
|
| 27 |
+
|
| 28 |
+
examples = [
|
| 29 |
+
[
|
| 30 |
+
"Amazon, founded by Jeff Bezos, is a leader in e-commerce and cloud computing. The company has also ventured into artificial intelligence and digital streaming.",
|
| 31 |
+
"en_core_web_sm",
|
| 32 |
+
"Founded_By, Located_In, Produces, Operates_In, Works_With, Known_For, Headquartered_In, Partnership_With, Innovates_In, Established_In",
|
| 33 |
+
],
|
| 34 |
+
[
|
| 35 |
+
"J.K. Rowling, the author of the Harry Potter series, has significantly impacted modern literature. Her books have been translated into numerous languages and adapted into successful films.",
|
| 36 |
+
"en_core_web_sm",
|
| 37 |
+
"Translated_Into, Adapted_Into, Born_In, Author_Of, Known_For, Works_With, Located_In, Writes_For, Produced_By, Published_By"
|
| 38 |
+
],
|
| 39 |
+
[
|
| 40 |
+
"Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in April 1976. The company is headquartered in Cupertino, California.",
|
| 41 |
+
"en_core_web_sm",
|
| 42 |
+
"CO_FOUNDER, HEADQUARTERED_IN, FOUNDED_BY, LOCATED_IN, ESTABLISHED_IN, PARTNERSHIP_WITH, WORKS_WITH, KNOWN_FOR"
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Load the relation extraction model
|
| 49 |
+
rel_model = GLiREL.from_pretrained("jackboyla/glirel_beta")
|
| 50 |
+
|
| 51 |
+
# Function to perform Named Entity Recognition
|
| 52 |
+
def perform_ner(text, model_name):
|
| 53 |
+
nlp = spacy.load(model_name)
|
| 54 |
+
doc = nlp(text)
|
| 55 |
+
return doc
|
| 56 |
+
|
| 57 |
+
# Function to extract relations
|
| 58 |
+
def extract_relations(tokens, ner, labels):
|
| 59 |
+
relations = rel_model.predict_relations(tokens, labels, threshold=0.0, ner=ner, top_k=1)
|
| 60 |
+
sorted_data_desc = sorted(relations, key=lambda x: x['score'], reverse=True)
|
| 61 |
+
return sorted_data_desc
|
| 62 |
+
|
| 63 |
+
def format_ner(text, ner):
|
| 64 |
+
if isinstance(ner[0], spacy.tokens.Span):
|
| 65 |
+
# if ner is spacy entities; otherwise we assume the format is correct
|
| 66 |
+
ner = [[ent.start_char, ent.end_char, ent.label_, ent.text] for ent in ner]
|
| 67 |
+
return {
|
| 68 |
+
"text": text,
|
| 69 |
+
"entities": [
|
| 70 |
+
{
|
| 71 |
+
"entity": entity[2],
|
| 72 |
+
"word": entity[3],
|
| 73 |
+
"start": entity[0],
|
| 74 |
+
"end": entity[1],
|
| 75 |
+
"score": 0,
|
| 76 |
+
}
|
| 77 |
+
for entity in ner
|
| 78 |
+
],
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
# Gradio Interface
|
| 82 |
+
def process(text, model_name, labels):
|
| 83 |
+
doc = perform_ner(text, model_name)
|
| 84 |
+
tokens = [token.text for token in doc]
|
| 85 |
+
ner = [[ent.start, (ent.end-1), ent.label_, ent.text] for ent in doc.ents]
|
| 86 |
+
labels = labels.split(',')
|
| 87 |
+
relations = extract_relations(tokens, ner, labels)
|
| 88 |
+
print(relations)
|
| 89 |
+
formatted_ner = format_ner(doc.text, doc.ents)
|
| 90 |
+
formatted_rel = ""
|
| 91 |
+
for item in relations:
|
| 92 |
+
formatted_rel += f"{item['head_text']} --> {item['label']} --> {item['tail_text']} \t\t| score: {item['score']}\n"
|
| 93 |
+
return formatted_ner, formatted_rel
|
| 94 |
+
|
| 95 |
+
# Gradio App Layout
|
| 96 |
+
with gr.Blocks() as demo:
|
| 97 |
+
|
| 98 |
+
gr.Markdown("# 🕵️♀️GLiREL: Zero-Shot Relation Extraction")
|
| 99 |
+
gr.Markdown("GitHub: https://github.com/jackboyla/GLiREL")
|
| 100 |
+
|
| 101 |
+
text_input = gr.Textbox(label="Input Text", value="Jack lives in London but he was born in Mongolia.")
|
| 102 |
+
model_name_input = gr.Dropdown(choices=["en_core_web_sm", "en_core_web_md", "en_core_web_lg"], label="NER Model", value="en_core_web_sm")
|
| 103 |
+
labels_input = gr.Textbox(label="Relation Labels (comma-separated)", value="country of origin, licensed to broadcast to, father, followed by, characters")
|
| 104 |
+
|
| 105 |
+
ner_output = gr.HighlightedText(label="NER")
|
| 106 |
+
rel_output = gr.Textbox(label="Relation Extraction Results")
|
| 107 |
+
|
| 108 |
+
extract_button = gr.Button("Extract Relations")
|
| 109 |
+
extract_button.click(
|
| 110 |
+
fn=process,
|
| 111 |
+
inputs=[text_input, model_name_input, labels_input],
|
| 112 |
+
outputs=[ner_output, rel_output]
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
examples = gr.Examples(
|
| 116 |
+
examples,
|
| 117 |
+
fn=process,
|
| 118 |
+
inputs=[text_input, model_name_input, labels_input],
|
| 119 |
+
outputs=[ner_output, rel_output],
|
| 120 |
+
cache_examples=True,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
demo.launch(server_port=9989)
|