Neelanjan's picture
Update app.py
0875fdf
raw
history blame
8.21 kB
# -*- coding: utf-8 -*-
"""Untitled1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1OpumpFAYHp3dJhfH9ZUWpQRDx9FqOVOd
"""
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
def extract_question_options(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
tables = soup.find_all('table', class_='menu-tbl')
question_ids = []
chosen_options = []
option_1_ids = []
option_2_ids = []
option_3_ids = []
option_4_ids = []
for table in tables:
question_id = table.find('td', string='Question ID :').find_next('td').text
chosen_option = table.find('td', string='Chosen Option :').find_next('td').text
option_1_id = table.find('td', string='Option 1 ID :').find_next('td').text
option_2_id = table.find('td', string='Option 2 ID :').find_next('td').text
option_3_id = table.find('td', string='Option 3 ID :').find_next('td').text
option_4_id = table.find('td', string='Option 4 ID :').find_next('td').text
status = table.find('td', string='Status :').find_next('td').text
if 'Not Answered' in status or 'Marked For Review' in status:
chosen_option = 'Not Attempted'
question_ids.append(question_id)
chosen_options.append(chosen_option)
option_1_ids.append(option_1_id)
option_2_ids.append(option_2_id)
option_3_ids.append(option_3_id)
option_4_ids.append(option_4_id)
data = {
'Question ID': question_ids,
'Chosen Option': chosen_options,
'Option 1 ID': option_1_ids,
'Option 2 ID': option_2_ids,
'Option 3 ID': option_3_ids,
'Option 4 ID': option_4_ids
}
df = pd.DataFrame(data)
new_data = []
for _, row in df.iterrows():
chosen_option = row['Chosen Option']
question_id = row['Question ID']
if chosen_option == 'Not Attempted':
option_id = 'Not Attempted'
else:
option_id = row[f'Option {chosen_option} ID']
new_data.append({'Question ID': question_id, 'My Options(s)': option_id})
new_df = pd.DataFrame(new_data)
return new_df
def extract_question_info(data):
lines = data.split("\n")
result = []
skip_row = False
for line in lines:
if line:
if skip_row:
skip_row = False
continue
parts = line.split("\t")
question_id = parts[2]
correct_option = ""
for option in parts[3:]:
if option != "None of These":
correct_option = option
break
result.append({"Question ID": question_id, "Correct Option(s)": correct_option})
skip_row = True
df = pd.DataFrame(result)
return df
def compare_answers(data, url):
# Call extract_question_info to get the ans_df DataFrame
ans_df = extract_question_info(data)
# Call extract_question_options to get the new_df DataFrame
new_df = extract_question_options(url)
# Merge the two DataFrames based on the 'Question ID' column
merged_df = ans_df.merge(new_df, on='Question ID', how='inner')
# Compare the Correct Option(s) and My Options(s) columns and assign marks
merged_df['Marks'] = merged_df.apply(lambda row: 4 if row['Correct Option(s)'] == row['My Options(s)']
else (-1 if row['My Options(s)'] != 'Not Attempted' else 0), axis=1)
# Calculate total marks
total_marks = len(ans_df) * 4
# Calculate number of wrong answers
wrong_answers = len(merged_df[merged_df['Marks'] == -1])
# Calculate number of right answers
right_answers = len(merged_df[merged_df['Marks'] == 4])
# Calculate number of not attempted questions
not_attempted = len(new_df[new_df['My Options(s)'] == 'Not Attempted'])
# Calculate marks obtained
marks_obtained = merged_df['Marks'].sum()
# Calculate percentage score
percentage_score = (marks_obtained / total_marks) * 100
# Create the markdown text
text = f"Total Marks: {total_marks}\n"
text += f"Number of Wrong Answers: {wrong_answers}\n"
text += f"Number of Right Answers: {right_answers}\n"
text += f"Number of Not Attempted Questions: {not_attempted}\n"
text += f"Marks Obtained: {marks_obtained}\n"
text += f"Percentage Score: {percentage_score}\n"
# Plotting the overall performance
labels = ['Right Answers', 'Wrong Answers', 'Not Attempted']
sizes = [right_answers, wrong_answers, not_attempted]
colors = ['#66BB6A', '#EF5350', '#FFA726']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
plt.axis('equal')
plt.title('Overall Performance')
return text, merged_df, plt
import gradio as gr
with gr.Blocks(theme='gradio/soft') as demo:
gr.Markdown("""
## FOLLOW THIS STEPS TO EXTRACT THE DATA
![Image](https://i.imgur.com/9dzYJZ1.gif)
""")
data = gr.Textbox(label="Correct Options in The Website",placeholder=
"""1 Data Science Artificial Intelligence_Eng - PART A 123456789 987654321
987654321 987654322 987654323 987654324 None of These
2 Data Science Artificial Intelligence_Eng - PART A 234567890 123456789
123456789 123456790 123456791 123456792 None of These
3 Data Science Artificial Intelligence_Eng - PART A 345678901 234567890
234567890 234567891 234567892 234567893 None of These
4 Data Science Artificial Intelligence_Eng - PART A 456789012 345678901
345678901 345678902 345678903 345678904 None of These
5 Data Science Artificial Intelligence_Eng - PART A 567890123 456789012
456789012 456789013 456789014 456789015 None of These
6 Data Science Artificial Intelligence_Eng - PART A 678901234 567890123
567890123 567890124 567890125 567890126 None of These
7 Data Science Artificial Intelligence_Eng - PART A 789012345 678901234
678901234 678901235 678901236 678901237 None of These
8 Data Science Artificial Intelligence_Eng - PART A 890123456 789012345
789012345 789012346 789012347 789012348 None of These
9 Data Science Artificial Intelligence_Eng - PART A 901234567 890123456
890123456 890123457 890123458 890123459 None of These
10 Data Science Artificial Intelligence_Eng - PART A 123456789 901234567
901234567 901234568 901234569 901234570 None of These
11 Data Science Artificial Intelligence_Eng - PART A 234567890 123456789
123456789 123456790 123456791 123456792 None of These
12 Data Science Artificial Intelligence_Eng - PART A 345678901 234567890
234567890 234567891 234567892 234567893 None of These
13 Data Science Artificial Intelligence_Eng - PART A 456789012 345678901
345678901 345678902 345678903
.
.
.
.
95 Data Science Artificial Intelligence_Eng - PART A 678901234 567890123
567890123 567890124 567890125 567890126 None of These
96 Data Science Artificial Intelligence_Eng - PART A 789012345 678901234
678901234 678901235 678901236 678901237 None of These
97 Data Science Artificial Intelligence_Eng - PART A 890123456 789012345
789012345 789012346 789012347 789012348 None of These
98 Data Science Artificial Intelligence_Eng - PART A 901234567 890123456
890123456 890123457 890123458 890123459 None of These
99 Data Science Artificial Intelligence_Eng - PART A 123456789 901234567
901234567 901234568 901234569 901234570 None of These
100 Data Science Artificial Intelligence_Eng - PART A 234567890 123456789
123456789 123456790 123456791 123456792 None of These""", lines=5)
gr.Markdown("![Image](https://i.ibb.co/FVwGm6L/Screenshot-179.png)")
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")
btn = gr.Button(value="Check Your Answer!")
out = gr.Textbox(value="", label="Output")
out1 = gr.Plot()
out2=gr.Dataframe()
btn.click(compare_answers, inputs=[data, url], outputs=[out,out2,out1])
gr.Markdown("Made with :heart: by Neelanjan Chakraborty")
if __name__ == "__main__":
demo.launch(debug= True,share=True)