Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the token classification pipeline using your model. | |
| # Note: aggregation_strategy="simple" will merge sub-word tokens so that you get a single label per entity. | |
| token_classifier = pipeline( | |
| "token-classification", | |
| model="almo762/biobert-dependency-parsing-v1.7", | |
| aggregation_strategy="simple" | |
| ) | |
| def classify_tokens(text: str): | |
| # Run the model on the input text | |
| predictions = token_classifier(text) | |
| # Prepare a table output: each row contains Token, Label, Confidence Score | |
| results = [] | |
| for pred in predictions: | |
| # The pipeline returns dictionaries with keys like "word", "entity_group", and "score". | |
| token = pred.get("word", "") | |
| label = pred.get("entity_group", "") | |
| score = pred.get("score", 0.0) | |
| results.append([token, label, f"{score:.2f}"]) | |
| # If no predictions were found, return a friendly message | |
| if not results: | |
| return [["No tokens found", "", ""]] | |
| return results | |
| examples = [ | |
| "Mice transgenic for the human T cell leukemia virus (HTLV-I) Tax gene develop fibroblastic tumors that express NF-kappa B-inducible early genes.", | |
| "In vitro inhibition of NF-kappa B expression by antisense oligodeoxynucleotides (ODNs) inhibited growth of these culture-adapted Tax-transformed fibroblasts as well as an HTLV-I-transformed human lymphocyte line.", | |
| "In contrast, antisense inhibition of Tax itself had no apparent effect on cell growth.", | |
| "Mice treated with antisense to NF-kappa B ODNs showed rapid regression of transplanted fibrosarcomas.", | |
| "This suggests that NF-kappa B expression may be necessary for the maintenance of the malignant phenotype and provides a therapeutic approach for HTLV-I-associated disease." | |
| ] | |
| # Build the Gradio interface. | |
| demo = gr.Interface( | |
| fn=classify_tokens, | |
| inputs=gr.Textbox(lines=4, placeholder="Enter text here..."), | |
| outputs=gr.Dataframe(headers=["Token", "Label", "Confidence Score"]), | |
| title="BioBERT Dependency Parsing Demo", | |
| description="Enter text to see the token classification output: each token is shown with its assigned label and confidence score. The label can be interpreted after reading the paper: Viable Dependency Parsing as Sequence Labeling. The encode label we used here is Relative Part-of-Speech-based, since Part-of-Speech is easier to read than other types of encoding.", | |
| examples=examples | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |