Commit
·
33b4f7f
1
Parent(s):
961677f
new index____
Browse files
main.py
CHANGED
@@ -48,6 +48,9 @@ app = Flask(__name__)
|
|
48 |
# Use the new upload directory
|
49 |
UPLOAD_FOLDER = upload_dir # Changed from 'uploads' to upload_dir
|
50 |
|
|
|
|
|
|
|
51 |
@app.route('/')
|
52 |
def index():
|
53 |
return render_template('index.html')
|
@@ -117,50 +120,79 @@ def compute_answers():
|
|
117 |
@app.route('/compute_marks', methods=['POST'])
|
118 |
def compute_marks():
|
119 |
try:
|
120 |
-
# Get answers
|
121 |
a = request.form.get('answers')
|
122 |
if not a:
|
123 |
return jsonify({"error": "No answers provided"}), 400
|
124 |
|
125 |
print("Received answers:", a)
|
126 |
|
|
|
127 |
a = json.loads(a)
|
128 |
answers = []
|
129 |
for i in a:
|
130 |
ans = i.split('\n\n')
|
131 |
answers.append(ans)
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
#
|
138 |
-
|
139 |
-
|
|
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
if
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
results = []
|
158 |
for student_folder, images in data.items():
|
159 |
for idx, image_info in enumerate(images):
|
160 |
try:
|
|
|
161 |
image_path = image_info['path']
|
162 |
s_answer = extract_text_from_image(image_path)
|
163 |
-
|
|
|
|
|
164 |
if idx < len(answers): # Make sure we have corresponding answer
|
165 |
tf_idf_word_values, max_tfidf = create_tfidf_values(answers[idx])
|
166 |
m = marks(s_answer, sen_vec_answers[idx], word_vec_answers[idx],
|
@@ -168,18 +200,18 @@ def compute_marks():
|
|
168 |
|
169 |
if isinstance(m, torch.Tensor):
|
170 |
m = m.item()
|
171 |
-
|
172 |
-
|
173 |
-
'student': student_folder,
|
174 |
-
'image_name': image_info['name'],
|
175 |
-
'marks': round(m, 2)
|
176 |
-
})
|
177 |
else:
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
183 |
except Exception as e:
|
184 |
print(f"Error processing {image_path}: {str(e)}")
|
185 |
results.append({
|
@@ -188,6 +220,7 @@ def compute_marks():
|
|
188 |
'marks': 0
|
189 |
})
|
190 |
|
|
|
191 |
return jsonify({"message": results}), 200
|
192 |
|
193 |
except Exception as e:
|
|
|
48 |
# Use the new upload directory
|
49 |
UPLOAD_FOLDER = upload_dir # Changed from 'uploads' to upload_dir
|
50 |
|
51 |
+
if not os.path.exists('ans_image'):
|
52 |
+
os.makedirs('ans_image')
|
53 |
+
|
54 |
@app.route('/')
|
55 |
def index():
|
56 |
return render_template('index.html')
|
|
|
120 |
@app.route('/compute_marks', methods=['POST'])
|
121 |
def compute_marks():
|
122 |
try:
|
123 |
+
# Get answers from frontend
|
124 |
a = request.form.get('answers')
|
125 |
if not a:
|
126 |
return jsonify({"error": "No answers provided"}), 400
|
127 |
|
128 |
print("Received answers:", a)
|
129 |
|
130 |
+
# Parse answers
|
131 |
a = json.loads(a)
|
132 |
answers = []
|
133 |
for i in a:
|
134 |
ans = i.split('\n\n')
|
135 |
answers.append(ans)
|
136 |
|
137 |
+
print("Processed answers:", answers)
|
138 |
+
|
139 |
+
# Create vectors for reference answers first
|
140 |
+
sen_vec_answers = []
|
141 |
+
word_vec_answers = []
|
142 |
+
for answer_set in answers:
|
143 |
+
temp_v = []
|
144 |
+
temp_w = []
|
145 |
+
for ans in answer_set:
|
146 |
+
temp_v.append(question_vector_sentence(ans))
|
147 |
+
temp_w.append(question_vector_word(ans))
|
148 |
+
sen_vec_answers.append(temp_v)
|
149 |
+
word_vec_answers.append(temp_w)
|
150 |
|
151 |
+
# Process uploaded files
|
152 |
+
files = request.files.getlist('files[]')
|
153 |
+
if not files:
|
154 |
+
return jsonify({"error": "No files uploaded"}), 400
|
155 |
|
156 |
+
data = {}
|
157 |
+
# Process and save uploaded files
|
158 |
+
for file in files:
|
159 |
+
if file.filename.endswith(('.jpg', '.jpeg', '.png')):
|
160 |
+
# Get the relative path and normalize it
|
161 |
+
relative_path = file.filename.replace('\\', '/')
|
162 |
+
path_parts = relative_path.split('/')
|
163 |
+
|
164 |
+
if len(path_parts) >= 2:
|
165 |
+
student_folder = path_parts[1] # Get the student folder name
|
166 |
+
|
167 |
+
# Save file in ans_image directory
|
168 |
+
save_dir = os.path.join('ans_image', student_folder)
|
169 |
+
os.makedirs(save_dir, exist_ok=True)
|
170 |
+
|
171 |
+
save_path = os.path.join(save_dir, path_parts[-1])
|
172 |
+
file.save(save_path)
|
173 |
+
save_path = save_path.replace('\\', '/')
|
174 |
+
|
175 |
+
# Store file info in data dictionary
|
176 |
+
if student_folder not in data:
|
177 |
+
data[student_folder] = []
|
178 |
+
data[student_folder].append({
|
179 |
+
'path': save_path,
|
180 |
+
'name': os.path.splitext(path_parts[-1])[0] # filename without extension
|
181 |
+
})
|
182 |
+
|
183 |
+
print("Processed files structure:", data)
|
184 |
+
|
185 |
+
# Calculate marks for each student and image
|
186 |
results = []
|
187 |
for student_folder, images in data.items():
|
188 |
for idx, image_info in enumerate(images):
|
189 |
try:
|
190 |
+
# Extract text from image
|
191 |
image_path = image_info['path']
|
192 |
s_answer = extract_text_from_image(image_path)
|
193 |
+
print(f"Extracted answer from {image_path}:", s_answer)
|
194 |
+
|
195 |
+
# Calculate marks
|
196 |
if idx < len(answers): # Make sure we have corresponding answer
|
197 |
tf_idf_word_values, max_tfidf = create_tfidf_values(answers[idx])
|
198 |
m = marks(s_answer, sen_vec_answers[idx], word_vec_answers[idx],
|
|
|
200 |
|
201 |
if isinstance(m, torch.Tensor):
|
202 |
m = m.item()
|
203 |
+
|
204 |
+
mark_value = round(float(m), 2)
|
|
|
|
|
|
|
|
|
205 |
else:
|
206 |
+
mark_value = 0
|
207 |
+
|
208 |
+
results.append({
|
209 |
+
'student': student_folder,
|
210 |
+
'image_name': image_info['name'],
|
211 |
+
'marks': mark_value
|
212 |
+
})
|
213 |
+
print(f"Marks for {student_folder}/{image_info['name']}: {mark_value}")
|
214 |
+
|
215 |
except Exception as e:
|
216 |
print(f"Error processing {image_path}: {str(e)}")
|
217 |
results.append({
|
|
|
220 |
'marks': 0
|
221 |
})
|
222 |
|
223 |
+
print("Final results:", results)
|
224 |
return jsonify({"message": results}), 200
|
225 |
|
226 |
except Exception as e:
|