Spaces:
Runtime error
Runtime error
Download app.py from 2001haitem/Spanbertapi: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/2001haitem/Spanbertapi/resolve/main/app.py
- Command line
-
hf download hf://spaces/2001haitem/Spanbertapi/app.py
-
curl -L -o app.py https://huggingface.co/spaces/2001haitem/Spanbertapi/resolve/main/app.py
2.83 kB
| 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() | |