xiomarablanco commited on
Commit
25f28ea
1 Parent(s): 07d342a

sin puerto en el app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -153
app.py CHANGED
@@ -1,153 +1,153 @@
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
6
- from plentas import Plentas
7
- import pandas as pd
8
- import zipfile
9
- import os
10
-
11
- def Main(configuration, uploadedFile):
12
-
13
- error = ""
14
- modelResult = ""
15
-
16
- configuration_dict = json.loads(configuration)
17
-
18
- try:
19
- uploadedFilePath = uploadedFile.name
20
-
21
- config_json = load_json("configV2.json")
22
-
23
- #configuring plentas methodology
24
- response = Plentas(config_json[0], [answersTodict(uploadedFilePath), createTeacherJson(configuration_dict)])
25
- # #overwriting the custom settings for the settings from the api
26
- response.setApiSettings(configuration)
27
-
28
- modelResult = jsonify(response.processApiData())
29
- except Exception as e:
30
- error = "Oops: " + str(e)
31
-
32
- return [error, modelResult]
33
-
34
- def createTeacherJson(configuration):
35
- """
36
- This function extracts the information about the subquestions and subanswers and puts them in the correct format.
37
- Inputs:
38
- config: The configured info from the api.
39
- Outputs:
40
- teachersJson: The generated dictionary with the subquestions.
41
- """
42
- teachersJson = {"enunciado": "", "minipreguntas":[], "keywords":""}
43
-
44
- #5 is the maximum number of permitted subquestions in the configuration2 page
45
-
46
- for i in range(5):
47
-
48
- try:
49
- teachersJson["minipreguntas"].append({
50
- "minipregunta": configuration["minip" + str(i+1)],
51
- "minirespuesta": configuration["minir" + str(i+1)]
52
- })
53
-
54
- except:
55
- break
56
-
57
- return teachersJson
58
-
59
- def extractZipData(ruta_zip):
60
- """
61
- This function extracts the students's answers from the zip file (the one the teacher has in the task section).
62
- Inputs:
63
- ruta_zip: The path inherited from answersTodict
64
- """
65
- #defining the path where the extracted info is to be stored
66
- ruta_extraccion = create_file_path("StudentAnswers/", doctype= 1)
67
- #extracting the info
68
- archivo_zip = zipfile.ZipFile(ruta_zip, "r")
69
- try:
70
- archivo_zip.extractall(pwd=None, path=ruta_extraccion)
71
- except:
72
- pass
73
- archivo_zip.close()
74
-
75
- def removeHtmlFromString(string):
76
- """
77
- This function removes the html tags from the student's response.
78
- Inputs:
79
- -string: The student's response
80
- Outputs:
81
- -new_string: The filtered response
82
- """
83
- string = string.encode('utf-8', 'replace')
84
- string = string.decode('utf-8', 'replace')
85
- new_string = ""
86
- skipChar = 0
87
- for char in string:
88
- if char == "<":
89
- skipChar = 1
90
- elif char == ">":
91
- skipChar = 0
92
- else:
93
- if not skipChar:
94
- new_string = new_string+char
95
-
96
- new_string = new_string.encode('utf-8', 'replace')
97
- new_string = new_string.decode('utf-8', 'replace')
98
- return new_string
99
-
100
- def answersTodict(zip_path):
101
- """
102
- This function extracts the students's answers and stacks them in one specific format so that it can be processed next.
103
- Inputs:
104
- ruta_zip: The path where the zip file is stored
105
- Outputs:
106
- studentAnswersDict: The dictionary with all the responses
107
- """
108
- #extracting the data
109
- extractZipData(zip_path)
110
-
111
- studentAnswersDict = []
112
-
113
- #stacking the information of each extracted folder
114
- for work_folder in os.listdir(create_file_path("StudentAnswers/", doctype= 1)):
115
- 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))))):
116
- student_name = student.split("(")
117
- student_name = student_name[0]
118
- try:
119
- #opening the file
120
-
121
- #fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'comments.txt', doctype= 1))
122
- #where the actual response is
123
- fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')
124
- #reading it
125
- lineas = fichero.readlines()
126
-
127
- #removing html
128
- lineas[0] = removeHtmlFromString(lineas[0])
129
-
130
- #saving it
131
- studentAnswersDict.append({"respuesta":lineas[0], "hashed_id":student_name, "TableIndex":indx})
132
-
133
- except:
134
- studentAnswersDict.append({"respuesta":"", "hashed_id":student_name, "TableIndex":indx})
135
-
136
- #saving the final dictionary
137
- save_json(create_file_path('ApiStudentsDict.json', doctype= 1),studentAnswersDict)
138
- return studentAnswersDict
139
-
140
- configuration = gr.inputs.Textbox(lines=10, placeholder="JSON de Configuración")
141
- zipFileInput = gr.inputs.File(label="ZIP file")
142
- #dataFrameOutput = gr.outputs.Dataframe(headers=["Resultados"], max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="Resultado")
143
-
144
- labelOutput = gr.outputs.Label(num_top_classes=None, type="auto", label="")
145
- labelError = gr.outputs.Label(num_top_classes=None, type="auto", label="")
146
-
147
- iface = gr.Interface(fn=Main
148
- , inputs=[configuration, zipFileInput]
149
- , outputs=[labelError, labelOutput]
150
- , title = "PLENTAS"
151
- )
152
-
153
- iface.launch(share = False,enable_queue=True, show_error =True, server_port=7870)
 
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
6
+ from plentas import Plentas
7
+ import pandas as pd
8
+ import zipfile
9
+ import os
10
+
11
+ def Main(configuration, uploadedFile):
12
+
13
+ error = ""
14
+ modelResult = ""
15
+
16
+ configuration_dict = json.loads(configuration)
17
+
18
+ try:
19
+ uploadedFilePath = uploadedFile.name
20
+
21
+ config_json = load_json("configV2.json")
22
+
23
+ #configuring plentas methodology
24
+ response = Plentas(config_json[0], [answersTodict(uploadedFilePath), createTeacherJson(configuration_dict)])
25
+ # #overwriting the custom settings for the settings from the api
26
+ response.setApiSettings(configuration)
27
+
28
+ modelResult = jsonify(response.processApiData())
29
+ except Exception as e:
30
+ error = "Oops: " + str(e)
31
+
32
+ return [error, modelResult]
33
+
34
+ def createTeacherJson(configuration):
35
+ """
36
+ This function extracts the information about the subquestions and subanswers and puts them in the correct format.
37
+ Inputs:
38
+ config: The configured info from the api.
39
+ Outputs:
40
+ teachersJson: The generated dictionary with the subquestions.
41
+ """
42
+ teachersJson = {"enunciado": "", "minipreguntas":[], "keywords":""}
43
+
44
+ #5 is the maximum number of permitted subquestions in the configuration2 page
45
+
46
+ for i in range(5):
47
+
48
+ try:
49
+ teachersJson["minipreguntas"].append({
50
+ "minipregunta": configuration["minip" + str(i+1)],
51
+ "minirespuesta": configuration["minir" + str(i+1)]
52
+ })
53
+
54
+ except:
55
+ break
56
+
57
+ return teachersJson
58
+
59
+ def extractZipData(ruta_zip):
60
+ """
61
+ This function extracts the students's answers from the zip file (the one the teacher has in the task section).
62
+ Inputs:
63
+ ruta_zip: The path inherited from answersTodict
64
+ """
65
+ #defining the path where the extracted info is to be stored
66
+ ruta_extraccion = create_file_path("StudentAnswers/", doctype= 1)
67
+ #extracting the info
68
+ archivo_zip = zipfile.ZipFile(ruta_zip, "r")
69
+ try:
70
+ archivo_zip.extractall(pwd=None, path=ruta_extraccion)
71
+ except:
72
+ pass
73
+ archivo_zip.close()
74
+
75
+ def removeHtmlFromString(string):
76
+ """
77
+ This function removes the html tags from the student's response.
78
+ Inputs:
79
+ -string: The student's response
80
+ Outputs:
81
+ -new_string: The filtered response
82
+ """
83
+ string = string.encode('utf-8', 'replace')
84
+ string = string.decode('utf-8', 'replace')
85
+ new_string = ""
86
+ skipChar = 0
87
+ for char in string:
88
+ if char == "<":
89
+ skipChar = 1
90
+ elif char == ">":
91
+ skipChar = 0
92
+ else:
93
+ if not skipChar:
94
+ new_string = new_string+char
95
+
96
+ new_string = new_string.encode('utf-8', 'replace')
97
+ new_string = new_string.decode('utf-8', 'replace')
98
+ return new_string
99
+
100
+ def answersTodict(zip_path):
101
+ """
102
+ This function extracts the students's answers and stacks them in one specific format so that it can be processed next.
103
+ Inputs:
104
+ ruta_zip: The path where the zip file is stored
105
+ Outputs:
106
+ studentAnswersDict: The dictionary with all the responses
107
+ """
108
+ #extracting the data
109
+ extractZipData(zip_path)
110
+
111
+ studentAnswersDict = []
112
+
113
+ #stacking the information of each extracted folder
114
+ for work_folder in os.listdir(create_file_path("StudentAnswers/", doctype= 1)):
115
+ 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))))):
116
+ student_name = student.split("(")
117
+ student_name = student_name[0]
118
+ try:
119
+ #opening the file
120
+
121
+ #fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'comments.txt', doctype= 1))
122
+ #where the actual response is
123
+ fichero = open(create_file_path("StudentAnswers/" + work_folder + "/" + student + "/" + 'Adjuntos del envio/Respuesta enviada', doctype= 1), encoding='utf-8')
124
+ #reading it
125
+ lineas = fichero.readlines()
126
+
127
+ #removing html
128
+ lineas[0] = removeHtmlFromString(lineas[0])
129
+
130
+ #saving it
131
+ studentAnswersDict.append({"respuesta":lineas[0], "hashed_id":student_name, "TableIndex":indx})
132
+
133
+ except:
134
+ studentAnswersDict.append({"respuesta":"", "hashed_id":student_name, "TableIndex":indx})
135
+
136
+ #saving the final dictionary
137
+ save_json(create_file_path('ApiStudentsDict.json', doctype= 1),studentAnswersDict)
138
+ return studentAnswersDict
139
+
140
+ configuration = gr.inputs.Textbox(lines=10, placeholder="JSON de Configuración")
141
+ zipFileInput = gr.inputs.File(label="ZIP file")
142
+ #dataFrameOutput = gr.outputs.Dataframe(headers=["Resultados"], max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="Resultado")
143
+
144
+ labelOutput = gr.outputs.Label(num_top_classes=None, type="auto", label="")
145
+ labelError = gr.outputs.Label(num_top_classes=None, type="auto", label="")
146
+
147
+ iface = gr.Interface(fn=Main
148
+ , inputs=[configuration, zipFileInput]
149
+ , outputs=[labelError, labelOutput]
150
+ , title = "PLENTAS"
151
+ )
152
+
153
+ iface.launch(share = False,enable_queue=True, show_error =True)