Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Union
|
| 2 |
+
import sys
|
| 3 |
+
from gliner import GLiNER
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
model = GLiNER.from_pretrained("VAGOsolutions/mmbert_GLiNER").eval()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
examples = [
|
| 11 |
+
# English Example
|
| 12 |
+
[
|
| 13 |
+
"On March 15, 2024, Dr. Sarah Johnson from Boston General Hospital diagnosed patient Michael Thompson, born on June 10, 1980 in New York, with pneumonia. The patient resides at 123 Main Street, Boston, and was prescribed a 7-day course of Amoxicillin 500mg.",
|
| 14 |
+
"date, doctor, hospital, diagnosis, patient, date of birth, place of birth, address, prescription, duration, medication",
|
| 15 |
+
True,
|
| 16 |
+
],
|
| 17 |
+
# German Example
|
| 18 |
+
[
|
| 19 |
+
"Die Firma TechSolutions GmbH, mit Sitz in der Musterstraße 45, Berlin, hat am 20.04.2024 einen Vertrag über 50.000€ mit der Innovations AG abgeschlossen. Der Geschäftsführer Max Müller und die Projektleiterin Anna Schmidt werden das Projekt bis zum 30.06.2024 abschließen.",
|
| 20 |
+
"firma, adresse, datum, vertrag, betrag, partner, geschäftsführer, projektleiter, deadline",
|
| 21 |
+
True,
|
| 22 |
+
],
|
| 23 |
+
# Italian Example
|
| 24 |
+
[
|
| 25 |
+
"La ditta Giardini Belli S.p.A., con partita IVA 01234567890, ha emesso una fattura di 500€ per la consulenza giuridica fornita dall'avvocato Giulia Bianchi, il cui studio si trova in Piazza del Duomo, Milano, dal giorno 01/04/2024.",
|
| 26 |
+
"ragione sociale, partita iva, importo, avvocato, indirizzo, data",
|
| 27 |
+
True,
|
| 28 |
+
],
|
| 29 |
+
# Spanish Example
|
| 30 |
+
[
|
| 31 |
+
"El 12/05/2024, el Banco Nacional de España aprobó un préstamo hipotecario de 300.000€ a favor de Carlos García, empleado en la empresa Tecnología Avanzada S.L., con sede en Calle Gran Vía 25, Madrid. El préstamo tiene un plazo de amortización de 25 años y está garantizado por la propiedad ubicada en Avenida de la Paz 10, Barcelona.",
|
| 32 |
+
"fecha, banco, tipo de préstamo, importe, beneficiario, empleador, dirección empresa, plazo, garantía, dirección propiedad",
|
| 33 |
+
True,
|
| 34 |
+
],
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
def ner(
|
| 38 |
+
text, labels: str, threshold: float, nested_ner: bool
|
| 39 |
+
) -> Dict[str, Union[str, int, float]]:
|
| 40 |
+
labels = labels.split(",")
|
| 41 |
+
return {
|
| 42 |
+
"text": text,
|
| 43 |
+
"entities": [
|
| 44 |
+
{
|
| 45 |
+
"entity": entity["label"],
|
| 46 |
+
"word": entity["text"],
|
| 47 |
+
"start": entity["start"],
|
| 48 |
+
"end": entity["end"],
|
| 49 |
+
"score": 0,
|
| 50 |
+
}
|
| 51 |
+
for entity in model.predict_entities(
|
| 52 |
+
text, labels, flat_ner=not nested_ner, threshold=threshold
|
| 53 |
+
)
|
| 54 |
+
],
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
with gr.Blocks(title="GLiNER-base") as demo:
|
| 58 |
+
gr.Markdown(
|
| 59 |
+
"""
|
| 60 |
+
# GLiNER Multilingual Named Entity Recognition Model
|
| 61 |
+
|
| 62 |
+
### Key Features
|
| 63 |
+
|
| 64 |
+
- **Multilingual Support**: The model supports multiple languages including English, German, Italian, Spanish, and more
|
| 65 |
+
- **Zero-Shot Entity Recognition**: Identify any entity type without requiring retraining
|
| 66 |
+
- **Efficient Architecture**: Lightweight and suitable for resource-constrained environments
|
| 67 |
+
- **Real-Time Performance**: Fast inference suitable for production applications
|
| 68 |
+
|
| 69 |
+
### Performance
|
| 70 |
+
|
| 71 |
+
- **Inference Time**: Optimized for both CPU and GPU environments, delivering fast and efficient entity recognition
|
| 72 |
+
- **Accuracy**: High precision and recall across multiple languages and entity types
|
| 73 |
+
|
| 74 |
+
### Useful Link
|
| 75 |
+
- **Model Page**: [mmbert_GLiNER](https://huggingface.co/VAGOsolutions/mmbert_GLiNER) on Hugging Face
|
| 76 |
+
"""
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
input_text = gr.Textbox(
|
| 80 |
+
value=examples[0][0], label="Text input", placeholder="Enter your text here"
|
| 81 |
+
)
|
| 82 |
+
with gr.Row() as row:
|
| 83 |
+
labels = gr.Textbox(
|
| 84 |
+
value=examples[0][1],
|
| 85 |
+
label="Labels",
|
| 86 |
+
placeholder="Enter your labels here (comma separated)",
|
| 87 |
+
scale=2,
|
| 88 |
+
)
|
| 89 |
+
threshold = gr.Slider(
|
| 90 |
+
0,
|
| 91 |
+
1,
|
| 92 |
+
value=0.3,
|
| 93 |
+
step=0.01,
|
| 94 |
+
label="Threshold",
|
| 95 |
+
info="Lower the threshold to increase how many entities get predicted.",
|
| 96 |
+
scale=1,
|
| 97 |
+
)
|
| 98 |
+
nested_ner = gr.Checkbox(
|
| 99 |
+
value=examples[0][2],
|
| 100 |
+
label="Nested NER",
|
| 101 |
+
info="Allow for nested NER?",
|
| 102 |
+
scale=0,
|
| 103 |
+
)
|
| 104 |
+
output = gr.HighlightedText(label="Predicted Entities")
|
| 105 |
+
submit_btn = gr.Button("Submit")
|
| 106 |
+
examples = gr.Examples(
|
| 107 |
+
examples,
|
| 108 |
+
fn=ner,
|
| 109 |
+
inputs=[input_text, labels, threshold, nested_ner],
|
| 110 |
+
outputs=output,
|
| 111 |
+
cache_examples=True,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Submitting
|
| 115 |
+
input_text.submit(
|
| 116 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
| 117 |
+
)
|
| 118 |
+
labels.submit(
|
| 119 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
| 120 |
+
)
|
| 121 |
+
threshold.release(
|
| 122 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
| 123 |
+
)
|
| 124 |
+
submit_btn.click(
|
| 125 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
| 126 |
+
)
|
| 127 |
+
nested_ner.change(
|
| 128 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
demo.queue()
|
| 132 |
+
demo.launch(debug=True)
|