rohitk123 commited on
Commit
2ce1fec
·
1 Parent(s): c14d608

Deploy Gradio app

Browse files
Files changed (3) hide show
  1. README.md +103 -10
  2. app.py +107 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,14 +1,107 @@
1
  ---
2
- title: Roberta Mcq Solver
3
- emoji: 🐨
4
- colorFrom: purple
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 6.22.0
8
- python_version: '3.12'
9
- app_file: app.py
10
- pinned: false
11
  license: apache-2.0
12
  ---
 
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
 
 
 
 
 
 
 
 
 
2
  license: apache-2.0
3
  ---
4
+ # 🧠 Smart MCQ Solver
5
 
6
+ A Deep Learning based Multiple Choice Question Solver built using **RoBERTa-Base** and Hugging Face Transformers.
7
+
8
+ ---
9
+
10
+ ## Project Overview
11
+
12
+ This project predicts the correct answer among five options for a multiple-choice question.
13
+
14
+ The model is fine-tuned using the Hugging Face `AutoModelForMultipleChoice` architecture.
15
+
16
+ ---
17
+
18
+ ## Features
19
+
20
+ - Fine-tuned RoBERTa-base
21
+ - Multiple Choice Question Answering
22
+ - Gradio Web Interface
23
+ - Hugging Face Transformers
24
+ - PyTorch Implementation
25
+
26
+ ---
27
+
28
+ ## Model
29
+
30
+ RoBERTa-base
31
+
32
+ Task:
33
+
34
+ Multiple Choice Question Answering
35
+
36
+ ---
37
+
38
+ ## Performance
39
+
40
+ | Metric | Score |
41
+ |---------|-------|
42
+ | MAP@3 | **0.9893** |
43
+ | F1 Score | **0.99** |
44
+
45
+ Cross Validation
46
+
47
+ | Fold | MAP@3 |
48
+ |------|--------|
49
+ |1|0.9871|
50
+ |2|0.9854|
51
+ |3|0.9963|
52
+ |4|0.9933|
53
+ |5|0.9846|
54
+
55
+ Mean MAP@3
56
+
57
+ 0.9893
58
+
59
+ ---
60
+
61
+ ## Installation
62
+
63
+ ```bash
64
+ git clone https://github.com/YOUR_USERNAME/smart-mcq-solver.git
65
+
66
+ cd smart-mcq-solver
67
+
68
+ pip install -r requirements.txt
69
+ ```
70
+
71
+ ---
72
+
73
+ ## Run
74
+
75
+ ```bash
76
+ python app.py
77
+ ```
78
+
79
+ ---
80
+
81
+ ## Screenshots
82
+
83
+ ### Home Page
84
+
85
+ ![Loading](image.png)
86
+
87
+ ### Prediction
88
+
89
+ ![Loading](image-1.png)
90
+
91
+ ---
92
+
93
+ ## Tech Stack
94
+
95
+ - Python
96
+ - PyTorch
97
+ - Transformers
98
+ - Hugging Face
99
+ - Gradio
100
+
101
+ ---
102
+
103
+ ## Author
104
+
105
+ Rohit Kumar
106
+
107
+ IIT Madras BS Degree
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+
4
+ from transformers import (
5
+ AutoTokenizer,
6
+ AutoModelForMultipleChoice
7
+ )
8
+
9
+ # -------------------------
10
+ # Device
11
+ # -------------------------
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+
14
+ # -------------------------
15
+ # Load tokenizer
16
+ # -------------------------
17
+ MODEL_NAME = "rohitk123/roberta-base-mcq-solver"
18
+
19
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
20
+ # -------------------------
21
+ # Load model
22
+ # -------------------------
23
+ model = AutoModelForMultipleChoice.from_pretrained(
24
+ MODEL_NAME
25
+ )
26
+
27
+ model.to(device)
28
+ model.eval()
29
+
30
+
31
+ # -------------------------
32
+ # Prediction Function
33
+ # -------------------------
34
+ def predict(question, A, B, C, D, E):
35
+
36
+ choices = [A, B, C, D, E]
37
+
38
+ encoding = tokenizer(
39
+ [question] * 5,
40
+ choices,
41
+ max_length=256,
42
+ truncation=True,
43
+ padding="max_length",
44
+ return_tensors="pt"
45
+ )
46
+
47
+ input_ids = encoding["input_ids"].unsqueeze(0).to(device)
48
+ attention_mask = encoding["attention_mask"].unsqueeze(0).to(device)
49
+
50
+ with torch.no_grad():
51
+
52
+ outputs = model(
53
+ input_ids=input_ids,
54
+ attention_mask=attention_mask
55
+ )
56
+
57
+ logits = outputs.logits
58
+
59
+ probs = torch.softmax(logits, dim=1)[0]
60
+
61
+ letters = ["A", "B", "C", "D", "E"]
62
+
63
+ pred = torch.argmax(probs).item()
64
+
65
+ answer = letters[pred]
66
+
67
+ result = f"Predicted Answer: {answer}\n\n"
68
+
69
+ result += "Scores\n\n"
70
+
71
+ for letter, score in zip(letters, probs):
72
+
73
+ result += f"{letter} : {score.item():.4f}\n"
74
+
75
+ return result
76
+
77
+
78
+ # -------------------------
79
+ # Gradio Interface
80
+ # -------------------------
81
+ demo = gr.Interface(
82
+ fn=predict,
83
+ inputs=[
84
+ gr.Textbox(lines=4, label="Question"),
85
+ gr.Textbox(label="Option A"),
86
+ gr.Textbox(label="Option B"),
87
+ gr.Textbox(label="Option C"),
88
+ gr.Textbox(label="Option D"),
89
+ gr.Textbox(label="Option E"),
90
+ ],
91
+ outputs=gr.Textbox(label="Prediction"),
92
+ title="RoBERTa-Base MCQ Solver",
93
+ description="Enter a question and five options.",
94
+ examples=[
95
+ [
96
+ "Which planet is known as the Red Planet?",
97
+ "Earth",
98
+ "Mars",
99
+ "Venus",
100
+ "Jupiter",
101
+ "Saturn"
102
+ ]
103
+ ]
104
+ )
105
+
106
+ if __name__ == "__main__":
107
+ demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio
4
+ sentencepiece
5
+ accelerate