Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Your private repo ID
|
| 5 |
+
model_id = "Statistical-Impossibility/Feline-NER-Test"
|
| 6 |
+
|
| 7 |
+
# Load NER pipeline
|
| 8 |
+
ner_pipeline = pipeline("token-classification", model=model_id, aggregation_strategy="simple")
|
| 9 |
+
|
| 10 |
+
def ner_predict(text):
|
| 11 |
+
results = ner_pipeline(text)
|
| 12 |
+
|
| 13 |
+
# Sort by start position for clean order
|
| 14 |
+
results = sorted(results, key=lambda x: x['start'])
|
| 15 |
+
|
| 16 |
+
# Build highlighted HTML
|
| 17 |
+
highlighted = ""
|
| 18 |
+
last_end = 0
|
| 19 |
+
for r in results:
|
| 20 |
+
# Add text before entity
|
| 21 |
+
highlighted += text[last_end:r['start']]
|
| 22 |
+
# Color map
|
| 23 |
+
color = {
|
| 24 |
+
"SYMPTOM": "yellow",
|
| 25 |
+
"DISEASE": "red",
|
| 26 |
+
"MEDICATION": "lightgreen",
|
| 27 |
+
"PROCEDURE": "lightblue",
|
| 28 |
+
"ANATOMY": "orange"
|
| 29 |
+
}.get(r['entity_group'], "gray")
|
| 30 |
+
# Highlight entity
|
| 31 |
+
highlighted += f"<mark style='background-color:{color}; padding:0.1em 0.2em; border-radius:3px;'>{r['word']}</mark>"
|
| 32 |
+
last_end = r['end']
|
| 33 |
+
highlighted += text[last_end:] # Remaining text
|
| 34 |
+
|
| 35 |
+
# Also return plain list
|
| 36 |
+
entity_list = "\n".join([f"{r['entity_group']}: {r['word']} (score: {r['score']:.3f})" for r in results])
|
| 37 |
+
|
| 38 |
+
return highlighted, entity_list
|
| 39 |
+
|
| 40 |
+
# Gradio interface
|
| 41 |
+
with gr.Blocks(title="Feline Veterinary NER Demo") as demo:
|
| 42 |
+
gr.Markdown("# Feline Veterinary NER Demo")
|
| 43 |
+
gr.Markdown("Paste a veterinary text – model highlights entities.")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column(scale=2):
|
| 47 |
+
input_text = gr.Textbox(label="Input Text", lines=10, placeholder="Paste article/abstract here...")
|
| 48 |
+
with gr.Column(scale=2):
|
| 49 |
+
output_html = gr.HTML(label="Highlighted Output")
|
| 50 |
+
output_list = gr.Textbox(label="Extracted Entities", lines=10)
|
| 51 |
+
|
| 52 |
+
btn = gr.Button("Run NER")
|
| 53 |
+
btn.click(
|
| 54 |
+
fn=ner_predict,
|
| 55 |
+
inputs=input_text,
|
| 56 |
+
outputs=[output_html, output_list]
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
gr.Examples(
|
| 60 |
+
examples=[
|
| 61 |
+
["A cat presented with vomiting and lethargy due to chronic kidney disease treated with subcutaneous fluids."],
|
| 62 |
+
["Ultrasound revealed adrenal mass consistent with hyperaldosteronism."]
|
| 63 |
+
],
|
| 64 |
+
inputs=input_text
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
demo.launch()
|