Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Load your fine-tuned SpanBERT ADR model
|
| 5 |
ner_pipeline = pipeline(
|
| 6 |
"token-classification",
|
| 7 |
-
model=
|
| 8 |
aggregation_strategy="simple"
|
| 9 |
)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def extract_adrs(text):
|
| 12 |
-
# Run the model
|
| 13 |
results = ner_pipeline(text)
|
| 14 |
-
|
| 15 |
-
# Return empty list if no entities detected
|
| 16 |
if not results:
|
| 17 |
return []
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
fn=extract_adrs,
|
| 27 |
-
inputs=gr.Textbox(lines=
|
| 28 |
outputs=gr.JSON(label="Extracted ADRs"),
|
| 29 |
-
title="π
|
| 30 |
-
description="Extracts adverse drug reactions (ADRs) from
|
|
|
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if __name__ == "__main__":
|
| 34 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# ==============================
|
| 5 |
+
# π§ Load model and tokenizer
|
| 6 |
+
# ==============================
|
| 7 |
+
MODEL_NAME = "hazarri/fine_tuned_spanbert"
|
| 8 |
|
|
|
|
| 9 |
ner_pipeline = pipeline(
|
| 10 |
"token-classification",
|
| 11 |
+
model=MODEL_NAME,
|
| 12 |
aggregation_strategy="simple"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 16 |
+
|
| 17 |
+
# ==============================
|
| 18 |
+
# π§ 1οΈβ£ ADR Extraction Function
|
| 19 |
+
# ==============================
|
| 20 |
def extract_adrs(text):
|
|
|
|
| 21 |
results = ner_pipeline(text)
|
|
|
|
|
|
|
| 22 |
if not results:
|
| 23 |
return []
|
| 24 |
+
return [r["word"] for r in results]
|
| 25 |
|
| 26 |
+
# ==============================
|
| 27 |
+
# βοΈ 2οΈβ£ Tokenizer Function
|
| 28 |
+
# ==============================
|
| 29 |
+
def tokenize_text(text):
|
| 30 |
+
tokens = tokenizer.tokenize(text)
|
| 31 |
+
return tokens
|
| 32 |
|
| 33 |
+
# ==============================
|
| 34 |
+
# π¨ Gradio Interfaces
|
| 35 |
+
# ==============================
|
| 36 |
+
adr_interface = gr.Interface(
|
| 37 |
fn=extract_adrs,
|
| 38 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter a medical review..."),
|
| 39 |
outputs=gr.JSON(label="Extracted ADRs"),
|
| 40 |
+
title="π ADR Extraction (SpanBERT)",
|
| 41 |
+
description="Extracts adverse drug reactions (ADRs) from text using a fine-tuned SpanBERT model.",
|
| 42 |
+
api_name="/predict" # β
for remote calls via gradio_client
|
| 43 |
)
|
| 44 |
|
| 45 |
+
tokenizer_interface = gr.Interface(
|
| 46 |
+
fn=tokenize_text,
|
| 47 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter text to tokenize..."),
|
| 48 |
+
outputs=gr.JSON(label="Tokens"),
|
| 49 |
+
title="π€ Tokenizer",
|
| 50 |
+
description="Displays tokens produced by the model's tokenizer.",
|
| 51 |
+
api_name="/tokenize" # β
second endpoint
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Combine both tools
|
| 55 |
+
demo = gr.TabbedInterface(
|
| 56 |
+
[adr_interface, tokenizer_interface],
|
| 57 |
+
["ADR Extraction", "Tokenizer"]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# ==============================
|
| 61 |
+
# π Launch the app
|
| 62 |
+
# ==============================
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# import gradio as gr
|
| 69 |
+
# from transformers import pipeline
|
| 70 |
+
|
| 71 |
+
# # Load your fine-tuned SpanBERT ADR model
|
| 72 |
+
# ner_pipeline = pipeline(
|
| 73 |
+
# "token-classification",
|
| 74 |
+
# model="hazarri/fine_tuned_spanbert", # replace with your actual model name
|
| 75 |
+
# aggregation_strategy="simple"
|
| 76 |
+
# )
|
| 77 |
+
|
| 78 |
+
# def extract_adrs(text):
|
| 79 |
+
# # Run the model
|
| 80 |
+
# results = ner_pipeline(text)
|
| 81 |
+
|
| 82 |
+
# # Return empty list if no entities detected
|
| 83 |
+
# if not results:
|
| 84 |
+
# return []
|
| 85 |
+
|
| 86 |
+
# # Extract only the ADR words
|
| 87 |
+
# adrs = [r["word"] for r in results]
|
| 88 |
+
|
| 89 |
+
# return adrs
|
| 90 |
+
|
| 91 |
+
# # Gradio interface
|
| 92 |
+
# demo = gr.Interface(
|
| 93 |
+
# fn=extract_adrs,
|
| 94 |
+
# inputs=gr.Textbox(lines=5, placeholder="Enter a medical review..."),
|
| 95 |
+
# outputs=gr.JSON(label="Extracted ADRs"),
|
| 96 |
+
# title="π SpanBERT ADR Extraction API",
|
| 97 |
+
# description="Extracts adverse drug reactions (ADRs) from patient reviews using a fine-tuned SpanBERT model."
|
| 98 |
+
# )
|
| 99 |
+
|
| 100 |
+
# if __name__ == "__main__":
|
| 101 |
+
# demo.launch()
|