Commit
Β·
048754e
1
Parent(s):
f36584d
changed the loading of the model
Browse files
app.py
CHANGED
|
@@ -1,11 +1,24 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
print("Model loaded successfully!")
|
| 11 |
|
|
@@ -83,29 +96,15 @@ def get_wikipedia_title(qid, language="en"):
|
|
| 83 |
|
| 84 |
def disambiguate_sentence(sentence):
|
| 85 |
# Generate model outputs for the sentence
|
| 86 |
-
|
| 87 |
-
**tokenizer([sentence], return_tensors="pt"),
|
| 88 |
-
num_beams=5,
|
| 89 |
-
num_return_sequences=5,
|
| 90 |
-
max_new_tokens=30,
|
| 91 |
-
)
|
| 92 |
-
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 93 |
-
print(f"Decoded: {decoded}")
|
| 94 |
-
wikipedia_name = decoded[0] # Assuming the entity name is in the output
|
| 95 |
-
qid = get_wikipedia_page_props(wikipedia_name)
|
| 96 |
-
print(f"QID: {qid}")
|
| 97 |
-
|
| 98 |
-
# Get Wikipedia title and URL
|
| 99 |
-
title, url = get_wikipedia_title(qid)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
return "No entity found."
|
| 103 |
|
| 104 |
# Create an HTML output with a clickable link
|
| 105 |
entity_info = f"""<div>
|
| 106 |
-
<strong>Entity:</strong> {title} <br>
|
| 107 |
-
<strong>QID:</strong> {qid} <br>
|
| 108 |
-
<a href="{url}" target="_blank">Wikipedia Page</a>
|
| 109 |
</div>
|
| 110 |
"""
|
| 111 |
return entity_info
|
|
@@ -117,7 +116,7 @@ def nel_app_interface():
|
|
| 117 |
label="Input Sentence",
|
| 118 |
placeholder="Enter your sentence here:",
|
| 119 |
)
|
| 120 |
-
output_entities = gr.HTML(label="Linked
|
| 121 |
|
| 122 |
# Interface definition
|
| 123 |
interface = gr.Interface(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
|
| 7 |
+
NEL_MODEL_NAME = "impresso-project/nel-mgenre-multilingual"
|
| 8 |
+
|
| 9 |
+
# Load the tokenizer and model from the specified pre-trained model name
|
| 10 |
+
# The model used here is "https://huggingface.co/impresso-project/nel-mgenre-multilingual"
|
| 11 |
+
nel_tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
+
"impresso-project/nel-mgenre-multilingual"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
nel_pipeline = pipeline(
|
| 16 |
+
"generic-nel",
|
| 17 |
+
model=NEL_MODEL_NAME,
|
| 18 |
+
tokenizer=nel_tokenizer,
|
| 19 |
+
trust_remote_code=True,
|
| 20 |
+
device="cpu",
|
| 21 |
+
)
|
| 22 |
|
| 23 |
print("Model loaded successfully!")
|
| 24 |
|
|
|
|
| 96 |
|
| 97 |
def disambiguate_sentence(sentence):
|
| 98 |
# Generate model outputs for the sentence
|
| 99 |
+
linked_entity = nel_pipeline(sentence)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
linked_entity = linked_entity[0]
|
|
|
|
| 102 |
|
| 103 |
# Create an HTML output with a clickable link
|
| 104 |
entity_info = f"""<div>
|
| 105 |
+
<strong>Entity:</strong> {linked_entity['title']} <br>
|
| 106 |
+
<strong>QID:</strong> {linked_entity['qid']} <br>
|
| 107 |
+
<a href="{linked_entity['url']}" target="_blank">Wikipedia Page</a>
|
| 108 |
</div>
|
| 109 |
"""
|
| 110 |
return entity_info
|
|
|
|
| 116 |
label="Input Sentence",
|
| 117 |
placeholder="Enter your sentence here:",
|
| 118 |
)
|
| 119 |
+
output_entities = gr.HTML(label="Linked Entities:")
|
| 120 |
|
| 121 |
# Interface definition
|
| 122 |
interface = gr.Interface(
|