roshcheeku commited on
Commit
95b865d
·
verified ·
1 Parent(s): 54be244

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -81
app.py CHANGED
@@ -1,88 +1,9 @@
1
- from flask import Flask, request, jsonify
2
- import os
3
- import uuid
4
- 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()])
21
-
22
- def extract_text_from_docx(filepath):
23
- doc = Document(filepath)
24
- return "\n".join([para.text for para in doc.paragraphs])
25
-
26
- def extract_text_from_excel(filepath):
27
- wb = openpyxl.load_workbook(filepath)
28
- sheet = wb.active
29
- text = ""
30
- for row in sheet.iter_rows(min_row=2, values_only=True):
31
- if any(row):
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:
38
- return jsonify({'error': 'No file provided'}), 400
39
-
40
- file = request.files['file']
41
- filename = secure_filename(file.filename)
42
- ext = filename.split('.')[-1].lower()
43
-
44
- uid = str(uuid.uuid4())
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
- quiz_data_store[quiz_id] = mcqs
60
-
61
- return jsonify({'quiz_id': quiz_id, 'mcqs': mcqs})
62
-
63
- @app.route('/submit', methods=['POST'])
64
- def submit_quiz():
65
- data = request.json
66
- quiz_id = data.get('quiz_id')
67
- user_answers = data.get('answers')
68
-
69
- if quiz_id not in quiz_data_store:
70
- return jsonify({'error': 'Invalid quiz ID'}), 404
71
-
72
- mcqs = quiz_data_store[quiz_id]
73
- correct = 0
74
- for i, ans in enumerate(user_answers):
75
- if i < len(mcqs) and ans.upper() == mcqs[i]['answer'].upper():
76
- correct += 1
77
-
78
- total = len(mcqs)
79
- accuracy = round((correct / total) * 100, 2) if total else 0
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)
 
1
+ from flask import Flask
 
 
 
 
 
 
 
 
2
  app = Flask(__name__)
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  @app.route('/')
5
  def home():
6
+ return "Hello, World!"
7
 
8
  if __name__ == '__main__':
9
  app.run(host='0.0.0.0', port=7860)