Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions backend/Generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Any, List, Mapping, Tuple
import re
import os
import uuid
import fitz
import mammoth

Expand Down Expand Up @@ -368,20 +369,28 @@
return result.value

def process_file(self, file):
file_path = os.path.join(self.upload_folder, file.filename)
file.save(file_path)
original_filename = os.path.basename(file.filename or "")
_, ext = os.path.splitext(original_filename)
lower_ext = ext.lower()
safe_filename = f"{uuid.uuid4().hex}{lower_ext}"
file_path = os.path.join(self.upload_folder, safe_filename)
content = ""

if file.filename.endswith('.txt'):
with open(file_path, 'r') as f:
content = f.read()
elif file.filename.endswith('.pdf'):
content = self.extract_text_from_pdf(file_path)
elif file.filename.endswith('.docx'):
content = self.extract_text_from_docx(file_path)

os.remove(file_path)
return content
try:
file.save(file_path)
if lower_ext == '.txt':
with open(file_path, 'r') as f:
content = f.read()
elif lower_ext == '.pdf':
content = self.extract_text_from_pdf(file_path)
elif lower_ext == '.docx':
content = self.extract_text_from_docx(file_path)
return content
finally:
try:
os.remove(file_path)
except FileNotFoundError:
pass

class QuestionGenerator:
"""A transformer-based NLP system for generating reading comprehension-style questions from
Expand Down
Loading