Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
from transformers import pipeline
|
4 |
-
import torch
|
5 |
|
6 |
class DiscAnalyzer:
|
7 |
def __init__(self):
|
@@ -12,40 +11,27 @@ class DiscAnalyzer:
|
|
12 |
'C': 'Compliance'
|
13 |
}
|
14 |
|
15 |
-
#
|
16 |
-
# Usando BLOOM-560M que tem bom suporte para português
|
17 |
-
self.tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-560m")
|
18 |
-
self.model = AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m")
|
19 |
self.generator = pipeline(
|
20 |
"text-generation",
|
21 |
-
model=
|
22 |
-
|
23 |
-
device=0 if torch.cuda.is_available() else -1
|
24 |
)
|
25 |
|
26 |
def generate_llm_report(self, scores, perfil_principal):
|
27 |
"""
|
28 |
-
Gera relatório personalizado usando modelo
|
29 |
-
"""
|
30 |
-
prompt = f"""
|
31 |
-
Analise o seguinte perfil DISC e gere um relatório detalhado:
|
32 |
-
|
33 |
-
Perfil DISC:
|
34 |
-
- Dominância: {scores['D']:.1f}%
|
35 |
-
- Influência: {scores['I']:.1f}%
|
36 |
-
- Estabilidade: {scores['S']:.1f}%
|
37 |
-
- Conformidade: {scores['C']:.1f}%
|
38 |
-
|
39 |
-
Perfil Principal: {self.disc_profiles[perfil_principal]}
|
40 |
-
|
41 |
-
Relatório do Perfil:
|
42 |
"""
|
|
|
|
|
|
|
|
|
43 |
|
44 |
try:
|
45 |
-
#
|
46 |
output = self.generator(
|
47 |
prompt,
|
48 |
-
max_length=
|
49 |
num_return_sequences=1,
|
50 |
temperature=0.7,
|
51 |
top_p=0.9,
|
@@ -57,38 +43,22 @@ class DiscAnalyzer:
|
|
57 |
except Exception as e:
|
58 |
return f"Erro ao gerar relatório: {str(e)}"
|
59 |
|
60 |
-
def avaliar_disc(self,
|
61 |
-
q1, q2, q3, q4,
|
62 |
-
q5, q6, q7, q8,
|
63 |
-
q9, q10, q11, q12,
|
64 |
-
q13, q14, q15, q16):
|
65 |
"""
|
66 |
-
|
67 |
"""
|
68 |
-
# Converter inputs para números
|
69 |
-
responses = [
|
70 |
-
int(q1), int(q2), int(q3), int(q4),
|
71 |
-
int(q5), int(q6), int(q7), int(q8),
|
72 |
-
int(q9), int(q10), int(q11), int(q12),
|
73 |
-
int(q13), int(q14), int(q15), int(q16)
|
74 |
-
]
|
75 |
-
|
76 |
-
# Calcular scores para cada dimensão DISC
|
77 |
-
d_score = sum([responses[i] for i in [0, 4, 8, 12]]) / 16 * 100
|
78 |
-
i_score = sum([responses[i] for i in [1, 5, 9, 13]]) / 16 * 100
|
79 |
-
s_score = sum([responses[i] for i in [2, 6, 10, 14]]) / 16 * 100
|
80 |
-
c_score = sum([responses[i] for i in [3, 7, 11, 15]]) / 16 * 100
|
81 |
|
82 |
-
#
|
83 |
scores = {
|
84 |
-
'D':
|
85 |
-
'I':
|
86 |
-
'S':
|
87 |
-
'C':
|
88 |
}
|
89 |
-
perfil_principal = max(scores, key=scores.get)
|
90 |
|
91 |
-
|
92 |
relatorio_llm = self.generate_llm_report(scores, perfil_principal)
|
93 |
|
94 |
return (
|
@@ -101,45 +71,31 @@ class DiscAnalyzer:
|
|
101 |
)
|
102 |
|
103 |
def create_disc_interface():
|
104 |
-
"""
|
105 |
-
Cria a interface Gradio para o analisador DISC.
|
106 |
-
"""
|
107 |
analyzer = DiscAnalyzer()
|
108 |
|
109 |
with gr.Blocks(title="Análise de Perfil DISC") as interface:
|
110 |
gr.Markdown("# Analisador de Perfil DISC com IA")
|
111 |
-
gr.Markdown(""
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
q4 = gr.Slider(1, 4, value=2, step=1, label="4. Sou preciso e analítico")
|
125 |
-
|
126 |
-
with gr.Column():
|
127 |
-
q5 = gr.Slider(1, 4, value=2, step=1, label="5. Gosto de assumir riscos")
|
128 |
-
q6 = gr.Slider(1, 4, value=2, step=1, label="6. Sou bom comunicador")
|
129 |
-
q7 = gr.Slider(1, 4, value=2, step=1, label="7. Sou bom ouvinte")
|
130 |
-
q8 = gr.Slider(1, 4, value=2, step=1, label="8. Sou organizado")
|
131 |
-
|
132 |
-
with gr.Column():
|
133 |
-
q9 = gr.Slider(1, 4, value=2, step=1, label="9. Tomo iniciativa")
|
134 |
-
q10 = gr.Slider(1, 4, value=2, step=1, label="10. Sou persuasivo")
|
135 |
-
q11 = gr.Slider(1, 4, value=2, step=1, label="11. Trabalho bem em equipe")
|
136 |
-
q12 = gr.Slider(1, 4, value=2, step=1, label="12. Sou detalhista")
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
143 |
|
144 |
analyze_btn = gr.Button("Analisar Perfil")
|
145 |
|
@@ -151,15 +107,13 @@ def create_disc_interface():
|
|
151 |
x="Dimensão",
|
152 |
y="Score",
|
153 |
title="Scores DISC",
|
154 |
-
tooltip=["Dimensão", "Score"],
|
155 |
height=400,
|
156 |
width=600
|
157 |
)
|
158 |
|
159 |
analyze_btn.click(
|
160 |
fn=analyzer.avaliar_disc,
|
161 |
-
inputs=
|
162 |
-
q9, q10, q11, q12, q13, q14, q15, q16],
|
163 |
outputs=[perfil_output, relatorio_output, plot_output]
|
164 |
)
|
165 |
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
from transformers import pipeline
|
|
|
4 |
|
5 |
class DiscAnalyzer:
|
6 |
def __init__(self):
|
|
|
11 |
'C': 'Compliance'
|
12 |
}
|
13 |
|
14 |
+
# Usando um modelo mais leve e otimizado para texto
|
|
|
|
|
|
|
15 |
self.generator = pipeline(
|
16 |
"text-generation",
|
17 |
+
model="facebook/opt-125m", # Modelo muito mais leve que o BLOOM
|
18 |
+
device="cpu" # Forçando CPU para evitar problemas de CUDA
|
|
|
19 |
)
|
20 |
|
21 |
def generate_llm_report(self, scores, perfil_principal):
|
22 |
"""
|
23 |
+
Gera relatório personalizado usando modelo mais leve.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
"""
|
25 |
+
prompt = f"""Analise o perfil DISC:
|
26 |
+
D:{scores['D']:.1f}% I:{scores['I']:.1f}% S:{scores['S']:.1f}% C:{scores['C']:.1f}%
|
27 |
+
Perfil Principal: {self.disc_profiles[perfil_principal]}
|
28 |
+
"""
|
29 |
|
30 |
try:
|
31 |
+
# Configurações otimizadas para geração mais rápida
|
32 |
output = self.generator(
|
33 |
prompt,
|
34 |
+
max_length=300, # Reduzido para resposta mais concisa
|
35 |
num_return_sequences=1,
|
36 |
temperature=0.7,
|
37 |
top_p=0.9,
|
|
|
43 |
except Exception as e:
|
44 |
return f"Erro ao gerar relatório: {str(e)}"
|
45 |
|
46 |
+
def avaliar_disc(self, *args):
|
|
|
|
|
|
|
|
|
47 |
"""
|
48 |
+
Versão otimizada da avaliação DISC.
|
49 |
"""
|
50 |
+
# Converter todos os inputs para números de uma vez
|
51 |
+
responses = [int(arg) for arg in args]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Cálculo otimizado dos scores
|
54 |
scores = {
|
55 |
+
'D': sum(responses[i] for i in [0, 4, 8, 12]) / 16 * 100,
|
56 |
+
'I': sum(responses[i] for i in [1, 5, 9, 13]) / 16 * 100,
|
57 |
+
'S': sum(responses[i] for i in [2, 6, 10, 14]) / 16 * 100,
|
58 |
+
'C': sum(responses[i] for i in [3, 7, 11, 15]) / 16 * 100
|
59 |
}
|
|
|
60 |
|
61 |
+
perfil_principal = max(scores, key=scores.get)
|
62 |
relatorio_llm = self.generate_llm_report(scores, perfil_principal)
|
63 |
|
64 |
return (
|
|
|
71 |
)
|
72 |
|
73 |
def create_disc_interface():
|
|
|
|
|
|
|
74 |
analyzer = DiscAnalyzer()
|
75 |
|
76 |
with gr.Blocks(title="Análise de Perfil DISC") as interface:
|
77 |
gr.Markdown("# Analisador de Perfil DISC com IA")
|
78 |
+
gr.Markdown("Responda cada questão com valores de 1 a 4")
|
79 |
+
|
80 |
+
# Criação otimizada das questões
|
81 |
+
questions = [
|
82 |
+
"1. Sou direto e decisivo", "2. Sou sociável e entusiasta",
|
83 |
+
"3. Sou paciente e cooperativo", "4. Sou preciso e analítico",
|
84 |
+
"5. Gosto de assumir riscos", "6. Sou bom comunicador",
|
85 |
+
"7. Sou bom ouvinte", "8. Sou organizado",
|
86 |
+
"9. Tomo iniciativa", "10. Sou persuasivo",
|
87 |
+
"11. Trabalho bem em equipe", "12. Sou detalhista",
|
88 |
+
"13. Busco resultados", "14. Sou motivador",
|
89 |
+
"15. Sou confiável", "16. Sou sistemático"
|
90 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
# Criando sliders em grupos de 4
|
93 |
+
sliders = []
|
94 |
+
for i in range(0, 16, 4):
|
95 |
+
with gr.Row():
|
96 |
+
with gr.Column():
|
97 |
+
for j in range(4):
|
98 |
+
sliders.append(gr.Slider(1, 4, value=2, step=1, label=questions[i+j]))
|
99 |
|
100 |
analyze_btn = gr.Button("Analisar Perfil")
|
101 |
|
|
|
107 |
x="Dimensão",
|
108 |
y="Score",
|
109 |
title="Scores DISC",
|
|
|
110 |
height=400,
|
111 |
width=600
|
112 |
)
|
113 |
|
114 |
analyze_btn.click(
|
115 |
fn=analyzer.avaliar_disc,
|
116 |
+
inputs=sliders,
|
|
|
117 |
outputs=[perfil_output, relatorio_output, plot_output]
|
118 |
)
|
119 |
|