File size: 2,047 Bytes
71e6ce1
 
 
d1ebd33
 
71e6ce1
 
d1ebd33
71e6ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
d1ebd33
71e6ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a77f996
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
"""
app.py
======
Gradio app for HF Spaces (ZeroGPU) — deploys the best single model
(DeBERTa-v3-small, 0.7544 MAP@3 on the Kaggle leaderboard).
"""

import spaces
import gradio as gr

from src.models.deberta import DeBERTaModel

_model = None


def get_model():
    global _model
    if _model is None:
        _model = DeBERTaModel().load()
    return _model


@spaces.GPU
def predict(prompt, opt_a, opt_b, opt_c, opt_d, opt_e):
    if not prompt or not prompt.strip():
        return "⚠️ Please enter a question.", {}

    options = [opt_a, opt_b, opt_c, opt_d, opt_e]
    if any(not (o or "").strip() for o in options):
        return "⚠️ Please fill in all 5 options (A-E).", {}

    model = get_model()
    proba = model.predict_proba_single(prompt, options)
    top3 = model.predict_top3_single(prompt, options)

    option_text = {"A": opt_a, "B": opt_b, "C": opt_c, "D": opt_d, "E": opt_e}
    medal = ["🥇", "🥈", "🥉"]
    answer_text = "\n".join(
        f"{medal[i]} **{letter}**: {option_text[letter]}" for i, letter in enumerate(top3)
    )

    confidences = {letter: float(score) for letter, score in zip("ABCDE", proba)}
    return answer_text, confidences


demo = gr.Interface(
    fn=predict,
    inputs=[
        gr.Textbox(label="Question", lines=3,
                    placeholder="e.g. What is the powerhouse of the cell?"),
        gr.Textbox(label="Option A"),
        gr.Textbox(label="Option B"),
        gr.Textbox(label="Option C"),
        gr.Textbox(label="Option D"),
        gr.Textbox(label="Option E"),
    ],
    outputs=[
        gr.Markdown(label="Top-3 Answer"),
        gr.Label(label="Confidence per option", num_top_classes=5),
    ],
    title="🧠 Smart MCQ Solver — DeBERTa-v3-small",
    description=(
        "Answers 5-option multiple-choice questions using a fine-tuned "
        "**DeBERTa-v3-small** model — the best individual model in this "
        "project (0.7544 MAP@3 on the Kaggle leaderboard)."
    ),
)

if __name__ == "__main__":
    demo.launch(ssr_mode=False)