Update app.py
Browse files
app.py
CHANGED
@@ -12,34 +12,39 @@ dataset_iter = load_dataset(
|
|
12 |
).shuffle(seed=42) # Shuffle added
|
13 |
|
14 |
# Load the model
|
15 |
-
model = GLiNER.from_pretrained("
|
16 |
|
17 |
def ner(text: str, labels: str, threshold: float, nested_ner: bool):
|
18 |
# Convert user-provided labels (comma-separated string) into a list
|
19 |
labels_list = [label.strip() for label in labels.split(",")]
|
20 |
|
|
|
|
|
|
|
|
|
21 |
# Predict entities using the GLiNER model
|
22 |
-
entities = model.predict_entities(
|
23 |
|
24 |
# Prepare entities for color-coded display using gr.HighlightedText
|
25 |
highlights = [(ent["start"], ent["end"], ent["label"]) for ent in entities]
|
26 |
|
|
|
27 |
return {
|
28 |
-
"text":
|
29 |
"entities": highlights
|
30 |
-
}
|
31 |
|
32 |
with gr.Blocks(title="General NER with Color-Coded Output") as demo:
|
33 |
gr.Markdown(
|
34 |
"""
|
35 |
# General Entity Recognition Demo
|
36 |
-
This demo selects a random text snippet from a subset of the British Library's books dataset and identifies entities using
|
37 |
"""
|
38 |
)
|
39 |
|
40 |
# Display a random example
|
41 |
input_text = gr.Textbox(
|
42 |
-
value="
|
43 |
label="Text input",
|
44 |
placeholder="Enter your text here",
|
45 |
lines=5
|
@@ -47,7 +52,7 @@ with gr.Blocks(title="General NER with Color-Coded Output") as demo:
|
|
47 |
|
48 |
with gr.Row() as row:
|
49 |
labels = gr.Textbox(
|
50 |
-
value="
|
51 |
label="Labels",
|
52 |
placeholder="Enter your labels here (comma separated)",
|
53 |
scale=2,
|
|
|
12 |
).shuffle(seed=42) # Shuffle added
|
13 |
|
14 |
# Load the model
|
15 |
+
model = GLiNER.from_pretrained("max-long/textile_machines_3_oct", trust_remote_code=True)
|
16 |
|
17 |
def ner(text: str, labels: str, threshold: float, nested_ner: bool):
|
18 |
# Convert user-provided labels (comma-separated string) into a list
|
19 |
labels_list = [label.strip() for label in labels.split(",")]
|
20 |
|
21 |
+
# Truncate the text to avoid length exceeding model limits (e.g., 384 tokens)
|
22 |
+
max_length = 384
|
23 |
+
truncated_text = text[:max_length]
|
24 |
+
|
25 |
# Predict entities using the GLiNER model
|
26 |
+
entities = model.predict_entities(truncated_text, labels_list, flat_ner=not nested_ner, threshold=threshold)
|
27 |
|
28 |
# Prepare entities for color-coded display using gr.HighlightedText
|
29 |
highlights = [(ent["start"], ent["end"], ent["label"]) for ent in entities]
|
30 |
|
31 |
+
# Return both the highlighted text and the raw entities in JSON format
|
32 |
return {
|
33 |
+
"text": truncated_text,
|
34 |
"entities": highlights
|
35 |
+
}, entities # Return both outputs: the first for HighlightedText, the second for JSON
|
36 |
|
37 |
with gr.Blocks(title="General NER with Color-Coded Output") as demo:
|
38 |
gr.Markdown(
|
39 |
"""
|
40 |
# General Entity Recognition Demo
|
41 |
+
This demo selects a random text snippet from a subset of the British Library's books dataset and identifies entities using a fine-tuned GLiNER model. You can specify the entities you want to find, and results will be displayed in a color-coded format.
|
42 |
"""
|
43 |
)
|
44 |
|
45 |
# Display a random example
|
46 |
input_text = gr.Textbox(
|
47 |
+
value="The machine is fed by means of an endless apron, the wool entering at the smaller end...",
|
48 |
label="Text input",
|
49 |
placeholder="Enter your text here",
|
50 |
lines=5
|
|
|
52 |
|
53 |
with gr.Row() as row:
|
54 |
labels = gr.Textbox(
|
55 |
+
value="Machine, Wool", # Default example labels
|
56 |
label="Labels",
|
57 |
placeholder="Enter your labels here (comma separated)",
|
58 |
scale=2,
|