File size: 1,721 Bytes
767d296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7638e13
767d296
 
 
7638e13
 
767d296
 
 
 
 
7638e13
767d296
 
 
 
 
 
 
 
 
 
cd21882
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F

# Load model
model_name = "tmt3103/BioASQ-yesno-PudMedBERT"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

def predict_yesno(context, question):
    inputs = tokenizer.encode_plus(
        question,
        context,
        return_tensors="pt",
        truncation=True,
        max_length=512,
        padding="max_length"
    )

    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        probs = F.softmax(logits, dim=1).squeeze()
        pred_id = logits.argmax().item()

    label = "Yes" if pred_id == 1 else "No"
    return f"{label} (Confidence: {probs[pred_id]:.2f})"

# Gradio
with gr.Blocks() as demo:
    gr.Markdown("BioASQ Yes/No Question Answering")
    gr.Markdown("""
    This demo uses a fine-tuned BERT model to answer biomedical yes/no questions based on context.<br>
    **Instructions**:
    1. Type the context and your yes/no question.
    2. Click 'Predict' to get the answer.
    """)

    with gr.Row():
        with gr.Column():
            context_input = gr.Textbox(label="Context", lines=8, placeholder="Paste biomedical context here...")
            question_input = gr.Textbox(label="Question", lines=2, placeholder="Enter your question here...")
            predict_button = gr.Button("Predict")
        with gr.Column():
            output = gr.Textbox(label="Prediction")

    predict_button.click(
        fn=predict_yesno,
        inputs=[context_input, question_input],
        outputs=output
    )

demo.launch(share=True)