Spaces:
Build error
Build error
Rohit Diwane
commited on
Commit
·
5ec1a12
1
Parent(s):
3945df2
Create utlis.py
Browse files
utlis.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import PyPDF2
|
3 |
+
import json
|
4 |
+
import traceback
|
5 |
+
|
6 |
+
|
7 |
+
def read_file(file):
|
8 |
+
if file.name.endswith(".pdf"):
|
9 |
+
try:
|
10 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
11 |
+
text=""
|
12 |
+
for page in pdf_reader.pages:
|
13 |
+
text+=page.extract_text()
|
14 |
+
return text
|
15 |
+
|
16 |
+
except Exception as e:
|
17 |
+
raise Exception("error reading the PDF file")
|
18 |
+
|
19 |
+
elif file.name.endswith(".txt"):
|
20 |
+
return file.read().decode("utf-8")
|
21 |
+
|
22 |
+
else:
|
23 |
+
raise Exception(
|
24 |
+
"unsupported file format only pdf and text file suppoted"
|
25 |
+
|
26 |
+
)
|
27 |
+
|
28 |
+
def get_table_data(quiz_str):
|
29 |
+
try:
|
30 |
+
# convert the quiz from a str to dict
|
31 |
+
quiz_dict=json.loads(quiz_str)
|
32 |
+
quiz_table_data=[]
|
33 |
+
|
34 |
+
# iterate over the quiz dictionary and extract the required information
|
35 |
+
for key,value in quiz_dict.items():
|
36 |
+
mcq=value["mcq"]
|
37 |
+
options=" || ".join(
|
38 |
+
[
|
39 |
+
f"{option}-> {option_value}" for option, option_value in value["options"].items()
|
40 |
+
|
41 |
+
]
|
42 |
+
)
|
43 |
+
|
44 |
+
correct=value["correct"]
|
45 |
+
quiz_table_data.append({"MCQ": mcq, "Choices": options, "Correct": correct})
|
46 |
+
|
47 |
+
return quiz_table_data
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
traceback.print_exception(type(e), e, e.__traceback__)
|
51 |
+
return False
|