Spaces:
Sleeping
Sleeping
pratikshahp
commited on
Commit
•
58ad313
1
Parent(s):
dbe0f9f
Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Utility package /helperfile
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import traceback
|
5 |
+
import PyPDF2
|
6 |
+
import requests
|
7 |
+
from bs4 import BeautifulSoup as Soup
|
8 |
+
|
9 |
+
def read_file(file):
|
10 |
+
if file.name.endswith(".pdf"):
|
11 |
+
try:
|
12 |
+
pdf_reader=PyPDF2.PdfFileReader(file)
|
13 |
+
text=""
|
14 |
+
for page in pdf_reader.pages:
|
15 |
+
text+=page.extract_text()
|
16 |
+
return text
|
17 |
+
except Exception as e:
|
18 |
+
raise Exception("error reading the pdf file")
|
19 |
+
elif file.name.endswith(".txt"):
|
20 |
+
return file.read().decode("utf-8")
|
21 |
+
else:
|
22 |
+
raise Exception(
|
23 |
+
"unsupported file format! only pdf and text file supported"
|
24 |
+
)
|
25 |
+
def get_table_data(quiz_str):
|
26 |
+
try:
|
27 |
+
#convert quiz from string to dictinary
|
28 |
+
quiz_dict=json.loads(quiz_str)
|
29 |
+
quiz_table_data=[]
|
30 |
+
|
31 |
+
#iterate over the quiz dictionary and extract the required information
|
32 |
+
for key, value in quiz_dict.items():
|
33 |
+
mcq = value["mcq"]
|
34 |
+
options = " | ".join(
|
35 |
+
[
|
36 |
+
f"{option}: {option_value}"
|
37 |
+
for option, option_value in value["options"].items()
|
38 |
+
]
|
39 |
+
)
|
40 |
+
correct = value["correct"]
|
41 |
+
quiz_table_data.append({"MCQ": mcq, "Choices": options, "Correct": correct})
|
42 |
+
|
43 |
+
return quiz_table_data
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
traceback.print_exception(type(e), e, e.__traceback__)
|
47 |
+
return False
|
48 |
+
|