Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,222 +1,214 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import numpy as np
|
| 6 |
-
import pickle
|
| 7 |
-
import re
|
| 8 |
-
import gradio as gr
|
| 9 |
-
from transformers import DebertaV2Model, DebertaV2Tokenizer
|
| 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 |
-
df['
|
| 47 |
-
df['
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
mf =
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# ==========================
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
self.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
nn.
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
nn.
|
| 77 |
-
nn.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
nn.
|
| 83 |
-
nn.
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# ==========================
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
with open(
|
| 107 |
-
|
| 108 |
-
with open(
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
gr.
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
if __name__ == "__main__":
|
| 216 |
-
iface.launch(
|
| 217 |
-
server_name="0.0.0.0", # Allow external connections
|
| 218 |
-
server_port=7860, # Default Gradio port
|
| 219 |
-
share=True, # Create public link
|
| 220 |
-
debug=False, # Disable debug mode for production
|
| 221 |
-
show_error=True # Show errors to users
|
| 222 |
-
)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pickle
|
| 7 |
+
import re
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from transformers import DebertaV2Model, DebertaV2Tokenizer
|
| 10 |
+
|
| 11 |
+
# ==========================
|
| 12 |
+
# Configuration
|
| 13 |
+
# ==========================
|
| 14 |
+
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 15 |
+
MAX_LENGTH = 256
|
| 16 |
+
MODELS_DIR = './models/'
|
| 17 |
+
CAT_ENCODER_PATH = os.path.join(MODELS_DIR, 'cat_encoder.pkl')
|
| 18 |
+
MISC_ENCODER_PATH = os.path.join(MODELS_DIR, 'misc_encoder.pkl')
|
| 19 |
+
FEATURE_COLS_PATH = os.path.join(MODELS_DIR, 'feature_cols.pkl')
|
| 20 |
+
DEFAULT_MODEL = 'map_2025_best_model_fold7.pt'
|
| 21 |
+
|
| 22 |
+
# ==========================
|
| 23 |
+
# Feature Extraction
|
| 24 |
+
# ==========================
|
| 25 |
+
def extract_math_features(text):
|
| 26 |
+
if not isinstance(text, str):
|
| 27 |
+
return {
|
| 28 |
+
'frac_count': 0, 'number_count': 0, 'operator_count': 0,
|
| 29 |
+
'decimal_count': 0, 'question_mark': 0, 'math_keyword_count': 0
|
| 30 |
+
}
|
| 31 |
+
features = {
|
| 32 |
+
'frac_count': len(re.findall(r'FRAC_\d+_\d+|\\frac', text)),
|
| 33 |
+
'number_count': len(re.findall(r'\b\d+\b', text)),
|
| 34 |
+
'operator_count': len(re.findall(r'[\+\-\*\/\=]', text)),
|
| 35 |
+
'decimal_count': len(re.findall(r'\d+\.\d+', text)),
|
| 36 |
+
'question_mark': int('?' in text),
|
| 37 |
+
'math_keyword_count': len(re.findall(r'solve|calculate|equation|fraction|decimal', text.lower()))
|
| 38 |
+
}
|
| 39 |
+
return features
|
| 40 |
+
|
| 41 |
+
def create_features(df):
|
| 42 |
+
for col in ['QuestionText', 'MC_Answer', 'StudentExplanation']:
|
| 43 |
+
df[col] = df[col].fillna('')
|
| 44 |
+
df['mc_answer_len'] = df['MC_Answer'].str.len()
|
| 45 |
+
df['explanation_len'] = df['StudentExplanation'].str.len()
|
| 46 |
+
df['question_len'] = df['QuestionText'].str.len()
|
| 47 |
+
df['explanation_to_question_ratio'] = df['explanation_len'] / (df['question_len'] + 1)
|
| 48 |
+
for col in ['QuestionText', 'MC_Answer', 'StudentExplanation']:
|
| 49 |
+
mf = df[col].apply(extract_math_features).apply(pd.Series)
|
| 50 |
+
prefix = 'mc_' if col == 'MC_Answer' else 'exp_' if col == 'StudentExplanation' else ''
|
| 51 |
+
mf.columns = [f'{prefix}{c}' for c in mf.columns]
|
| 52 |
+
df = pd.concat([df, mf], axis=1)
|
| 53 |
+
df['sentence'] = (
|
| 54 |
+
"Question: " + df['QuestionText'] +
|
| 55 |
+
" Answer: " + df['MC_Answer'] +
|
| 56 |
+
" Explanation: " + df['StudentExplanation']
|
| 57 |
+
)
|
| 58 |
+
return df
|
| 59 |
+
|
| 60 |
+
# ==========================
|
| 61 |
+
# Model Definition
|
| 62 |
+
# ==========================
|
| 63 |
+
class MathMisconceptionModel(nn.Module):
|
| 64 |
+
def __init__(self, n_categories, n_misconceptions, feature_dim):
|
| 65 |
+
super().__init__()
|
| 66 |
+
self.bert = DebertaV2Model.from_pretrained('microsoft/deberta-v3-small')
|
| 67 |
+
self.tokenizer = DebertaV2Tokenizer.from_pretrained('microsoft/deberta-v3-small')
|
| 68 |
+
self.feature_processor = nn.Sequential(
|
| 69 |
+
nn.Linear(feature_dim, 64),
|
| 70 |
+
nn.ReLU(),
|
| 71 |
+
nn.Dropout(0.3)
|
| 72 |
+
)
|
| 73 |
+
self.category_head = nn.Sequential(
|
| 74 |
+
nn.Linear(768 + 64, 256),
|
| 75 |
+
nn.ReLU(),
|
| 76 |
+
nn.Dropout(0.2),
|
| 77 |
+
nn.Linear(256, n_categories)
|
| 78 |
+
)
|
| 79 |
+
self.misconception_head = nn.Sequential(
|
| 80 |
+
nn.Linear(768 + 64, 256),
|
| 81 |
+
nn.ReLU(),
|
| 82 |
+
nn.Dropout(0.2),
|
| 83 |
+
nn.Linear(256, n_misconceptions)
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def forward(self, input_texts, features):
|
| 87 |
+
tokens = self.tokenizer(
|
| 88 |
+
input_texts,
|
| 89 |
+
padding=True,
|
| 90 |
+
truncation=True,
|
| 91 |
+
max_length=MAX_LENGTH,
|
| 92 |
+
return_tensors="pt"
|
| 93 |
+
).to(DEVICE)
|
| 94 |
+
outputs = self.bert(**tokens)
|
| 95 |
+
text_emb = outputs.last_hidden_state[:, 0, :]
|
| 96 |
+
feat_emb = self.feature_processor(features)
|
| 97 |
+
combined = torch.cat([text_emb, feat_emb], dim=1)
|
| 98 |
+
return self.category_head(combined), self.misconception_head(combined)
|
| 99 |
+
|
| 100 |
+
# ==========================
|
| 101 |
+
# Load Resources
|
| 102 |
+
# ==========================
|
| 103 |
+
try:
|
| 104 |
+
with open(CAT_ENCODER_PATH, 'rb') as f:
|
| 105 |
+
cat_enc = pickle.load(f)
|
| 106 |
+
with open(MISC_ENCODER_PATH, 'rb') as f:
|
| 107 |
+
misc_enc = pickle.load(f)
|
| 108 |
+
with open(FEATURE_COLS_PATH, 'rb') as f:
|
| 109 |
+
feature_cols = pickle.load(f)
|
| 110 |
+
except FileNotFoundError as e:
|
| 111 |
+
print(f"Error loading resources: {e}")
|
| 112 |
+
exit()
|
| 113 |
+
|
| 114 |
+
# Dummy scaler (no scaling)
|
| 115 |
+
class IdentityScaler:
|
| 116 |
+
def transform(self, X):
|
| 117 |
+
return X
|
| 118 |
+
|
| 119 |
+
scaler = IdentityScaler()
|
| 120 |
+
|
| 121 |
+
# ==========================
|
| 122 |
+
# Prediction Logic
|
| 123 |
+
# ==========================
|
| 124 |
+
def predict(model_name, question, mc_answer, explanation, export_csv):
|
| 125 |
+
model_path = os.path.join(MODELS_DIR, model_name)
|
| 126 |
+
if not os.path.exists(model_path):
|
| 127 |
+
return "Model not found.", None
|
| 128 |
+
|
| 129 |
+
data = {
|
| 130 |
+
'QuestionText': [question],
|
| 131 |
+
'MC_Answer': [mc_answer],
|
| 132 |
+
'StudentExplanation': [explanation]
|
| 133 |
+
}
|
| 134 |
+
df = pd.DataFrame(data)
|
| 135 |
+
|
| 136 |
+
processed_df = create_features(df.copy())
|
| 137 |
+
for col in feature_cols:
|
| 138 |
+
if col not in processed_df.columns:
|
| 139 |
+
processed_df[col] = 0
|
| 140 |
+
features = processed_df[feature_cols].fillna(0).values
|
| 141 |
+
features_scaled = scaler.transform(features)
|
| 142 |
+
|
| 143 |
+
model = MathMisconceptionModel(
|
| 144 |
+
n_categories=len(cat_enc.classes_),
|
| 145 |
+
n_misconceptions=len(misc_enc.classes_),
|
| 146 |
+
feature_dim=features_scaled.shape[1]
|
| 147 |
+
).to(DEVICE)
|
| 148 |
+
|
| 149 |
+
model.load_state_dict(torch.load(model_path, map_location=DEVICE))
|
| 150 |
+
model.eval()
|
| 151 |
+
|
| 152 |
+
text = processed_df['sentence'].tolist()
|
| 153 |
+
features_tensor = torch.tensor(features_scaled, dtype=torch.float).to(DEVICE)
|
| 154 |
+
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
cat_logits, misc_logits = model(text, features_tensor)
|
| 157 |
+
cat_pred = torch.argmax(cat_logits, 1).cpu().item()
|
| 158 |
+
misc_pred = torch.argmax(misc_logits, 1).cpu().item()
|
| 159 |
+
|
| 160 |
+
predicted_category = cat_enc.inverse_transform([cat_pred])[0]
|
| 161 |
+
predicted_misconception = misc_enc.inverse_transform([misc_pred])[0]
|
| 162 |
+
|
| 163 |
+
result_text = (
|
| 164 |
+
f"Predicted Category: {predicted_category}\n"
|
| 165 |
+
f"Predicted Misconception: {predicted_misconception}"
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
csv_path = None
|
| 169 |
+
if export_csv:
|
| 170 |
+
export_df = pd.DataFrame([{
|
| 171 |
+
"Question": question,
|
| 172 |
+
"MC_Answer": mc_answer,
|
| 173 |
+
"Student_Explanation": explanation,
|
| 174 |
+
"Predicted_Category": predicted_category,
|
| 175 |
+
"Predicted_Misconception": predicted_misconception,
|
| 176 |
+
"Model_Used": model_name
|
| 177 |
+
}])
|
| 178 |
+
csv_path = "predictions.csv"
|
| 179 |
+
file_exists = os.path.isfile(csv_path)
|
| 180 |
+
export_df.to_csv(csv_path, mode='a', header=not file_exists, index=False)
|
| 181 |
+
|
| 182 |
+
return result_text, csv_path
|
| 183 |
+
|
| 184 |
+
# ==========================
|
| 185 |
+
# Gradio UI
|
| 186 |
+
# ==========================
|
| 187 |
+
model_files = [f for f in os.listdir(MODELS_DIR) if f.endswith('.pt')]
|
| 188 |
+
|
| 189 |
+
iface = gr.Interface(
|
| 190 |
+
fn=predict,
|
| 191 |
+
inputs=[
|
| 192 |
+
gr.Dropdown(model_files, value=DEFAULT_MODEL, label="Select Model"),
|
| 193 |
+
gr.Textbox(label="Enter Question", lines=3),
|
| 194 |
+
gr.Textbox(label="Enter Correct Answer (MC_Answer)", lines=1),
|
| 195 |
+
gr.Textbox(label="Enter Student's Explanation", lines=5),
|
| 196 |
+
gr.Checkbox(label="Export Prediction to CSV")
|
| 197 |
+
],
|
| 198 |
+
outputs=[
|
| 199 |
+
gr.Textbox(label="Prediction Result"),
|
| 200 |
+
gr.File(label="CSV File (if exported)")
|
| 201 |
+
],
|
| 202 |
+
title="Math Misconception Predictor",
|
| 203 |
+
description="Select a model and provide the question, correct answer, and student's explanation to get a prediction.",
|
| 204 |
+
theme=gr.themes.Soft()
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
iface.launch(
|
| 209 |
+
server_name="0.0.0.0",
|
| 210 |
+
server_port=7860,
|
| 211 |
+
share=True,
|
| 212 |
+
debug=False,
|
| 213 |
+
show_error=True
|
| 214 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|