Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,85 +2,63 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
|
5 |
-
class
|
6 |
def __init__(self):
|
7 |
-
self.size =
|
8 |
-
self.cube = np.zeros((6,
|
9 |
|
10 |
-
# Inicializar com
|
11 |
-
|
12 |
-
|
13 |
-
self.cube[1][0][0] = 2 # Verde
|
14 |
-
self.cube[1][0][1] = 2 # Verde
|
15 |
-
self.cube[1][1][0] = 4 # Vermelho
|
16 |
-
self.cube[1][1][1] = 3 # Azul
|
17 |
-
|
18 |
-
# Face 2: 2 laranjas, 1 branco, 1 amarelo, resto verde
|
19 |
-
self.cube[2] = np.full((4, 4), 2) # Preenche tudo com verde
|
20 |
-
self.cube[2][0][0] = 5 # Laranja
|
21 |
-
self.cube[2][0][1] = 5 # Laranja
|
22 |
-
self.cube[2][1][0] = 0 # Branco
|
23 |
-
self.cube[2][1][1] = 1 # Amarelo
|
24 |
-
|
25 |
-
# Face 4: 1 verde, 1 vermelho, 1 azul, 1 amarelo, resto vermelho
|
26 |
-
self.cube[4] = np.full((4, 4), 4) # Preenche tudo com vermelho
|
27 |
-
self.cube[4][0][0] = 2 # Verde
|
28 |
-
self.cube[4][0][1] = 4 # Vermelho
|
29 |
-
self.cube[4][1][0] = 3 # Azul
|
30 |
-
self.cube[4][1][1] = 1 # Amarelo
|
31 |
-
|
32 |
-
# Outras faces com cores padrão
|
33 |
-
for i in [0,3,5]:
|
34 |
-
self.cube[i] = np.full((4, 4), i)
|
35 |
|
36 |
self.color_names = {
|
37 |
-
0: "Branco",
|
38 |
-
1: "Amarelo",
|
39 |
-
2: "Verde",
|
40 |
-
3: "Azul",
|
41 |
-
4: "Vermelho",
|
42 |
-
5: "Laranja"
|
43 |
}
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def get_solution(self):
|
46 |
"""Retorna sequência de passos para resolver o cubo"""
|
47 |
solution = []
|
48 |
|
49 |
-
# Analisar face
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
#
|
58 |
-
solution.append("
|
59 |
-
solution.append("
|
60 |
-
solution.append("-
|
61 |
-
solution.append("-
|
62 |
-
solution.append("- Restante em verde")
|
63 |
solution.append("")
|
64 |
-
|
65 |
-
|
66 |
-
solution.append("
|
67 |
-
solution.append("- Encontrado 1 centro verde")
|
68 |
-
solution.append("- Encontrado 1 centro vermelho")
|
69 |
-
solution.append("- Encontrado 1 centro azul")
|
70 |
-
solution.append("- Encontrado 1 centro amarelo")
|
71 |
-
solution.append("- Restante em vermelho")
|
72 |
solution.append("")
|
73 |
-
|
74 |
-
solution.append("
|
75 |
-
solution.append("
|
76 |
-
solution.append("2. Resolver bordas")
|
77 |
-
solution.append("3. Resolver como 3x3x3")
|
78 |
-
solution.append("4. Corrigir paridades")
|
79 |
|
80 |
return solution
|
81 |
|
82 |
def create_visualization(cube_state):
|
83 |
-
fig, ax = plt.subplots(
|
84 |
plt.subplots_adjust(hspace=0.4, wspace=0.4)
|
85 |
|
86 |
colors = {
|
@@ -102,60 +80,104 @@ def create_visualization(cube_state):
|
|
102 |
}
|
103 |
|
104 |
face_positions = [
|
105 |
-
(
|
106 |
-
(
|
107 |
-
(1, 2), # Face direita
|
108 |
-
(1, 0), # Face esquerda
|
109 |
-
(
|
110 |
-
(
|
111 |
]
|
112 |
|
113 |
for face_idx, (row, col) in enumerate(face_positions):
|
114 |
-
|
115 |
-
|
116 |
-
for
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
for i in range(
|
131 |
for j in range(4):
|
132 |
if (i, j) not in face_positions:
|
133 |
fig.delaxes(ax[i, j])
|
134 |
|
135 |
return fig
|
136 |
|
137 |
-
def
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
# Criar interface Gradio
|
144 |
-
with gr.Blocks(title="Cubo Mágico
|
145 |
gr.Markdown("""
|
146 |
-
# Cubo Mágico
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
152 |
""")
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
with gr.Row():
|
155 |
cube_vis = gr.Plot(label="Visualização do Cubo")
|
156 |
solution = gr.Textbox(label="Análise e Solução", lines=20)
|
157 |
|
158 |
-
|
|
|
|
|
|
|
|
|
159 |
|
160 |
# Iniciar aplicação
|
161 |
demo.launch()
|
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
|
5 |
+
class RubiksCube2x2:
|
6 |
def __init__(self):
|
7 |
+
self.size = 2
|
8 |
+
self.cube = np.zeros((6, 2, 2), dtype=int)
|
9 |
|
10 |
+
# Inicializar faces com cores padrão
|
11 |
+
for i in range(6):
|
12 |
+
self.cube[i] = np.full((2, 2), i)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
self.color_names = {
|
15 |
+
0: "Branco", # Face superior
|
16 |
+
1: "Amarelo", # Face inferior
|
17 |
+
2: "Verde", # Face frontal
|
18 |
+
3: "Azul", # Face traseira
|
19 |
+
4: "Vermelho", # Face direita
|
20 |
+
5: "Laranja" # Face esquerda
|
21 |
}
|
22 |
|
23 |
+
def set_face(self, face_num, colors):
|
24 |
+
"""Define as cores de uma face específica"""
|
25 |
+
if isinstance(colors, str):
|
26 |
+
colors = [int(c) for c in colors.split(',') if c.strip()]
|
27 |
+
if len(colors) == 4: # 2x2 = 4 cores
|
28 |
+
self.cube[face_num] = np.array(colors).reshape(2, 2)
|
29 |
+
else:
|
30 |
+
raise ValueError(f"Face {face_num} precisa de exatamente 4 cores")
|
31 |
+
|
32 |
def get_solution(self):
|
33 |
"""Retorna sequência de passos para resolver o cubo"""
|
34 |
solution = []
|
35 |
|
36 |
+
# Analisar cada face
|
37 |
+
for face in range(6):
|
38 |
+
colors_in_face = np.unique(self.cube[face])
|
39 |
+
solution.append(f"\nAnálise da Face {self.color_names[face]}:")
|
40 |
+
for color in colors_in_face:
|
41 |
+
count = np.count_nonzero(self.cube[face] == color)
|
42 |
+
solution.append(f"- {count}x {self.color_names[color]}")
|
43 |
|
44 |
+
# Passos para solução
|
45 |
+
solution.append("\nPassos para resolução:")
|
46 |
+
solution.append("1. Resolver face branca")
|
47 |
+
solution.append(" - Posicionar peças brancas na face superior")
|
48 |
+
solution.append(" - Alinhar cores adjacentes")
|
|
|
49 |
solution.append("")
|
50 |
+
solution.append("2. Resolver face amarela")
|
51 |
+
solution.append(" - Orientar peças amarelas")
|
52 |
+
solution.append(" - Posicionar cantos corretamente")
|
|
|
|
|
|
|
|
|
|
|
53 |
solution.append("")
|
54 |
+
solution.append("3. Verificar alinhamento")
|
55 |
+
solution.append(" - Alinhar faces laterais")
|
56 |
+
solution.append(" - Corrigir orientação se necessário")
|
|
|
|
|
|
|
57 |
|
58 |
return solution
|
59 |
|
60 |
def create_visualization(cube_state):
|
61 |
+
fig, ax = plt.subplots(3, 4, figsize=(10, 8))
|
62 |
plt.subplots_adjust(hspace=0.4, wspace=0.4)
|
63 |
|
64 |
colors = {
|
|
|
80 |
}
|
81 |
|
82 |
face_positions = [
|
83 |
+
(0, 1), # Face superior (branco)
|
84 |
+
(1, 1), # Face frontal (verde)
|
85 |
+
(1, 2), # Face direita (vermelho)
|
86 |
+
(1, 0), # Face esquerda (laranja)
|
87 |
+
(2, 1), # Face inferior (amarelo)
|
88 |
+
(1, 3), # Face traseira (azul)
|
89 |
]
|
90 |
|
91 |
for face_idx, (row, col) in enumerate(face_positions):
|
92 |
+
face = cube_state[face_idx]
|
93 |
+
for i in range(2):
|
94 |
+
for j in range(2):
|
95 |
+
color = colors[face[i, j]]
|
96 |
+
ax[row, col].add_patch(plt.Rectangle(
|
97 |
+
(j/2, (1-i)/2), 1/2, 1/2,
|
98 |
+
facecolor=color,
|
99 |
+
edgecolor='black'
|
100 |
+
))
|
101 |
+
ax[row, col].set_xlim(0, 1)
|
102 |
+
ax[row, col].set_ylim(0, 1)
|
103 |
+
ax[row, col].set_xticks([])
|
104 |
+
ax[row, col].set_yticks([])
|
105 |
+
ax[row, col].set_title(f'Face {color_names[face_idx]}')
|
106 |
+
|
107 |
+
# Remover subplots não utilizados
|
108 |
+
for i in range(3):
|
109 |
for j in range(4):
|
110 |
if (i, j) not in face_positions:
|
111 |
fig.delaxes(ax[i, j])
|
112 |
|
113 |
return fig
|
114 |
|
115 |
+
def process_input(colors_input):
|
116 |
+
"""Processa a entrada de cores e retorna visualização e solução"""
|
117 |
+
cube = RubiksCube2x2()
|
118 |
+
|
119 |
+
try:
|
120 |
+
# Separar entrada em faces
|
121 |
+
faces = colors_input.strip().split('\n')
|
122 |
+
if len(faces) > 6:
|
123 |
+
faces = faces[:6]
|
124 |
+
|
125 |
+
# Configurar cada face
|
126 |
+
for i, face in enumerate(faces):
|
127 |
+
if face.strip():
|
128 |
+
cube.set_face(i, face)
|
129 |
+
|
130 |
+
fig = create_visualization(cube.cube)
|
131 |
+
solution = "\n".join(cube.get_solution())
|
132 |
+
return fig, solution
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
return None, f"Erro ao processar entrada: {str(e)}"
|
136 |
|
137 |
# Criar interface Gradio
|
138 |
+
with gr.Blocks(title="Cubo Mágico 2x2") as demo:
|
139 |
gr.Markdown("""
|
140 |
+
# Resolvedor de Cubo Mágico 2x2
|
141 |
+
|
142 |
+
Digite as cores para cada face (4 números por face, separados por vírgula):
|
143 |
+
- 0: Branco
|
144 |
+
- 1: Amarelo
|
145 |
+
- 2: Verde
|
146 |
+
- 3: Azul
|
147 |
+
- 4: Vermelho
|
148 |
+
- 5: Laranja
|
149 |
+
|
150 |
+
Exemplo para uma face branca:
|
151 |
+
0,0,0,0
|
152 |
|
153 |
+
Digite uma face por linha, na ordem:
|
154 |
+
1. Face Superior (normalmente branca)
|
155 |
+
2. Face Inferior (normalmente amarela)
|
156 |
+
3. Face Frontal (normalmente verde)
|
157 |
+
4. Face Traseira (normalmente azul)
|
158 |
+
5. Face Direita (normalmente vermelha)
|
159 |
+
6. Face Esquerda (normalmente laranja)
|
160 |
""")
|
161 |
|
162 |
+
with gr.Row():
|
163 |
+
input_text = gr.Textbox(
|
164 |
+
label="Digite as cores (uma face por linha)",
|
165 |
+
lines=6,
|
166 |
+
placeholder="0,0,0,0\n1,1,1,1\n2,2,2,2\n3,3,3,3\n4,4,4,4\n5,5,5,5"
|
167 |
+
)
|
168 |
+
|
169 |
+
with gr.Row():
|
170 |
+
submit_btn = gr.Button("Analisar Cubo")
|
171 |
+
|
172 |
with gr.Row():
|
173 |
cube_vis = gr.Plot(label="Visualização do Cubo")
|
174 |
solution = gr.Textbox(label="Análise e Solução", lines=20)
|
175 |
|
176 |
+
submit_btn.click(
|
177 |
+
fn=process_input,
|
178 |
+
inputs=[input_text],
|
179 |
+
outputs=[cube_vis, solution]
|
180 |
+
)
|
181 |
|
182 |
# Iniciar aplicação
|
183 |
demo.launch()
|