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