mcq / app.py
ni20032004's picture
Upload app.py with huggingface_hub
587fa09 verified
Raw
History Blame Contribute Delete
1.48 kB
import pickle
import gradio as gr
from scipy.sparse import hstack
OPTIONS = ["A","B","C","D","E"]
with open("model.pkl","rb") as f:
data = pickle.load(f)
tfw = data["tfw"]
tfc = data["tfc"]
clf = data["clf"]
def predict(question, a, b, c, d, e):
options = [a, b, c, d, e]
texts = [question + " [SEP] " + o for o in options]
X = hstack([tfw.transform(texts), tfc.transform(texts)])
probs = clf.predict_proba(X)[:, 1]
ranked = sorted(zip(OPTIONS, options, probs), key=lambda x: x[2], reverse=True)
out = "## Top 3 Predictions\n\n"
medals = ["πŸ₯‡", "πŸ₯ˆ", "πŸ₯‰"]
for i, (letter, text, score) in enumerate(ranked[:3]):
out += f"{medals[i]} **Option {letter}** β€” Score: {score:.3f}\n\n{text}\n\n"
return out
with gr.Blocks(title="Smart MCQ Solver") as demo:
gr.Markdown("# 🧠 Smart MCQ Solver β€” 22f3001980")
gr.Markdown("TF-IDF + Logistic Regression Pairwise Ranker")
question = gr.Textbox(lines=3, label="Question")
with gr.Row():
a = gr.Textbox(label="Option A")
b = gr.Textbox(label="Option B")
c = gr.Textbox(label="Option C")
with gr.Row():
d = gr.Textbox(label="Option D")
e = gr.Textbox(label="Option E")
btn = gr.Button("πŸ” Predict Top 3", variant="primary")
out = gr.Markdown(label="Result")
btn.click(fn=predict, inputs=[question,a,b,c,d,e], outputs=out)
demo.launch(server_name="0.0.0.0", server_port=7860, block=True)