a-01a commited on
Commit
31edbfc
·
verified ·
1 Parent(s): 99cbc5b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +204 -0
app.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+
3
+ import os
4
+ import torch
5
+ import numpy as np
6
+ import gradio as gr
7
+ from scipy.stats import rankdata
8
+ from scipy.special import softmax
9
+ from transformers import (
10
+ AutoModelForCausalLM,
11
+ AutoModelForMultipleChoice,
12
+ AutoModelForSequenceClassification,
13
+ AutoTokenizer,
14
+ BitsAndBytesConfig
15
+ )
16
+ from peft import PeftModel
17
+
18
+ HF_TOKEN = os.environ.get("HF_TOKEN")
19
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
20
+
21
+ FOLDS = [1]
22
+
23
+ BASE_MODEL_NAME_QWEN = "Qwen/Qwen3-4B-Instruct-2507"
24
+ MODEL1_RAG_QWEN3_REPO = "a-01a/QSolver_Decoder_V16"
25
+ MODEL2_DEBERTA_REPO = "a-01a/QSolver_Encoder_V2"
26
+ MODEL3_SCRATCH_REPO = "a-01a/QSolver_Scratch"
27
+
28
+ OPTION_LETTERS = ["A", "B", "C", "D", "E"]
29
+ IDX_TO_LABEL = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
30
+
31
+ models_loaded = False
32
+ tokenizer_m1 = None
33
+ model1 = None
34
+ tokenizer_m2 = None
35
+ model2 = None
36
+ tokenizer_m3 = None
37
+ model3 = None
38
+ qwen_option_token_ids = None
39
+
40
+ def sharpen_probs_single(probs, power=1.5):
41
+ p_pow = np.power(probs, power)
42
+ return p_pow / np.sum(p_pow)
43
+
44
+ def to_percentile_ranks_single(probs):
45
+ return rankdata(probs) / len(probs)
46
+
47
+ def format_qwen3_prompt(context, question, opts):
48
+ ctx_line = f"Context: {context.strip()}\n" if context.strip() else ""
49
+ return (
50
+ f"<|im_start|>system\n"
51
+ f"You are a scientific expert. Base your answer STRICTLY on the provided Context. "
52
+ f"Output ONLY the single letter corresponding to the correct option (A, B, C, D, or E).<|im_end|>\n"
53
+ f"<|im_start|>user\n"
54
+ f"{ctx_line}Question: {question}\n"
55
+ f"A) {opts['A']}\nB) {opts['B']}\nC) {opts['C']}\nD) {opts['D']}\nE) {opts['E']}<|im_end|>\n"
56
+ f"<|im_start|>assistant\n"
57
+ )
58
+
59
+ def get_answer_token_id(tokenizer, letter):
60
+ probe = "<|im_start|>assistant\n"
61
+ return tokenizer(probe + letter, add_special_tokens=False)["input_ids"][len(tokenizer(probe, add_special_tokens=False)["input_ids"]):][0]
62
+
63
+ def load_all_models():
64
+ global models_loaded, tokenizer_m1, model1, tokenizer_m2, model2, tokenizer_m3, model3, qwen_option_token_ids
65
+ if models_loaded:
66
+ return
67
+
68
+ device = "cuda"
69
+
70
+ bnb_config = BitsAndBytesConfig(
71
+ load_in_4bit=True,
72
+ bnb_4bit_quant_type="nf4",
73
+ bnb_4bit_compute_dtype=torch.float16
74
+ )
75
+
76
+ tokenizer_m1 = AutoTokenizer.from_pretrained(BASE_MODEL_NAME_QWEN, token=HF_TOKEN, use_fast=True)
77
+ tokenizer_m1.truncation_side = "left"
78
+ tokenizer_m1.padding_side = "left"
79
+ if tokenizer_m1.pad_token is None:
80
+ tokenizer_m1.pad_token = tokenizer_m1.eos_token
81
+
82
+ qwen_base = AutoModelForCausalLM.from_pretrained(
83
+ BASE_MODEL_NAME_QWEN,
84
+ quantization_config=bnb_config,
85
+ device_map="auto",
86
+ token=HF_TOKEN
87
+ )
88
+ model1 = PeftModel.from_pretrained(qwen_base, MODEL1_RAG_QWEN3_REPO, subfolder=f"fold_{FOLDS[0]}", token=HF_TOKEN)
89
+ model1.eval()
90
+
91
+ qwen_option_token_ids = [get_answer_token_id(tokenizer_m1, l) for l in OPTION_LETTERS]
92
+
93
+ tokenizer_m2 = AutoTokenizer.from_pretrained(MODEL2_DEBERTA_REPO, subfolder=f"fold_{FOLDS[0]}", token=HF_TOKEN, use_fast=True)
94
+ model2 = AutoModelForMultipleChoice.from_pretrained(
95
+ MODEL2_DEBERTA_REPO,
96
+ subfolder=f"fold_{FOLDS[0]}",
97
+ token=HF_TOKEN,
98
+ torch_dtype=torch.float16
99
+ ).to(device)
100
+ model2.eval()
101
+
102
+ tokenizer_m3 = AutoTokenizer.from_pretrained(MODEL3_SCRATCH_REPO, subfolder=f"fold_{FOLDS[0]}", token=HF_TOKEN, use_fast=True)
103
+ model3 = AutoModelForSequenceClassification.from_pretrained(
104
+ MODEL3_SCRATCH_REPO,
105
+ subfolder=f"fold_{FOLDS[0]}",
106
+ token=HF_TOKEN,
107
+ torch_dtype=torch.float16,
108
+ num_labels=1
109
+ ).to(device)
110
+ model3.eval()
111
+
112
+ models_loaded = True
113
+
114
+ @spaces.GPU
115
+ @torch.inference_mode()
116
+ def run_inference(context, question, opt_a, opt_b, opt_c, opt_d, opt_e):
117
+ load_all_models()
118
+ device = "cuda"
119
+
120
+ opts = {"A": opt_a, "B": opt_b, "C": opt_c, "D": opt_d, "E": opt_e}
121
+
122
+ qwen_prompt = format_qwen3_prompt(context, question, opts)
123
+ inputs_m1 = tokenizer_m1([qwen_prompt], return_tensors="pt", padding=True, truncation=True, max_length=1280).to(device)
124
+ out_m1 = model1(**inputs_m1)
125
+ logits_m1 = out_m1.logits[:, -1, qwen_option_token_ids].float().cpu().numpy()[0]
126
+ m1_probs = softmax(logits_m1)
127
+
128
+ q_repeats = [question] * 5
129
+ opt_list = [opts["A"], opts["B"], opts["C"], opts["D"], opts["E"]]
130
+ inputs_m2 = tokenizer_m2(q_repeats, opt_list, padding=True, truncation=True, max_length=512, return_tensors="pt")
131
+ inputs_m2 = {k: v.unsqueeze(0).to(device) for k, v in inputs_m2.items()}
132
+ out_m2 = model2(**inputs_m2)
133
+ m2_probs = softmax(out_m2.logits.cpu().numpy()[0])
134
+
135
+ scratch_texts = [f"Question: {question}\nOption: {opt}" for opt in opt_list]
136
+ inputs_m3 = tokenizer_m3(scratch_texts, padding=True, truncation=True, max_length=256, return_tensors="pt").to(device)
137
+ out_m3 = model3(**inputs_m3)
138
+ logits_m3 = out_m3.logits.squeeze(-1).cpu().numpy()
139
+ m3_probs = softmax(logits_m3)
140
+
141
+ m1_sharp = sharpen_probs_single(m1_probs, power=1.5)
142
+ m2_sharp = sharpen_probs_single(m2_probs, power=1.8)
143
+ m3_sharp = sharpen_probs_single(m3_probs, power=1.5)
144
+
145
+ m1_ranks = to_percentile_ranks_single(m1_probs)
146
+ m2_ranks = to_percentile_ranks_single(m2_probs)
147
+ m3_ranks = to_percentile_ranks_single(m3_probs)
148
+
149
+ deb_sorted = np.sort(m2_probs)[::-1]
150
+ margin = deb_sorted[0] - deb_sorted[1]
151
+
152
+ if margin >= 0.35:
153
+ w_deb, w_scratch, w_qwen = 0.82, 0.11, 0.07
154
+ elif margin >= 0.15:
155
+ w_deb, w_scratch, w_qwen = 0.62, 0.23, 0.15
156
+ else:
157
+ w_deb, w_scratch, w_qwen = 0.42, 0.34, 0.24
158
+
159
+ p_blend = (w_deb * m2_sharp) + (w_scratch * m3_sharp) + (w_qwen * m1_sharp)
160
+ r_blend = (w_deb * m2_ranks) + (w_scratch * m3_ranks) + (w_qwen * m1_ranks)
161
+
162
+ final_blend_scores = (0.85 * p_blend) + (0.15 * r_blend)
163
+
164
+ top3_idx = np.argsort(-final_blend_scores)[:3]
165
+ top3_labels = " ".join([IDX_TO_LABEL[i] for i in top3_idx])
166
+
167
+ def dict_format(probs):
168
+ return {IDX_TO_LABEL[i]: float(probs[i]) for i in range(5)}
169
+
170
+ return dict_format(m1_probs), dict_format(m2_probs), dict_format(m3_probs), top3_labels
171
+
172
+ with gr.Blocks(title="TriModel Optimized Ensemble", theme=gr.themes.Soft()) as demo:
173
+ gr.Markdown("TriModel Optimized MCQ Solver Ensemble")
174
+ gr.Markdown("Input a question and 5 options. Optionally, provide context for Qwen3's RAG component. The application dynamically weights the 3 models based on DeBERTa's prediction confidence margin.")
175
+
176
+ with gr.Row():
177
+ with gr.Column(scale=2):
178
+ context_in = gr.Textbox(label="Context (Optional RAG input)", lines=3, placeholder="Paste background text here...")
179
+ question_in = gr.Textbox(label="Question", lines=2, placeholder="What is the process of...")
180
+ opt_a_in = gr.Textbox(label="Option A")
181
+ opt_b_in = gr.Textbox(label="Option B")
182
+ opt_c_in = gr.Textbox(label="Option C")
183
+ opt_d_in = gr.Textbox(label="Option D")
184
+ opt_e_in = gr.Textbox(label="Option E")
185
+ submit_btn = gr.Button("Run Inference", variant="primary")
186
+
187
+ with gr.Column(scale=3):
188
+ gr.Markdown("Ensemble Final Prediction (Map@3)")
189
+ ensemble_out = gr.Textbox(label="Top 3 Predictions", text_align="center", scale=2)
190
+
191
+ gr.Markdown("Individual Model Probabilities")
192
+ with gr.Row():
193
+ m1_out = gr.Label(label="Model 1: Qwen3 RAG (4B)")
194
+ m2_out = gr.Label(label="Model 2: DeBERTa-v3 Encoder")
195
+ m3_out = gr.Label(label="Model 3: Scratch Classifier")
196
+
197
+ submit_btn.click(
198
+ fn=run_inference,
199
+ inputs=[context_in, question_in, opt_a_in, opt_b_in, opt_c_in, opt_d_in, opt_e_in],
200
+ outputs=[m1_out, m2_out, m3_out, ensemble_out]
201
+ )
202
+
203
+ if __name__ == "__main__":
204
+ demo.launch()