Spaces:
Runtime error
Runtime error
xiomarablanco
commited on
Commit
·
b654a0b
1
Parent(s):
7b818fd
Update app.py
Browse files
app.py
CHANGED
@@ -1,344 +1,344 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import json
|
3 |
-
from flask import jsonify
|
4 |
-
from sentence_transformers import SentenceTransformer, InputExample, util
|
5 |
-
from codeScripts.utils import save_json, load_json, create_file_path, remove
|
6 |
-
from plentas import Plentas
|
7 |
-
import pandas as pd
|
8 |
-
import zipfile
|
9 |
-
import os
|
10 |
-
import shutil
|
11 |
-
from datetime import datetime
|
12 |
-
import tablib
|
13 |
-
from pathlib import Path
|
14 |
-
|
15 |
-
def Main(uploadedFile, txtFileInput, orthographyPercentage, syntaxPercentage, semanticPercentage, studentsRange):
|
16 |
-
|
17 |
-
error = ""
|
18 |
-
excelPath = None
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
try:
|
23 |
-
if not txtFileInput:
|
24 |
-
error="Por favor seleccione un archivo con las preguntas y respuestas"
|
25 |
-
return [error, excelPath]
|
26 |
-
else:
|
27 |
-
txtFileInput = txtFileInput.name
|
28 |
-
|
29 |
-
configuration = readQATextFile(txtFileInput)
|
30 |
-
|
31 |
-
configuration["ortographyPercentage"] = float(orthographyPercentage)
|
32 |
-
configuration["syntaxPercentage"] = float(syntaxPercentage)
|
33 |
-
configuration["semanticPercentage"] = float(semanticPercentage)
|
34 |
-
|
35 |
-
if studentsRange == "":
|
36 |
-
studentsRange = "All"
|
37 |
-
|
38 |
-
configuration["students"] = studentsRange
|
39 |
-
|
40 |
-
if not uploadedFile:
|
41 |
-
error="Por favor seleccione el .zip con las respuestas de los alumnos"
|
42 |
-
return [error, excelPath]
|
43 |
-
else:
|
44 |
-
uploadedFilePath = uploadedFile.name
|
45 |
-
|
46 |
-
config_json = load_json("configV2.json")
|
47 |
-
|
48 |
-
answersDict = None
|
49 |
-
try:
|
50 |
-
answersDict = answersTodict(uploadedFilePath)
|
51 |
-
except Exception as ex:
|
52 |
-
error = "Error in answersTodict: " + str(ex)
|
53 |
-
return [error, excelPath]
|
54 |
-
|
55 |
-
teacherJson = None
|
56 |
-
try:
|
57 |
-
teacherJson = createTeacherJson(configuration)
|
58 |
-
except Exception as ex:
|
59 |
-
error = "Error in createTeacherJson: " + str(ex)
|
60 |
-
return [error, excelPath]
|
61 |
-
|
62 |
-
try:
|
63 |
-
# #configuring plentas methodology
|
64 |
-
response = Plentas(config_json[0], [answersDict, teacherJson])
|
65 |
-
except Exception as ex:
|
66 |
-
error = "Error configuring: " + str(ex)
|
67 |
-
return [error, excelPath]
|
68 |
-
|
69 |
-
try:
|
70 |
-
# # #overwriting the custom settings for the settings from the api
|
71 |
-
response.setApiSettings(configuration)
|
72 |
-
except Exception as ex:
|
73 |
-
error = "Error setting: " + str(ex)
|
74 |
-
return [error, excelPath]
|
75 |
-
|
76 |
-
try:
|
77 |
-
print("Processing!")
|
78 |
-
modelResult = response.processApiData()
|
79 |
-
except Exception as ex:
|
80 |
-
error = "Error processing: " + str(ex)
|
81 |
-
return [error, excelPath]
|
82 |
-
|
83 |
-
# modelJson = json.dumps(modelResult)
|
84 |
-
|
85 |
-
excelPath = exportResultToExcelFile(modelResult)
|
86 |
-
|
87 |
-
except Exception as e:
|
88 |
-
error = "Error exporting to Excel: " + str(e)
|
89 |
-
|
90 |
-
return [error, excelPath]
|
91 |
-
|
92 |
-
def exportResultToExcelFile(modelResult):
|
93 |
-
|
94 |
-
excelData = []
|
95 |
-
|
96 |
-
studentsArray = modelResult[0]
|
97 |
-
index = 0
|
98 |
-
for item in studentsArray:
|
99 |
-
#print("ITEM - " + str(item))
|
100 |
-
studentData = item[index]
|
101 |
-
excelData.append(studentData)
|
102 |
-
index+= 1
|
103 |
-
|
104 |
-
#tableResults = tablib.Dataset(headers=('ID', 'SimilitudSpacy', 'SimilitudBert', 'NotaSemanticaSpacy', 'NotaSemanticaBert', 'NotaSintaxis', 'NotaOrtografia','NotaTotalSpacy','NotaTotalBert','Feedback'))
|
105 |
-
tableResults = tablib.Dataset(headers=('ID', 'SumaTotalSpacy', 'SumaTotaldBert', 'NotaSemanticaSpacy', 'NotaSemanticaBert', 'NotaSintaxis', 'NotaOrtografia','NotaTotalSpacy','NotaTotalBert','Feedback'))
|
106 |
-
|
107 |
-
tableResults.json=json.dumps(excelData)
|
108 |
-
tableExport=tableResults.export('xlsx')
|
109 |
-
outputFilePath = './output/' + str(datetime.now().microsecond) + '_plentas_output.xlsx'
|
110 |
-
# outputFilePath = './output/plentas_output.xlsx'
|
111 |
-
with open(outputFilePath, 'wb') as f: # open the xlsx file
|
112 |
-
f.write(tableExport) # write the dataset to the xlsx file
|
113 |
-
f.close()
|
114 |
-
return outputFilePath
|
115 |
-
|
116 |
-
def copySpanishDictionaries():
|
117 |
-
try:
|
118 |
-
shutil.copy("./assets/hunspell_dictionaries/es_ES/es_ES.aff", "/home/user/.local/lib/python3.8/site-packages/hunspell/dictionaries/es_ES.aff")
|
119 |
-
shutil.copy("./assets/hunspell_dictionaries/es_ES/es_ES.dic", "/home/user/.local/lib/python3.8/site-packages/hunspell/dictionaries/es_ES.dic")
|
120 |
-
except Exception as ex:
|
121 |
-
print("Error copying dictionaries" + str(ex))
|
122 |
-
|
123 |
-
def readQATextFile(qaTextFilePath):
|
124 |
-
configuration = {}
|
125 |
-
|
126 |
-
f = open(qaTextFilePath, 'r')
|
127 |
-
lines = f.readlines()
|
128 |
-
|
129 |
-
count = 0
|
130 |
-
qCount=1
|
131 |
-
|
132 |
-
q = ""
|
133 |
-
a = ""
|
134 |
-
while count < len(lines):
|
135 |
-
if q == "" or q == "\n":
|
136 |
-
q = lines[count]
|
137 |
-
count += 1
|
138 |
-
continue
|
139 |
-
|
140 |
-
if a == "" or a == "\n":
|
141 |
-
a = lines[count]
|
142 |
-
count += 1
|
143 |
-
|
144 |
-
if q != "" and a != "":
|
145 |
-
configuration["minip" + str(qCount)] = q
|
146 |
-
configuration["minir" + str(qCount)] = a
|
147 |
-
qCount += 1
|
148 |
-
q = ""
|
149 |
-
a = ""
|
150 |
-
|
151 |
-
return configuration
|
152 |
-
|
153 |
-
def createTeacherJson(configuration):
|
154 |
-
"""
|
155 |
-
This function extracts the information about the subquestions and subanswers and puts them in the correct format.
|
156 |
-
Inputs:
|
157 |
-
config: The configured info from the api.
|
158 |
-
Outputs:
|
159 |
-
teachersJson: The generated dictionary with the subquestions.
|
160 |
-
"""
|
161 |
-
teachersJson = {"enunciado": "", "minipreguntas":[], "keywords":""}
|
162 |
-
|
163 |
-
#5 is the maximum number of permitted subquestions in the configuration2 page
|
164 |
-
|
165 |
-
for i in range(5):
|
166 |
-
|
167 |
-
try:
|
168 |
-
teachersJson["minipreguntas"].append({
|
169 |
-
"minipregunta": configuration["minip" + str(i+1)],
|
170 |
-
"minirespuesta": configuration["minir" + str(i+1)]
|
171 |
-
})
|
172 |
-
|
173 |
-
except:
|
174 |
-
break
|
175 |
-
|
176 |
-
return teachersJson
|
177 |
-
|
178 |
-
def extractZipData(ruta_zip):
|
179 |
-
"""
|
180 |
-
This function extracts the students's answers from the zip file (the one the teacher has in the task section).
|
181 |
-
Inputs:
|
182 |
-
ruta_zip: The path inherited from answersTodict
|
183 |
-
"""
|
184 |
-
#defining the path where the extracted info is to be stored
|
185 |
-
ruta_extraccion = create_file_path("StudentAnswers/", doctype= 1)
|
186 |
-
#extracting the info
|
187 |
-
archivo_zip = zipfile.ZipFile(ruta_zip, "r")
|
188 |
-
try:
|
189 |
-
archivo_zip.extractall(pwd=None, path=ruta_extraccion)
|
190 |
-
#archivo_zip.extract(pwd=None, path=ruta_extraccion)
|
191 |
-
except:
|
192 |
-
pass
|
193 |
-
archivo_zip.close()
|
194 |
-
|
195 |
-
def removeHtmlFromString(string):
|
196 |
-
"""
|
197 |
-
This function removes the html tags from the student's response.
|
198 |
-
Inputs:
|
199 |
-
-string: The student's response
|
200 |
-
Outputs:
|
201 |
-
-new_string: The filtered response
|
202 |
-
"""
|
203 |
-
string = string.encode('utf-8', 'replace')
|
204 |
-
string = string.decode('utf-8', 'replace')
|
205 |
-
new_string = ""
|
206 |
-
skipChar = 0
|
207 |
-
for char in string:
|
208 |
-
if char == "<":
|
209 |
-
skipChar = 1
|
210 |
-
elif char == ">":
|
211 |
-
skipChar = 0
|
212 |
-
else:
|
213 |
-
if not skipChar:
|
214 |
-
new_string = new_string+char
|
215 |
-
|
216 |
-
new_string = new_string.encode('utf-8', 'replace')
|
217 |
-
new_string = new_string.decode('utf-8', 'replace')
|
218 |
-
return new_string
|
219 |
-
|
220 |
-
def answersTodict(zip_path):
|
221 |
-
"""
|
222 |
-
This function extracts the students's answers and stacks them in one specific format so that it can be processed next.
|
223 |
-
Inputs:
|
224 |
-
ruta_zip: The path where the zip file is stored
|
225 |
-
Outputs:
|
226 |
-
studentAnswersDict: The dictionary with all the responses
|
227 |
-
"""
|
228 |
-
|
229 |
-
# path
|
230 |
-
remove('api/StudentAnswers/')
|
231 |
-
|
232 |
-
#extracting the data
|
233 |
-
extractZipData(zip_path)
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
studentAnswersDict = []
|
238 |
-
indx2=0
|
239 |
-
|
240 |
-
#stacking the information of each extracted folder
|
241 |
-
for work_folder in os.listdir(create_file_path("StudentAnswers/", doctype= 1)):
|
242 |
-
#print("work_folder: " + work_folder)
|
243 |
-
|
244 |
-
for student, indx in zip(os.listdir(create_file_path("StudentAnswers/" + work_folder, doctype= 1)), range(len(os.listdir(create_file_path("StudentAnswers/" + work_folder, doctype= 1))))):
|
245 |
-
student_name = student.split("(")
|
246 |
-
student_name = student_name[0]
|
247 |
-
|
248 |
-
print("student: " + str(student) + " - index: " + str(indx))
|
249 |
-
|
250 |
-
try:
|
251 |
-
#opening the file
|
252 |
-
|
253 |
-
#fichero1 = create_file_path("StudentAnswers/" + work_folder + "/" + student+ "/" + 'Adjuntos del envio/', doctype= 1)
|
254 |
-
fichero1 = create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + student+'_submissionText.html', doctype= 1)
|
255 |
-
|
256 |
-
#where the actual response is
|
257 |
-
|
258 |
-
|
259 |
-
if os.path.exists(fichero1):
|
260 |
-
print("Fichero: "+str(fichero1))
|
261 |
-
#fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')
|
262 |
-
#fichero = create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1)
|
263 |
-
#if os.path.exists(fichero):
|
264 |
-
# fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')
|
265 |
-
#else:
|
266 |
-
fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + student+'_submissionText.html', doctype= 1), encoding='utf-8')
|
267 |
-
|
268 |
-
print("fichero abierto")
|
269 |
-
#reading it
|
270 |
-
lineas = fichero.readlines()
|
271 |
-
|
272 |
-
textoFichero = ""
|
273 |
-
|
274 |
-
if (len(lineas) > 0):
|
275 |
-
textoFichero = lineas[0]
|
276 |
-
|
277 |
-
print("texto: " + textoFichero)
|
278 |
-
|
279 |
-
#removing html
|
280 |
-
textoFichero = removeHtmlFromString(textoFichero)
|
281 |
-
|
282 |
-
#saving it
|
283 |
-
studentAnswersDict.append({"respuesta":textoFichero, "hashed_id":student_name, "TableIndex":indx})
|
284 |
-
print("fichero procesado")
|
285 |
-
|
286 |
-
elif os.path.exists(create_file_path("StudentAnswers/" + work_folder, doctype= 1)) :
|
287 |
-
#print("Entra por acá2")
|
288 |
-
student_name2 = work_folder.split("_")
|
289 |
-
student_name = student_name2[0]
|
290 |
-
student_id2=student_name2[1]
|
291 |
-
student_assingsubmission = student_name2[2]
|
292 |
-
student_response = student_name2[3]
|
293 |
-
#print("Sigue acá2")
|
294 |
-
#print("StudentResponse"+str(student_response))
|
295 |
-
if student_response=='onlinetext':
|
296 |
-
|
297 |
-
# print("Fichero: " + "StudentAnswers/" + work_folder+"/onlinetext.html")
|
298 |
-
fichero = open(create_file_path("StudentAnswers/" + work_folder+"/onlinetext.html", doctype= 1), encoding='utf-8')
|
299 |
-
|
300 |
-
lineas = fichero.readlines()
|
301 |
-
|
302 |
-
#removing html
|
303 |
-
lineas[0] = removeHtmlFromString(lineas[0])
|
304 |
-
|
305 |
-
#saving it
|
306 |
-
indx2+=1
|
307 |
-
studentAnswersDict.append({"respuesta":lineas[0], "hashed_id":student_name, "TableIndex":indx2})
|
308 |
-
#break
|
309 |
-
#elif student_response!='file' or None:
|
310 |
-
# print("Fichero no encontrado")
|
311 |
-
# studentAnswersDict.append({"respuesta":'', "hashed_id":student_name, "TableIndex":indx2})
|
312 |
-
|
313 |
-
except:
|
314 |
-
#studentAnswersDict.append({"respuesta":"", "hashed_id":student_name, "TableIndex":indx})
|
315 |
-
print("Error buscando ficheros:"+fichero)
|
316 |
-
|
317 |
-
#print("DICT" + json.dumps(studentAnswersDict))
|
318 |
-
|
319 |
-
#saving the final dictionary
|
320 |
-
save_json(create_file_path('ApiStudentsDict.json', doctype= 1),studentAnswersDict)
|
321 |
-
return studentAnswersDict
|
322 |
-
|
323 |
-
|
324 |
-
zipFileInput = gr.inputs.File(label="1. Selecciona el .ZIP con las respuestas de los alumnos")
|
325 |
-
txtFileInput = gr.inputs.File(label="2. Selecciona el .txt con las preguntas y respuestas correctas. Escriba una pregunta en una sola línea y debajo la respuesta en la línea siguiente.")
|
326 |
-
orthographyPercentage = gr.inputs.Textbox(label="Ortografía",lines=1, placeholder="0",default=0.1, numeric=1)
|
327 |
-
syntaxPercentage = gr.inputs.Textbox(label="Sintaxis",lines=1, placeholder="0",default=0.1,numeric=1)
|
328 |
-
semanticPercentage = gr.inputs.Textbox(label="Semántica",lines=1, placeholder="0",default=0.8, numeric=1)
|
329 |
-
studentsRange = gr.inputs.Textbox(label="Estudiantes a evaluar",lines=1, placeholder="Dejar vacío para evaluar todos")
|
330 |
-
#dataFrameOutput = gr.outputs.Dataframe(headers=["Resultados"], max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="Resultado")
|
331 |
-
|
332 |
-
labelOutput = gr.outputs.Label(num_top_classes=None, type="auto", label="Output")
|
333 |
-
labelError = gr.outputs.Label(num_top_classes=None, type="auto", label="Errores")
|
334 |
-
downloadExcelButton = gr.outputs.File('Resultados')
|
335 |
-
|
336 |
-
iface = gr.Interface(fn=Main
|
337 |
-
, inputs=[zipFileInput, txtFileInput, orthographyPercentage, syntaxPercentage, semanticPercentage, studentsRange]
|
338 |
-
, outputs=[labelError, downloadExcelButton]
|
339 |
-
, title = "PLENTAS"
|
340 |
-
|
341 |
-
)
|
342 |
-
|
343 |
-
#iface.launch(share = False,enable_queue=True, show_error =True, server_port= 7861)
|
344 |
iface.launch(share = False,enable_queue=True, show_error =True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from flask import jsonify
|
4 |
+
from sentence_transformers import SentenceTransformer, InputExample, util
|
5 |
+
from codeScripts.utils import save_json, load_json, create_file_path, remove
|
6 |
+
from plentas import Plentas
|
7 |
+
import pandas as pd
|
8 |
+
import zipfile
|
9 |
+
import os
|
10 |
+
import shutil
|
11 |
+
from datetime import datetime
|
12 |
+
import tablib
|
13 |
+
from pathlib import Path
|
14 |
+
|
15 |
+
def Main(uploadedFile, txtFileInput, orthographyPercentage, syntaxPercentage, semanticPercentage, studentsRange):
|
16 |
+
|
17 |
+
error = ""
|
18 |
+
excelPath = None
|
19 |
+
|
20 |
+
copySpanishDictionaries()
|
21 |
+
|
22 |
+
try:
|
23 |
+
if not txtFileInput:
|
24 |
+
error="Por favor seleccione un archivo con las preguntas y respuestas"
|
25 |
+
return [error, excelPath]
|
26 |
+
else:
|
27 |
+
txtFileInput = txtFileInput.name
|
28 |
+
|
29 |
+
configuration = readQATextFile(txtFileInput)
|
30 |
+
|
31 |
+
configuration["ortographyPercentage"] = float(orthographyPercentage)
|
32 |
+
configuration["syntaxPercentage"] = float(syntaxPercentage)
|
33 |
+
configuration["semanticPercentage"] = float(semanticPercentage)
|
34 |
+
|
35 |
+
if studentsRange == "":
|
36 |
+
studentsRange = "All"
|
37 |
+
|
38 |
+
configuration["students"] = studentsRange
|
39 |
+
|
40 |
+
if not uploadedFile:
|
41 |
+
error="Por favor seleccione el .zip con las respuestas de los alumnos"
|
42 |
+
return [error, excelPath]
|
43 |
+
else:
|
44 |
+
uploadedFilePath = uploadedFile.name
|
45 |
+
|
46 |
+
config_json = load_json("configV2.json")
|
47 |
+
|
48 |
+
answersDict = None
|
49 |
+
try:
|
50 |
+
answersDict = answersTodict(uploadedFilePath)
|
51 |
+
except Exception as ex:
|
52 |
+
error = "Error in answersTodict: " + str(ex)
|
53 |
+
return [error, excelPath]
|
54 |
+
|
55 |
+
teacherJson = None
|
56 |
+
try:
|
57 |
+
teacherJson = createTeacherJson(configuration)
|
58 |
+
except Exception as ex:
|
59 |
+
error = "Error in createTeacherJson: " + str(ex)
|
60 |
+
return [error, excelPath]
|
61 |
+
|
62 |
+
try:
|
63 |
+
# #configuring plentas methodology
|
64 |
+
response = Plentas(config_json[0], [answersDict, teacherJson])
|
65 |
+
except Exception as ex:
|
66 |
+
error = "Error configuring: " + str(ex)
|
67 |
+
return [error, excelPath]
|
68 |
+
|
69 |
+
try:
|
70 |
+
# # #overwriting the custom settings for the settings from the api
|
71 |
+
response.setApiSettings(configuration)
|
72 |
+
except Exception as ex:
|
73 |
+
error = "Error setting: " + str(ex)
|
74 |
+
return [error, excelPath]
|
75 |
+
|
76 |
+
try:
|
77 |
+
print("Processing!")
|
78 |
+
modelResult = response.processApiData()
|
79 |
+
except Exception as ex:
|
80 |
+
error = "Error processing: " + str(ex)
|
81 |
+
return [error, excelPath]
|
82 |
+
|
83 |
+
# modelJson = json.dumps(modelResult)
|
84 |
+
|
85 |
+
excelPath = exportResultToExcelFile(modelResult)
|
86 |
+
|
87 |
+
except Exception as e:
|
88 |
+
error = "Error exporting to Excel: " + str(e)
|
89 |
+
|
90 |
+
return [error, excelPath]
|
91 |
+
|
92 |
+
def exportResultToExcelFile(modelResult):
|
93 |
+
|
94 |
+
excelData = []
|
95 |
+
|
96 |
+
studentsArray = modelResult[0]
|
97 |
+
index = 0
|
98 |
+
for item in studentsArray:
|
99 |
+
#print("ITEM - " + str(item))
|
100 |
+
studentData = item[index]
|
101 |
+
excelData.append(studentData)
|
102 |
+
index+= 1
|
103 |
+
|
104 |
+
#tableResults = tablib.Dataset(headers=('ID', 'SimilitudSpacy', 'SimilitudBert', 'NotaSemanticaSpacy', 'NotaSemanticaBert', 'NotaSintaxis', 'NotaOrtografia','NotaTotalSpacy','NotaTotalBert','Feedback'))
|
105 |
+
tableResults = tablib.Dataset(headers=('ID', 'SumaTotalSpacy', 'SumaTotaldBert', 'NotaSemanticaSpacy', 'NotaSemanticaBert', 'NotaSintaxis', 'NotaOrtografia','NotaTotalSpacy','NotaTotalBert','Feedback'))
|
106 |
+
|
107 |
+
tableResults.json=json.dumps(excelData)
|
108 |
+
tableExport=tableResults.export('xlsx')
|
109 |
+
outputFilePath = './output/' + str(datetime.now().microsecond) + '_plentas_output.xlsx'
|
110 |
+
# outputFilePath = './output/plentas_output.xlsx'
|
111 |
+
with open(outputFilePath, 'wb') as f: # open the xlsx file
|
112 |
+
f.write(tableExport) # write the dataset to the xlsx file
|
113 |
+
f.close()
|
114 |
+
return outputFilePath
|
115 |
+
|
116 |
+
def copySpanishDictionaries():
|
117 |
+
try:
|
118 |
+
shutil.copy("./assets/hunspell_dictionaries/es_ES/es_ES.aff", "/home/user/.local/lib/python3.8/site-packages/hunspell/dictionaries/es_ES.aff")
|
119 |
+
shutil.copy("./assets/hunspell_dictionaries/es_ES/es_ES.dic", "/home/user/.local/lib/python3.8/site-packages/hunspell/dictionaries/es_ES.dic")
|
120 |
+
except Exception as ex:
|
121 |
+
print("Error copying dictionaries" + str(ex))
|
122 |
+
|
123 |
+
def readQATextFile(qaTextFilePath):
|
124 |
+
configuration = {}
|
125 |
+
|
126 |
+
f = open(qaTextFilePath, 'r')
|
127 |
+
lines = f.readlines()
|
128 |
+
|
129 |
+
count = 0
|
130 |
+
qCount=1
|
131 |
+
|
132 |
+
q = ""
|
133 |
+
a = ""
|
134 |
+
while count < len(lines):
|
135 |
+
if q == "" or q == "\n":
|
136 |
+
q = lines[count]
|
137 |
+
count += 1
|
138 |
+
continue
|
139 |
+
|
140 |
+
if a == "" or a == "\n":
|
141 |
+
a = lines[count]
|
142 |
+
count += 1
|
143 |
+
|
144 |
+
if q != "" and a != "":
|
145 |
+
configuration["minip" + str(qCount)] = q
|
146 |
+
configuration["minir" + str(qCount)] = a
|
147 |
+
qCount += 1
|
148 |
+
q = ""
|
149 |
+
a = ""
|
150 |
+
|
151 |
+
return configuration
|
152 |
+
|
153 |
+
def createTeacherJson(configuration):
|
154 |
+
"""
|
155 |
+
This function extracts the information about the subquestions and subanswers and puts them in the correct format.
|
156 |
+
Inputs:
|
157 |
+
config: The configured info from the api.
|
158 |
+
Outputs:
|
159 |
+
teachersJson: The generated dictionary with the subquestions.
|
160 |
+
"""
|
161 |
+
teachersJson = {"enunciado": "", "minipreguntas":[], "keywords":""}
|
162 |
+
|
163 |
+
#5 is the maximum number of permitted subquestions in the configuration2 page
|
164 |
+
|
165 |
+
for i in range(5):
|
166 |
+
|
167 |
+
try:
|
168 |
+
teachersJson["minipreguntas"].append({
|
169 |
+
"minipregunta": configuration["minip" + str(i+1)],
|
170 |
+
"minirespuesta": configuration["minir" + str(i+1)]
|
171 |
+
})
|
172 |
+
|
173 |
+
except:
|
174 |
+
break
|
175 |
+
|
176 |
+
return teachersJson
|
177 |
+
|
178 |
+
def extractZipData(ruta_zip):
|
179 |
+
"""
|
180 |
+
This function extracts the students's answers from the zip file (the one the teacher has in the task section).
|
181 |
+
Inputs:
|
182 |
+
ruta_zip: The path inherited from answersTodict
|
183 |
+
"""
|
184 |
+
#defining the path where the extracted info is to be stored
|
185 |
+
ruta_extraccion = create_file_path("StudentAnswers/", doctype= 1)
|
186 |
+
#extracting the info
|
187 |
+
archivo_zip = zipfile.ZipFile(ruta_zip, "r")
|
188 |
+
try:
|
189 |
+
archivo_zip.extractall(pwd=None, path=ruta_extraccion)
|
190 |
+
#archivo_zip.extract(pwd=None, path=ruta_extraccion)
|
191 |
+
except:
|
192 |
+
pass
|
193 |
+
archivo_zip.close()
|
194 |
+
|
195 |
+
def removeHtmlFromString(string):
|
196 |
+
"""
|
197 |
+
This function removes the html tags from the student's response.
|
198 |
+
Inputs:
|
199 |
+
-string: The student's response
|
200 |
+
Outputs:
|
201 |
+
-new_string: The filtered response
|
202 |
+
"""
|
203 |
+
string = string.encode('utf-8', 'replace')
|
204 |
+
string = string.decode('utf-8', 'replace')
|
205 |
+
new_string = ""
|
206 |
+
skipChar = 0
|
207 |
+
for char in string:
|
208 |
+
if char == "<":
|
209 |
+
skipChar = 1
|
210 |
+
elif char == ">":
|
211 |
+
skipChar = 0
|
212 |
+
else:
|
213 |
+
if not skipChar:
|
214 |
+
new_string = new_string+char
|
215 |
+
|
216 |
+
new_string = new_string.encode('utf-8', 'replace')
|
217 |
+
new_string = new_string.decode('utf-8', 'replace')
|
218 |
+
return new_string
|
219 |
+
|
220 |
+
def answersTodict(zip_path):
|
221 |
+
"""
|
222 |
+
This function extracts the students's answers and stacks them in one specific format so that it can be processed next.
|
223 |
+
Inputs:
|
224 |
+
ruta_zip: The path where the zip file is stored
|
225 |
+
Outputs:
|
226 |
+
studentAnswersDict: The dictionary with all the responses
|
227 |
+
"""
|
228 |
+
|
229 |
+
# path
|
230 |
+
remove('api/StudentAnswers/')
|
231 |
+
|
232 |
+
#extracting the data
|
233 |
+
extractZipData(zip_path)
|
234 |
+
|
235 |
+
|
236 |
+
|
237 |
+
studentAnswersDict = []
|
238 |
+
indx2=0
|
239 |
+
|
240 |
+
#stacking the information of each extracted folder
|
241 |
+
for work_folder in os.listdir(create_file_path("StudentAnswers/", doctype= 1)):
|
242 |
+
#print("work_folder: " + work_folder)
|
243 |
+
|
244 |
+
for student, indx in zip(os.listdir(create_file_path("StudentAnswers/" + work_folder, doctype= 1)), range(len(os.listdir(create_file_path("StudentAnswers/" + work_folder, doctype= 1))))):
|
245 |
+
student_name = student.split("(")
|
246 |
+
student_name = student_name[0]
|
247 |
+
|
248 |
+
print("student: " + str(student) + " - index: " + str(indx))
|
249 |
+
|
250 |
+
try:
|
251 |
+
#opening the file
|
252 |
+
|
253 |
+
#fichero1 = create_file_path("StudentAnswers/" + work_folder + "/" + student+ "/" + 'Adjuntos del envio/', doctype= 1)
|
254 |
+
fichero1 = create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + student+'_submissionText.html', doctype= 1)
|
255 |
+
|
256 |
+
#where the actual response is
|
257 |
+
|
258 |
+
|
259 |
+
if os.path.exists(fichero1):
|
260 |
+
print("Fichero: "+str(fichero1))
|
261 |
+
#fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')
|
262 |
+
#fichero = create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1)
|
263 |
+
#if os.path.exists(fichero):
|
264 |
+
# fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')
|
265 |
+
#else:
|
266 |
+
fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + student+'_submissionText.html', doctype= 1), encoding='utf-8')
|
267 |
+
|
268 |
+
print("fichero abierto")
|
269 |
+
#reading it
|
270 |
+
lineas = fichero.readlines()
|
271 |
+
|
272 |
+
textoFichero = ""
|
273 |
+
|
274 |
+
if (len(lineas) > 0):
|
275 |
+
textoFichero = lineas[0]
|
276 |
+
|
277 |
+
print("texto: " + textoFichero)
|
278 |
+
|
279 |
+
#removing html
|
280 |
+
textoFichero = removeHtmlFromString(textoFichero)
|
281 |
+
|
282 |
+
#saving it
|
283 |
+
studentAnswersDict.append({"respuesta":textoFichero, "hashed_id":student_name, "TableIndex":indx})
|
284 |
+
print("fichero procesado")
|
285 |
+
|
286 |
+
elif os.path.exists(create_file_path("StudentAnswers/" + work_folder, doctype= 1)) :
|
287 |
+
#print("Entra por acá2")
|
288 |
+
student_name2 = work_folder.split("_")
|
289 |
+
student_name = student_name2[0]
|
290 |
+
student_id2=student_name2[1]
|
291 |
+
student_assingsubmission = student_name2[2]
|
292 |
+
student_response = student_name2[3]
|
293 |
+
#print("Sigue acá2")
|
294 |
+
#print("StudentResponse"+str(student_response))
|
295 |
+
if student_response=='onlinetext':
|
296 |
+
|
297 |
+
# print("Fichero: " + "StudentAnswers/" + work_folder+"/onlinetext.html")
|
298 |
+
fichero = open(create_file_path("StudentAnswers/" + work_folder+"/onlinetext.html", doctype= 1), encoding='utf-8')
|
299 |
+
|
300 |
+
lineas = fichero.readlines()
|
301 |
+
|
302 |
+
#removing html
|
303 |
+
lineas[0] = removeHtmlFromString(lineas[0])
|
304 |
+
|
305 |
+
#saving it
|
306 |
+
indx2+=1
|
307 |
+
studentAnswersDict.append({"respuesta":lineas[0], "hashed_id":student_name, "TableIndex":indx2})
|
308 |
+
#break
|
309 |
+
#elif student_response!='file' or None:
|
310 |
+
# print("Fichero no encontrado")
|
311 |
+
# studentAnswersDict.append({"respuesta":'', "hashed_id":student_name, "TableIndex":indx2})
|
312 |
+
|
313 |
+
except:
|
314 |
+
#studentAnswersDict.append({"respuesta":"", "hashed_id":student_name, "TableIndex":indx})
|
315 |
+
print("Error buscando ficheros:"+fichero)
|
316 |
+
|
317 |
+
#print("DICT" + json.dumps(studentAnswersDict))
|
318 |
+
|
319 |
+
#saving the final dictionary
|
320 |
+
save_json(create_file_path('ApiStudentsDict.json', doctype= 1),studentAnswersDict)
|
321 |
+
return studentAnswersDict
|
322 |
+
|
323 |
+
|
324 |
+
zipFileInput = gr.inputs.File(label="1. Selecciona el .ZIP con las respuestas de los alumnos")
|
325 |
+
txtFileInput = gr.inputs.File(label="2. Selecciona el .txt con las preguntas y respuestas correctas. Escriba una pregunta en una sola línea y debajo la respuesta en la línea siguiente.")
|
326 |
+
orthographyPercentage = gr.inputs.Textbox(label="Ortografía",lines=1, placeholder="0",default=0.1, numeric=1)
|
327 |
+
syntaxPercentage = gr.inputs.Textbox(label="Sintaxis",lines=1, placeholder="0",default=0.1,numeric=1)
|
328 |
+
semanticPercentage = gr.inputs.Textbox(label="Semántica",lines=1, placeholder="0",default=0.8, numeric=1)
|
329 |
+
studentsRange = gr.inputs.Textbox(label="Estudiantes a evaluar",lines=1, placeholder="Dejar vacío para evaluar todos")
|
330 |
+
#dataFrameOutput = gr.outputs.Dataframe(headers=["Resultados"], max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="Resultado")
|
331 |
+
|
332 |
+
labelOutput = gr.outputs.Label(num_top_classes=None, type="auto", label="Output")
|
333 |
+
labelError = gr.outputs.Label(num_top_classes=None, type="auto", label="Errores")
|
334 |
+
downloadExcelButton = gr.outputs.File('Resultados')
|
335 |
+
|
336 |
+
iface = gr.Interface(fn=Main
|
337 |
+
, inputs=[zipFileInput, txtFileInput, orthographyPercentage, syntaxPercentage, semanticPercentage, studentsRange]
|
338 |
+
, outputs=[labelError, downloadExcelButton]
|
339 |
+
, title = "PLENTAS"
|
340 |
+
|
341 |
+
)
|
342 |
+
|
343 |
+
#iface.launch(share = False,enable_queue=True, show_error =True, server_port= 7861)
|
344 |
iface.launch(share = False,enable_queue=True, show_error =True)
|