Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,16 +5,21 @@ import pdfplumber
|
|
| 5 |
from docx import Document
|
| 6 |
import openpyxl
|
| 7 |
from werkzeug.utils import secure_filename
|
|
|
|
| 8 |
from model_utils import extract_mcqs_with_model
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
-
# Use /tmp/uploads to avoid permission errors
|
| 13 |
app.config['UPLOAD_FOLDER'] = '/tmp/uploads'
|
| 14 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 15 |
|
| 16 |
quiz_data_store = {}
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
def extract_text_from_pdf(filepath):
|
| 19 |
with pdfplumber.open(filepath) as pdf:
|
| 20 |
return "\n".join([p.extract_text() for p in pdf.pages if p.extract_text()])
|
|
@@ -32,6 +37,31 @@ def extract_text_from_excel(filepath):
|
|
| 32 |
text += " ".join([str(cell) for cell in row if cell is not None]) + "\n"
|
| 33 |
return text
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@app.route('/upload', methods=['POST'])
|
| 36 |
def upload_file():
|
| 37 |
if 'file' not in request.files:
|
|
@@ -45,21 +75,29 @@ def upload_file():
|
|
| 45 |
save_path = os.path.join(app.config['UPLOAD_FOLDER'], uid + '_' + filename)
|
| 46 |
file.save(save_path)
|
| 47 |
|
| 48 |
-
if ext == 'pdf':
|
| 49 |
-
text = extract_text_from_pdf(save_path)
|
| 50 |
-
elif ext == 'docx':
|
| 51 |
-
text = extract_text_from_docx(save_path)
|
| 52 |
-
elif ext in ['xls', 'xlsx']:
|
| 53 |
-
text = extract_text_from_excel(save_path)
|
| 54 |
-
else:
|
| 55 |
-
return jsonify({'error': 'Unsupported file type'}), 400
|
| 56 |
-
|
| 57 |
-
mcqs = extract_mcqs_with_model(text)
|
| 58 |
quiz_id = str(uuid.uuid4())
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
return jsonify({'
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
@app.route('/submit', methods=['POST'])
|
| 64 |
def submit_quiz():
|
| 65 |
data = request.json
|
|
@@ -80,9 +118,15 @@ def submit_quiz():
|
|
| 80 |
|
| 81 |
return jsonify({'score': correct, 'total': total, 'accuracy': accuracy})
|
| 82 |
|
|
|
|
|
|
|
|
|
|
| 83 |
@app.route('/')
|
| 84 |
def home():
|
| 85 |
return "MCQ Extraction Flask API is running!"
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
if __name__ == '__main__':
|
| 88 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 5 |
from docx import Document
|
| 6 |
import openpyxl
|
| 7 |
from werkzeug.utils import secure_filename
|
| 8 |
+
from threading import Thread
|
| 9 |
from model_utils import extract_mcqs_with_model
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
| 13 |
+
# Use /tmp/uploads to avoid permission errors
|
| 14 |
app.config['UPLOAD_FOLDER'] = '/tmp/uploads'
|
| 15 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 16 |
|
| 17 |
quiz_data_store = {}
|
| 18 |
+
processing_results = {}
|
| 19 |
|
| 20 |
+
# -------------------------------
|
| 21 |
+
# Text Extraction Functions
|
| 22 |
+
# -------------------------------
|
| 23 |
def extract_text_from_pdf(filepath):
|
| 24 |
with pdfplumber.open(filepath) as pdf:
|
| 25 |
return "\n".join([p.extract_text() for p in pdf.pages if p.extract_text()])
|
|
|
|
| 37 |
text += " ".join([str(cell) for cell in row if cell is not None]) + "\n"
|
| 38 |
return text
|
| 39 |
|
| 40 |
+
# -------------------------------
|
| 41 |
+
# Background Thread Function
|
| 42 |
+
# -------------------------------
|
| 43 |
+
def background_process(file_path, quiz_id, ext):
|
| 44 |
+
try:
|
| 45 |
+
if ext == 'pdf':
|
| 46 |
+
text = extract_text_from_pdf(file_path)
|
| 47 |
+
elif ext == 'docx':
|
| 48 |
+
text = extract_text_from_docx(file_path)
|
| 49 |
+
elif ext in ['xls', 'xlsx']:
|
| 50 |
+
text = extract_text_from_excel(file_path)
|
| 51 |
+
else:
|
| 52 |
+
processing_results[quiz_id] = {'error': 'Unsupported file type'}
|
| 53 |
+
return
|
| 54 |
+
|
| 55 |
+
mcqs = extract_mcqs_with_model(text)
|
| 56 |
+
processing_results[quiz_id] = {'mcqs': mcqs}
|
| 57 |
+
quiz_data_store[quiz_id] = mcqs
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
processing_results[quiz_id] = {'error': str(e)}
|
| 61 |
+
|
| 62 |
+
# -------------------------------
|
| 63 |
+
# Non-blocking Upload Route
|
| 64 |
+
# -------------------------------
|
| 65 |
@app.route('/upload', methods=['POST'])
|
| 66 |
def upload_file():
|
| 67 |
if 'file' not in request.files:
|
|
|
|
| 75 |
save_path = os.path.join(app.config['UPLOAD_FOLDER'], uid + '_' + filename)
|
| 76 |
file.save(save_path)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
quiz_id = str(uuid.uuid4())
|
| 79 |
+
thread = Thread(target=background_process, args=(save_path, quiz_id, ext))
|
| 80 |
+
thread.start()
|
| 81 |
+
|
| 82 |
+
return jsonify({'quiz_id': quiz_id, 'status': 'processing'})
|
| 83 |
+
|
| 84 |
+
# -------------------------------
|
| 85 |
+
# Status Check Route
|
| 86 |
+
# -------------------------------
|
| 87 |
+
@app.route('/status/<quiz_id>', methods=['GET'])
|
| 88 |
+
def check_status(quiz_id):
|
| 89 |
+
if quiz_id not in processing_results:
|
| 90 |
+
return jsonify({'status': 'processing'}), 202
|
| 91 |
+
|
| 92 |
+
result = processing_results[quiz_id]
|
| 93 |
+
if 'error' in result:
|
| 94 |
+
return jsonify({'status': 'failed', 'error': result['error']}), 500
|
| 95 |
|
| 96 |
+
return jsonify({'status': 'completed', 'mcqs': result['mcqs']}), 200
|
| 97 |
|
| 98 |
+
# -------------------------------
|
| 99 |
+
# Quiz Submission
|
| 100 |
+
# -------------------------------
|
| 101 |
@app.route('/submit', methods=['POST'])
|
| 102 |
def submit_quiz():
|
| 103 |
data = request.json
|
|
|
|
| 118 |
|
| 119 |
return jsonify({'score': correct, 'total': total, 'accuracy': accuracy})
|
| 120 |
|
| 121 |
+
# -------------------------------
|
| 122 |
+
# Root Route
|
| 123 |
+
# -------------------------------
|
| 124 |
@app.route('/')
|
| 125 |
def home():
|
| 126 |
return "MCQ Extraction Flask API is running!"
|
| 127 |
|
| 128 |
+
# -------------------------------
|
| 129 |
+
# Start the App
|
| 130 |
+
# -------------------------------
|
| 131 |
if __name__ == '__main__':
|
| 132 |
app.run(host='0.0.0.0', port=7860)
|