File size: 14,090 Bytes
b654a0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dcd702
b654a0b
 
0c44394
b654a0b
 
 
 
 
b4bfa74
 
 
 
 
 
 
 
 
 
b654a0b
 
e936903
b4bfa74
 
 
 
 
 
 
 
 
e936903
b4bfa74
 
b654a0b
 
 
 
df9870a
 
b654a0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3f7f19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import gradio as gr
import json
from flask import jsonify
from sentence_transformers import SentenceTransformer, InputExample, util
from codeScripts.utils import save_json, load_json, create_file_path, remove
from plentas import Plentas
import pandas as pd
import zipfile
import os
import shutil
from datetime import datetime
import tablib
from pathlib import Path

def Main(uploadedFile, txtFileInput, orthographyPercentage, syntaxPercentage, semanticPercentage, studentsRange):
  
    error = ""
    excelPath = None
    
    copySpanishDictionaries()

    try:        
        if not txtFileInput:
           error="Por favor seleccione un archivo con las preguntas y respuestas"
           return [error, excelPath]
        else:
            txtFileInput = txtFileInput.name

        configuration = readQATextFile(txtFileInput)

        configuration["ortographyPercentage"] = float(orthographyPercentage)
        configuration["syntaxPercentage"] = float(syntaxPercentage)
        configuration["semanticPercentage"] = float(semanticPercentage)

        if studentsRange == "":
            studentsRange = "All"

        configuration["students"] = studentsRange

        if not uploadedFile:
            error="Por favor seleccione el .zip con las respuestas de los alumnos"
            return [error, excelPath]
        else:
            uploadedFilePath = uploadedFile.name
                     
        config_json = load_json("configV2.json")

        answersDict = None
        try:
            answersDict = answersTodict(uploadedFilePath)
        except Exception as ex:
            error = "Error in answersTodict: " + str(ex)
            return [error, excelPath]
        
        teacherJson = None
        try:
            teacherJson = createTeacherJson(configuration)
        except Exception as ex:
            error = "Error in createTeacherJson: " + str(ex)
            return [error, excelPath]
        
        try: 
            # #configuring plentas methodology
            response = Plentas(config_json[0], [answersDict, teacherJson])            
        except Exception as ex:            
            error = "Error configuring: " + str(ex)
            return [error, excelPath]
        
        try:             
            # # #overwriting the custom settings for the settings from the api      
            response.setApiSettings(configuration)
        except Exception as ex:            
            error = "Error setting: " + str(ex)
            return [error, excelPath]

        try:    
            print("Processing!")
            modelResult = response.processApiData()
        except Exception as ex:            
            error = "Error processing: " + str(ex)
            return [error, excelPath]

        # modelJson = json.dumps(modelResult)
        print(modelResult)
        excelPath = exportResultToExcelFile(modelResult)

    except Exception as ex:
        error = "Error exporting to Excel: " + str(e)
    
    return [error, excelPath]

def exportResultToExcelFile(modelResult):
    try:
        excelData = []

        studentsArray = modelResult[0]
        index = 0
        for item in studentsArray:
            #print("ITEM - " + str(item))
            studentData = item[index]
            excelData.append(studentData)
            index+= 1
    
        #tableResults = tablib.Dataset(headers=('ID', 'SimilitudSpacy', 'SimilitudBert', 'NotaSemanticaSpacy', 'NotaSemanticaBert', 'NotaSintaxis', 'NotaOrtografia','NotaTotalSpacy','NotaTotalBert','Feedback'))    
        tableResults = tablib.Dataset(headers=('ID', 'SumaTotalSpacy', 'SumaTotaldBert', 'NotaSemanticaSpacy', 'NotaSemanticaBert', 'NotaSintaxis', 'NotaOrtografia','NotaTotalSpacy','NotaTotalBert','Feedback'))    

        tableResults.json=json.dumps(excelData)
        tableExport=tableResults.export('xlsx')
        outputFilePath = './output/' + str(datetime.now().microsecond) + '_plentas_output.xlsx'
        # outputFilePath = './output/plentas_output.xlsx'
        with open(outputFilePath, 'wb') as f:  # open the xlsx file
            f.write(tableExport)  # write the dataset to the xlsx file
        f.close()
        
    except Exception as ex:
        print("Error exportando Excel:"+ str(e))
        
    return outputFilePath

def copySpanishDictionaries():
    try:
        shutil.copy("./assets/hunspell_dictionaries/es_ES/es_ES.aff", "/home/user/.local/lib/python3.9/site-packages/hunspell/dictionaries/es_ES.aff")
        shutil.copy("./assets/hunspell_dictionaries/es_ES/es_ES.dic", "/home/user/.local/lib/python3.9/site-packages/hunspell/dictionaries/es_ES.dic")        
    except Exception as ex:
        print("Error copying dictionaries" + str(ex))

def readQATextFile(qaTextFilePath):
    configuration = {}

    f = open(qaTextFilePath, 'r')
    lines = f.readlines()

    count = 0
    qCount=1
    
    q = ""
    a = ""
    while count < len(lines):
        if q == "" or q == "\n":
            q = lines[count]
            count += 1
            continue

        if a == "" or a == "\n":
            a = lines[count]
            count += 1            
            
        if q != "" and a != "":
            configuration["minip" + str(qCount)] = q
            configuration["minir" + str(qCount)] = a
            qCount += 1
            q = ""
            a = ""
    
    return configuration

def createTeacherJson(configuration):
    """
    This function extracts the information about the subquestions and subanswers and puts them in the correct format.
    Inputs:
        config: The configured info from the api.
    Outputs:
        teachersJson: The generated dictionary with the subquestions.
    """
    teachersJson = {"enunciado": "", "minipreguntas":[], "keywords":""}

    #5 is the maximum number of permitted subquestions in the configuration2 page
    
    for i in range(5):
       
        try:
            teachersJson["minipreguntas"].append({
				"minipregunta": configuration["minip" + str(i+1)],
				"minirespuesta": configuration["minir" + str(i+1)]
			})

        except:
            break

    return teachersJson

def extractZipData(ruta_zip):
    """
    This function extracts the students's answers from the zip file (the one the teacher has in the task section).
    Inputs:
        ruta_zip: The path inherited from answersTodict
    """
    #defining the path where the extracted info is to be stored
    ruta_extraccion = create_file_path("StudentAnswers/", doctype= 1)
    #extracting the info
    archivo_zip = zipfile.ZipFile(ruta_zip, "r")
    try:
        archivo_zip.extractall(pwd=None, path=ruta_extraccion)
        #archivo_zip.extract(pwd=None, path=ruta_extraccion)
    except:
        pass
    archivo_zip.close()

def removeHtmlFromString(string):
    """
    This function removes the html tags from the student's response.
    Inputs:
        -string: The student's response
    Outputs:
        -new_string: The filtered response
    """
    string = string.encode('utf-8', 'replace')
    string = string.decode('utf-8', 'replace')
    new_string = ""
    skipChar = 0
    for char in string:
        if char == "<":
            skipChar = 1
        elif char == ">":
            skipChar = 0
        else:
            if not skipChar:        
                new_string = new_string+char

    new_string = new_string.encode('utf-8', 'replace')
    new_string = new_string.decode('utf-8', 'replace')
    return new_string

def answersTodict(zip_path):
    """
    This function extracts the students's answers and stacks them in one specific format so that it can be processed next.
    Inputs:
        ruta_zip: The path where the zip file is stored
    Outputs:
        studentAnswersDict: The dictionary with all the responses
    """
  
    # path
    remove('api/StudentAnswers/')

    #extracting the data
    extractZipData(zip_path)
    
    
    
    studentAnswersDict = []
    indx2=0

    #stacking the information of each extracted folder
    for work_folder in os.listdir(create_file_path("StudentAnswers/", doctype= 1)):
        #print("work_folder: " + work_folder)

        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))))):
            student_name = student.split("(")
            student_name = student_name[0]

            print("student: " + str(student) + " - index: " + str(indx))

            try:
                #opening the file
               
                #fichero1 = create_file_path("StudentAnswers/" + work_folder + "/" + student+ "/" + 'Adjuntos del envio/', doctype= 1)
                fichero1 = create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + student+'_submissionText.html', doctype= 1)
                
                #where the actual response is 
              

                if os.path.exists(fichero1):
                    print("Fichero: "+str(fichero1))    
                    #fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')                
                    #fichero = create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1)
                    #if os.path.exists(fichero):
                    #    fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')                
                    #else:
                    fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + student+'_submissionText.html', doctype= 1), encoding='utf-8')                
                    
                    print("fichero abierto")
                    #reading it
                    lineas = fichero.readlines()

                    textoFichero = ""

                    if (len(lineas) > 0):
                        textoFichero = lineas[0]

                    print("texto: " + textoFichero)

                    #removing html                
                    textoFichero = removeHtmlFromString(textoFichero)           
                                
                    #saving it                                
                    studentAnswersDict.append({"respuesta":textoFichero, "hashed_id":student_name, "TableIndex":indx})
                    print("fichero procesado")

                elif os.path.exists(create_file_path("StudentAnswers/" + work_folder, doctype= 1)) :                     
                    #print("Entra por acá2")
                    student_name2 = work_folder.split("_")
                    student_name = student_name2[0]
                    student_id2=student_name2[1]
                    student_assingsubmission = student_name2[2]
                    student_response = student_name2[3]
                    #print("Sigue acá2")
                    #print("StudentResponse"+str(student_response))
                    if student_response=='onlinetext':

                      #  print("Fichero: " + "StudentAnswers/" + work_folder+"/onlinetext.html")
                        fichero = open(create_file_path("StudentAnswers/" + work_folder+"/onlinetext.html", doctype= 1), encoding='utf-8')                
                                                                        
                        lineas = fichero.readlines()

                        #removing html                
                        lineas[0] = removeHtmlFromString(lineas[0]) 
                                                       
                        #saving it         
                        indx2+=1                                               
                        studentAnswersDict.append({"respuesta":lineas[0], "hashed_id":student_name, "TableIndex":indx2})
                        #break
                    #elif student_response!='file' or None:
                       # print("Fichero no encontrado")
                     #   studentAnswersDict.append({"respuesta":'', "hashed_id":student_name, "TableIndex":indx2})

            except:
                #studentAnswersDict.append({"respuesta":"", "hashed_id":student_name, "TableIndex":indx})
                print("Error buscando ficheros:"+fichero)

    #print("DICT" + json.dumps(studentAnswersDict))

    #saving the final dictionary
    save_json(create_file_path('ApiStudentsDict.json', doctype= 1),studentAnswersDict)
    return studentAnswersDict

    
zipFileInput = gr.inputs.File(label="1. Selecciona el .ZIP con las respuestas de los alumnos")
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.")
orthographyPercentage = gr.inputs.Textbox(label="Ortografía",lines=1, placeholder="0",default=0.1, numeric=1)
syntaxPercentage = gr.inputs.Textbox(label="Sintaxis",lines=1, placeholder="0",default=0.1,numeric=1)
semanticPercentage = gr.inputs.Textbox(label="Semántica",lines=1, placeholder="0",default=0.8, numeric=1)
studentsRange = gr.inputs.Textbox(label="Estudiantes a evaluar",lines=1, placeholder="Dejar vacío para evaluar todos")
        #dataFrameOutput = gr.outputs.Dataframe(headers=["Resultados"], max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="Resultado")

labelOutput = gr.outputs.Label(num_top_classes=None, type="auto", label="Output")
labelError = gr.outputs.Label(num_top_classes=None, type="auto", label="Errores")
downloadExcelButton = gr.outputs.File('Resultados')

iface = gr.Interface(fn=Main
    , inputs=[zipFileInput, txtFileInput, orthographyPercentage, syntaxPercentage, semanticPercentage, studentsRange]
    , outputs=[labelError, downloadExcelButton]
    , title = "PLENTAS"
    
)

#iface.launch(share = False,enable_queue=True, show_error =True, server_port= 7861)
iface.launch(share = False,enable_queue=True, show_error =True)