Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,40 +1,15 @@
|
|
| 1 |
-
#
|
| 2 |
-
import os
|
| 3 |
import json
|
| 4 |
-
import traceback
|
| 5 |
-
import fitz
|
| 6 |
-
import requests
|
| 7 |
-
from bs4 import BeautifulSoup as Soup
|
| 8 |
|
| 9 |
-
def
|
| 10 |
pdf_document = fitz.open(stream=file.read(), filetype="pdf")
|
| 11 |
text = ""
|
| 12 |
for page_num in range(pdf_document.page_count):
|
| 13 |
page = pdf_document.load_page(page_num)
|
| 14 |
text += page.get_text()
|
| 15 |
return text
|
| 16 |
-
|
| 17 |
-
def get_table_data(quiz_str):
|
| 18 |
-
try:
|
| 19 |
-
#convert quiz from string to dictinary
|
| 20 |
-
quiz_dict=json.loads(quiz_str)
|
| 21 |
-
quiz_table_data=[]
|
| 22 |
-
|
| 23 |
-
#iterate over the quiz dictionary and extract the required information
|
| 24 |
-
for key, value in quiz_dict.items():
|
| 25 |
-
mcq = value["mcq"]
|
| 26 |
-
options = " | ".join(
|
| 27 |
-
[
|
| 28 |
-
f"{option}: {option_value}"
|
| 29 |
-
for option, option_value in value["options"].items()
|
| 30 |
-
]
|
| 31 |
-
)
|
| 32 |
-
correct = value["correct"]
|
| 33 |
-
quiz_table_data.append({"MCQ": mcq, "Choices": options, "Correct": correct})
|
| 34 |
-
|
| 35 |
-
return quiz_table_data
|
| 36 |
-
|
| 37 |
-
except Exception as e:
|
| 38 |
-
traceback.print_exception(type(e), e, e.__traceback__)
|
| 39 |
-
return False
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
|
|
|
| 2 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def read_pdf(file):
|
| 5 |
pdf_document = fitz.open(stream=file.read(), filetype="pdf")
|
| 6 |
text = ""
|
| 7 |
for page_num in range(pdf_document.page_count):
|
| 8 |
page = pdf_document.load_page(page_num)
|
| 9 |
text += page.get_text()
|
| 10 |
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def get_table_data():
|
| 13 |
+
with open("response.json", "r") as file:
|
| 14 |
+
data = json.load(file)
|
| 15 |
+
return data["question_format"]
|