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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install requests transformers
|
2 |
+
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# 1) Lista completa das perguntas
|
8 |
+
questions = [
|
9 |
+
{
|
10 |
+
"id": 1,
|
11 |
+
"question": "1.1 - Unidade do CEU:",
|
12 |
+
"type": "radio",
|
13 |
+
"options": [
|
14 |
+
"CEU itapoã",
|
15 |
+
"CEU Recanto",
|
16 |
+
"CEU QNR 02",
|
17 |
+
"CEU QMN 28",
|
18 |
+
"CEU QNN 13",
|
19 |
+
"Outro"
|
20 |
+
],
|
21 |
+
"otherOption": True,
|
22 |
+
"section": "1. Informações Gerais",
|
23 |
+
"allowComment": False
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"id": 2,
|
27 |
+
"question": "1.2 - Categoria de Participante:",
|
28 |
+
"type": "radio",
|
29 |
+
"options": [
|
30 |
+
"Aluno(a)",
|
31 |
+
"Pai/Mãe/Responsável",
|
32 |
+
"Professor(a)",
|
33 |
+
"Gestor(a)",
|
34 |
+
"Monitor(a)",
|
35 |
+
"Outro"
|
36 |
+
],
|
37 |
+
"otherOption": True,
|
38 |
+
"section": "1. Informações Gerais",
|
39 |
+
"allowComment": False,
|
40 |
+
"interpretation": "A pergunta identifica qual o tipo de envolvimento do participante com o CEU..."
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"id": 3,
|
44 |
+
"question": "2.1 - Como você avalia a infraestrutura da unidade do CEU que frequenta?",
|
45 |
+
"type": "radio",
|
46 |
+
"options": ["Excelente", "Boa", "Regular", "Ruim", "Muito Ruim"],
|
47 |
+
"section": "2. Avaliação Geral",
|
48 |
+
"allowComment": True,
|
49 |
+
"interpretation": "A maioria dos alunos avalia a infraestrutura como boa ou excelente..."
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"id": 4,
|
53 |
+
"question": "2.3 - Como você avalia os programas e atividades oferecidos?",
|
54 |
+
"type": "radio",
|
55 |
+
"options": ["Excelente", "Boa", "Regular", "Ruim", "Muito Ruim"],
|
56 |
+
"section": "2. Avaliação Geral",
|
57 |
+
"allowComment": True,
|
58 |
+
"interpretation": "Os programas e atividades oferecidos são amplamente bem avaliados..."
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"id": 5,
|
62 |
+
"question": "2.4 - Os horários das atividades atendem às suas necessidades?",
|
63 |
+
"type": "radio",
|
64 |
+
"options": ["Sim", "Parcialmente", "Não"],
|
65 |
+
"section": "2. Avaliação Geral",
|
66 |
+
"allowComment": True,
|
67 |
+
"interpretation": "Horários adequados facilitam a participação..."
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"id": 6,
|
71 |
+
"question": "2.5 - Você sente que sua participação nas atividades contribui para seu desenvolvimento pessoal/profissional?",
|
72 |
+
"type": "radio",
|
73 |
+
"options": ["Sim", "Parcialmente", "Não"],
|
74 |
+
"section": "2. Avaliação Geral",
|
75 |
+
"allowComment": True,
|
76 |
+
"interpretation": "A percepção de ganho pessoal/profissional é um indicador..."
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"id": 7,
|
80 |
+
"question": "3.1 - Como você avalia o atendimento da equipe do CEU (monitores, professores, gestores)?",
|
81 |
+
"type": "radio",
|
82 |
+
"options": ["Excelente", "Boa", "Regular", "Ruim", "Muito Ruim"],
|
83 |
+
"section": "3. Atendimento e Qualidade do Serviço",
|
84 |
+
"allowComment": True,
|
85 |
+
"interpretation": "O atendimento da equipe é crucial para a satisfação..."
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"id": 8,
|
89 |
+
"question": "3.2 - Você sente que a equipe do CEU está disponível e pronta para ajudar quando necessário?",
|
90 |
+
"type": "radio",
|
91 |
+
"options": ["Sempre", "Na maioria das vezes", "Às vezes", "Raramente", "Nunca"],
|
92 |
+
"section": "3. Atendimento e Qualidade do Serviço",
|
93 |
+
"allowComment": True,
|
94 |
+
"interpretation": "Disponibilidade e prontidão da equipe refletem diretamente no bem-estar..."
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"id": 9,
|
98 |
+
"question": "3.3 - Como você avalia a comunicação da unidade (informação sobre eventos, mudanças, etc.)?",
|
99 |
+
"type": "radio",
|
100 |
+
"options": ["Excelente", "Boa", "Regular", "Ruim", "Muito Ruim"],
|
101 |
+
"section": "3. Atendimento e Qualidade do Serviço",
|
102 |
+
"allowComment": True,
|
103 |
+
"interpretation": "Uma boa comunicação é essencial para engajamento..."
|
104 |
+
},
|
105 |
+
{
|
106 |
+
"id": 10,
|
107 |
+
"question": "4.1 - As atividades do CEU têm impacto positivo na sua vida ou na de seus filhos?",
|
108 |
+
"type": "radio",
|
109 |
+
"options": ["Sim", "Parcialmente", "Não"],
|
110 |
+
"section": "4. Impacto e Satisfação Pessoal",
|
111 |
+
"allowComment": True,
|
112 |
+
"interpretation": "Medir o impacto positivo demonstra o valor social do CEU..."
|
113 |
+
},
|
114 |
+
{
|
115 |
+
"id": 11,
|
116 |
+
"question": "4.2 - Você indicaria o CEU para amigos ou familiares?",
|
117 |
+
"type": "radio",
|
118 |
+
"options": ["Sim", "Talvez", "Não"],
|
119 |
+
"section": "4. Impacto e Satisfação Pessoal",
|
120 |
+
"allowComment": True,
|
121 |
+
"interpretation": "O indicador de recomendação sugere grau de satisfação..."
|
122 |
+
},
|
123 |
+
{
|
124 |
+
"id": 12,
|
125 |
+
"question": "5.1 - Há algo que você gostaria de ver melhorado no CEU?",
|
126 |
+
"type": "radio",
|
127 |
+
"options": ["Sim", "Não"],
|
128 |
+
"section": "5. Sugestões e Melhorias",
|
129 |
+
"allowComment": True,
|
130 |
+
"interpretation": "Esta pergunta abre espaço para melhorias pontuais e específicas..."
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"id": 13,
|
134 |
+
"question": "5.2 - Que tipo de novas atividades ou programas você gostaria de ver no CEU?",
|
135 |
+
"type": "radio",
|
136 |
+
"options": ["Sim", "Não"],
|
137 |
+
"section": "5. Sugestões e Melhorias",
|
138 |
+
"allowComment": True,
|
139 |
+
"interpretation": "Novas atividades ou programas expandem a atratividade do CEU..."
|
140 |
+
},
|
141 |
+
{
|
142 |
+
"id": 14,
|
143 |
+
"question": "5.3 - Há algum outro comentário ou sugestão que gostaria de compartilhar?",
|
144 |
+
"type": "radio",
|
145 |
+
"options": ["Sim", "Não"],
|
146 |
+
"section": "5. Sugestões e Melhorias",
|
147 |
+
"allowComment": True,
|
148 |
+
"interpretation": "Um espaço aberto para o participante expressar livremente..."
|
149 |
+
}
|
150 |
+
]
|
151 |
+
|
152 |
+
# Mapeia id da pergunta => texto
|
153 |
+
questions_map = {q["id"]: q["question"] for q in questions}
|
154 |
+
|
155 |
+
# URL da API
|
156 |
+
url = "https://proced.datasavvy.com.br/api/responses"
|
157 |
+
|
158 |
+
# Consome a API
|
159 |
+
response = requests.get(url)
|
160 |
+
if response.status_code == 200:
|
161 |
+
data = response.json()
|
162 |
+
else:
|
163 |
+
print("Erro ao acessar a API:", response.status_code)
|
164 |
+
exit()
|
165 |
+
|
166 |
+
# Montar lista de associações
|
167 |
+
associacoes = []
|
168 |
+
for registro in data:
|
169 |
+
if "answers" in registro and "comments" in registro:
|
170 |
+
for pergunta_id_str, resposta in registro["answers"].items():
|
171 |
+
try:
|
172 |
+
pergunta_id = int(pergunta_id_str)
|
173 |
+
except ValueError:
|
174 |
+
pergunta_id = None
|
175 |
+
|
176 |
+
comentario = registro["comments"].get(pergunta_id_str, None)
|
177 |
+
|
178 |
+
# Aqui, ao invés de "studentId": registro["studentId"]
|
179 |
+
# Usamos o token que está em registro["student_info"]["token"]
|
180 |
+
associacoes.append({
|
181 |
+
"studentId": registro["student_info"]["token"], # <== Mudança aqui
|
182 |
+
"perguntaId": pergunta_id_str,
|
183 |
+
"perguntaTexto": questions_map.get(pergunta_id, f"Pergunta {pergunta_id_str}"),
|
184 |
+
"resposta": resposta,
|
185 |
+
"comentario": comentario
|
186 |
+
})
|
187 |
+
|
188 |
+
# Inicializar o pipeline de Zero-Shot Classification
|
189 |
+
classifier = pipeline(
|
190 |
+
task="zero-shot-classification",
|
191 |
+
model="joeddav/xlm-roberta-large-xnli" # modelo multilíngue PT
|
192 |
+
)
|
193 |
+
|
194 |
+
candidate_labels = [
|
195 |
+
"sugestões de melhoria",
|
196 |
+
"elogios",
|
197 |
+
"reclamações",
|
198 |
+
"solicitações de serviços ou infraestrutura"
|
199 |
+
]
|
200 |
+
|
201 |
+
classified_comments = []
|
202 |
+
for registro in data:
|
203 |
+
if "comments" not in registro or "answers" not in registro:
|
204 |
+
continue
|
205 |
+
|
206 |
+
for question_id_str, comment in registro["comments"].items():
|
207 |
+
if not comment or not comment.strip():
|
208 |
+
continue
|
209 |
+
|
210 |
+
answer = registro["answers"].get(question_id_str, "Sem resposta")
|
211 |
+
|
212 |
+
try:
|
213 |
+
q_id = int(question_id_str)
|
214 |
+
except ValueError:
|
215 |
+
q_id = None
|
216 |
+
|
217 |
+
question_text = questions_map.get(q_id, f"Pergunta {question_id_str}")
|
218 |
+
|
219 |
+
text_for_model = f"Pergunta: {question_text}. Comentário: {comment}"
|
220 |
+
|
221 |
+
classification_result = classifier(
|
222 |
+
text_for_model,
|
223 |
+
candidate_labels,
|
224 |
+
multi_label=False
|
225 |
+
)
|
226 |
+
|
227 |
+
best_label = classification_result["labels"][0]
|
228 |
+
best_score = classification_result["scores"][0]
|
229 |
+
|
230 |
+
# Aqui também
|
231 |
+
classified_comments.append({
|
232 |
+
"studentId": registro["student_info"]["token"], # <== Mudança aqui
|
233 |
+
"questionId": question_id_str,
|
234 |
+
"questionText": question_text,
|
235 |
+
"answer": answer,
|
236 |
+
"comment": comment,
|
237 |
+
"classification": best_label,
|
238 |
+
"confidence": best_score
|
239 |
+
})
|
240 |
+
|
241 |
+
# Salvar resultados em JSON
|
242 |
+
with open("associacoes.json", "w", encoding="utf-8") as assoc_file:
|
243 |
+
json.dump(associacoes, assoc_file, indent=4, ensure_ascii=False)
|
244 |
+
print("Arquivo 'associacoes.json' criado com sucesso!")
|
245 |
+
|
246 |
+
with open("classified_comments.json", "w", encoding="utf-8") as classified_file:
|
247 |
+
json.dump(classified_comments, classified_file, indent=4, ensure_ascii=False)
|
248 |
+
print("Arquivo 'classified_comments.json' criado com sucesso!")
|
249 |
+
|
250 |
+
# Mostra uma amostra
|
251 |
+
print("\n--- Associações (amostra) ---")
|
252 |
+
for assoc in associacoes[:5]:
|
253 |
+
print(assoc)
|
254 |
+
|
255 |
+
print("\n--- Classificações (amostra) ---")
|
256 |
+
for classified in classified_comments[:5]:
|
257 |
+
print(classified)
|