Neelanjan commited on
Commit
8257b79
1 Parent(s): c211f72

Upload cuet.py

Browse files
Files changed (1) hide show
  1. cuet.py +216 -0
cuet.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Untitled1.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1OpumpFAYHp3dJhfH9ZUWpQRDx9FqOVOd
8
+ """
9
+
10
+
11
+
12
+ import requests
13
+ from bs4 import BeautifulSoup
14
+ import pandas as pd
15
+ import matplotlib.pyplot as plt
16
+
17
+ def extract_question_options(url):
18
+ response = requests.get(url)
19
+ soup = BeautifulSoup(response.content, 'html.parser')
20
+ tables = soup.find_all('table', class_='menu-tbl')
21
+
22
+ question_ids = []
23
+ chosen_options = []
24
+ option_1_ids = []
25
+ option_2_ids = []
26
+ option_3_ids = []
27
+ option_4_ids = []
28
+
29
+ for table in tables:
30
+ question_id = table.find('td', string='Question ID :').find_next('td').text
31
+ chosen_option = table.find('td', string='Chosen Option :').find_next('td').text
32
+ option_1_id = table.find('td', string='Option 1 ID :').find_next('td').text
33
+ option_2_id = table.find('td', string='Option 2 ID :').find_next('td').text
34
+ option_3_id = table.find('td', string='Option 3 ID :').find_next('td').text
35
+ option_4_id = table.find('td', string='Option 4 ID :').find_next('td').text
36
+
37
+ status = table.find('td', string='Status :').find_next('td').text
38
+ if 'Not Answered' in status or 'Marked For Review' in status:
39
+ chosen_option = 'Not Attempted'
40
+
41
+ question_ids.append(question_id)
42
+ chosen_options.append(chosen_option)
43
+ option_1_ids.append(option_1_id)
44
+ option_2_ids.append(option_2_id)
45
+ option_3_ids.append(option_3_id)
46
+ option_4_ids.append(option_4_id)
47
+
48
+ data = {
49
+ 'Question ID': question_ids,
50
+ 'Chosen Option': chosen_options,
51
+ 'Option 1 ID': option_1_ids,
52
+ 'Option 2 ID': option_2_ids,
53
+ 'Option 3 ID': option_3_ids,
54
+ 'Option 4 ID': option_4_ids
55
+ }
56
+ df = pd.DataFrame(data)
57
+
58
+ new_data = []
59
+ for _, row in df.iterrows():
60
+ chosen_option = row['Chosen Option']
61
+ question_id = row['Question ID']
62
+ if chosen_option == 'Not Attempted':
63
+ option_id = 'Not Attempted'
64
+ else:
65
+ option_id = row[f'Option {chosen_option} ID']
66
+
67
+ new_data.append({'Question ID': question_id, 'My Options(s)': option_id})
68
+
69
+ new_df = pd.DataFrame(new_data)
70
+ return new_df
71
+
72
+ def extract_question_info(data):
73
+ lines = data.split("\n")
74
+
75
+ result = []
76
+ skip_row = False
77
+
78
+ for line in lines:
79
+ if line:
80
+ if skip_row:
81
+ skip_row = False
82
+ continue
83
+
84
+ parts = line.split("\t")
85
+ question_id = parts[2]
86
+ correct_option = ""
87
+ for option in parts[3:]:
88
+ if option != "None of These":
89
+ correct_option = option
90
+ break
91
+
92
+ result.append({"Question ID": question_id, "Correct Option(s)": correct_option})
93
+ skip_row = True
94
+
95
+ df = pd.DataFrame(result)
96
+ return df
97
+
98
+ def compare_answers(data, url):
99
+ # Call extract_question_info to get the ans_df DataFrame
100
+ ans_df = extract_question_info(data)
101
+
102
+ # Call extract_question_options to get the new_df DataFrame
103
+ new_df = extract_question_options(url)
104
+
105
+ # Merge the two DataFrames based on the 'Question ID' column
106
+ merged_df = ans_df.merge(new_df, on='Question ID', how='inner')
107
+
108
+ # Compare the Correct Option(s) and My Options(s) columns and assign marks
109
+ merged_df['Marks'] = merged_df.apply(lambda row: 4 if row['Correct Option(s)'] == row['My Options(s)']
110
+ else (-1 if row['My Options(s)'] != 'Not Attempted' else 0), axis=1)
111
+
112
+ # Calculate total marks
113
+ total_marks = len(ans_df) * 4
114
+
115
+ # Calculate number of wrong answers
116
+ wrong_answers = len(merged_df[merged_df['Marks'] == -1])
117
+
118
+ # Calculate number of right answers
119
+ right_answers = len(merged_df[merged_df['Marks'] == 4])
120
+
121
+ # Calculate number of not attempted questions
122
+ not_attempted = len(new_df[new_df['My Options(s)'] == 'Not Attempted'])
123
+
124
+ # Calculate marks obtained
125
+ marks_obtained = merged_df['Marks'].sum()
126
+
127
+ # Calculate percentage score
128
+ percentage_score = (marks_obtained / total_marks) * 100
129
+
130
+ # Create the markdown text
131
+ text = f"Total Marks: {total_marks}\n"
132
+ text += f"Number of Wrong Answers: {wrong_answers}\n"
133
+ text += f"Number of Right Answers: {right_answers}\n"
134
+ text += f"Number of Not Attempted Questions: {not_attempted}\n"
135
+ text += f"Marks Obtained: {marks_obtained}\n"
136
+ text += f"Percentage Score: {percentage_score}\n"
137
+
138
+ # Plotting the overall performance
139
+ labels = ['Right Answers', 'Wrong Answers', 'Not Attempted']
140
+ sizes = [right_answers, wrong_answers, not_attempted]
141
+ colors = ['#66BB6A', '#EF5350', '#FFA726']
142
+ plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
143
+ plt.axis('equal')
144
+ plt.title('Overall Performance')
145
+
146
+ return text, merged_df, plt
147
+
148
+ import gradio as gr
149
+
150
+ with gr.Blocks(theme='gradio/soft') as demo:
151
+ gr.Markdown("""
152
+ ## FOLLOW THIS STEPS TO EXTRACT THE DATA
153
+ ![Image](https://i.imgur.com/9dzYJZ1.gif)
154
+
155
+ """)
156
+ data = gr.Textbox(label="Correct Options in The Website",placeholder=
157
+ """1 Data Science Artificial Intelligence_Eng - PART A 123456789 987654321
158
+ 987654321 987654322 987654323 987654324 None of These
159
+ 2 Data Science Artificial Intelligence_Eng - PART A 234567890 123456789
160
+ 123456789 123456790 123456791 123456792 None of These
161
+ 3 Data Science Artificial Intelligence_Eng - PART A 345678901 234567890
162
+ 234567890 234567891 234567892 234567893 None of These
163
+ 4 Data Science Artificial Intelligence_Eng - PART A 456789012 345678901
164
+ 345678901 345678902 345678903 345678904 None of These
165
+ 5 Data Science Artificial Intelligence_Eng - PART A 567890123 456789012
166
+ 456789012 456789013 456789014 456789015 None of These
167
+ 6 Data Science Artificial Intelligence_Eng - PART A 678901234 567890123
168
+ 567890123 567890124 567890125 567890126 None of These
169
+ 7 Data Science Artificial Intelligence_Eng - PART A 789012345 678901234
170
+ 678901234 678901235 678901236 678901237 None of These
171
+ 8 Data Science Artificial Intelligence_Eng - PART A 890123456 789012345
172
+ 789012345 789012346 789012347 789012348 None of These
173
+ 9 Data Science Artificial Intelligence_Eng - PART A 901234567 890123456
174
+ 890123456 890123457 890123458 890123459 None of These
175
+ 10 Data Science Artificial Intelligence_Eng - PART A 123456789 901234567
176
+ 901234567 901234568 901234569 901234570 None of These
177
+ 11 Data Science Artificial Intelligence_Eng - PART A 234567890 123456789
178
+ 123456789 123456790 123456791 123456792 None of These
179
+ 12 Data Science Artificial Intelligence_Eng - PART A 345678901 234567890
180
+ 234567890 234567891 234567892 234567893 None of These
181
+ 13 Data Science Artificial Intelligence_Eng - PART A 456789012 345678901
182
+ 345678901 345678902 345678903
183
+ .
184
+ .
185
+ .
186
+ .
187
+ 95 Data Science Artificial Intelligence_Eng - PART A 678901234 567890123
188
+ 567890123 567890124 567890125 567890126 None of These
189
+ 96 Data Science Artificial Intelligence_Eng - PART A 789012345 678901234
190
+ 678901234 678901235 678901236 678901237 None of These
191
+ 97 Data Science Artificial Intelligence_Eng - PART A 890123456 789012345
192
+ 789012345 789012346 789012347 789012348 None of These
193
+ 98 Data Science Artificial Intelligence_Eng - PART A 901234567 890123456
194
+ 890123456 890123457 890123458 890123459 None of These
195
+ 99 Data Science Artificial Intelligence_Eng - PART A 123456789 901234567
196
+ 901234567 901234568 901234569 901234570 None of These
197
+ 100 Data Science Artificial Intelligence_Eng - PART A 234567890 123456789
198
+ 123456789 123456790 123456791 123456792 None of These""", lines=5)
199
+ gr.Markdown("""
200
+
201
+ ![Image](https://i.ibb.co/FVwGm6L/Screenshot-179.png)
202
+
203
+ """)
204
+ url = gr.Textbox(label="Link to your Answers URL",placeholder="https://cdn3.digialm.com//per/g28/pub/XXXX/touchstone/AssessmentQPHTMLMode1//XXXXXXXX/XXXXXXXX/XXXXXXXX/XXXXXXXXXXX.html")
205
+ btn = gr.Button(value="Check Your Answer!")
206
+ out = gr.Textbox(value="", label="Output")
207
+ out1 = gr.Plot()
208
+ out2=gr.Dataframe()
209
+ btn.click(compare_answers, inputs=[data, url], outputs=[out,out2,out1])
210
+ gr.Markdown("""
211
+
212
+ if __name__ == "__main__":
213
+ demo.launch(debug= True,share=True)
214
+
215
+
216
+