Spaces:
Running
Running
Update interface.py
Browse files- interface.py +122 -196
interface.py
CHANGED
@@ -1,226 +1,160 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
)
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
}
|
15 |
|
16 |
-
.gradio-container {
|
17 |
-
max-width: 1400px !important;
|
18 |
-
margin: 0 auto;
|
19 |
-
width:
|
20 |
-
display: flex;
|
21 |
-
flex-direction: column;
|
22 |
}
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
display: flex;
|
27 |
-
flex-direction: column;
|
28 |
-
|
|
|
29 |
}
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
padding: 20px !important;
|
35 |
-
|
36 |
-
|
37 |
-
text-align: center !important;
|
38 |
-
height: 10vh !important;
|
39 |
-
display: flex;
|
40 |
-
flex-direction: column;
|
41 |
-
justify-content: center;
|
42 |
-
align-items: center;
|
43 |
-
flex-shrink: 0;
|
44 |
}
|
45 |
|
|
|
46 |
.chat-container {
|
47 |
-
|
48 |
-
overflow-y: auto;
|
49 |
-
|
50 |
-
|
51 |
-
}
|
52 |
-
|
53 |
-
.input-container {
|
54 |
-
height: 15vh !important; /* Aumentado para melhor centralização */
|
55 |
display: flex;
|
56 |
flex-direction: column;
|
57 |
-
justify-content: center;
|
58 |
-
align-items: center; /* Centraliza horizontalmente */
|
59 |
-
flex-shrink: 0;
|
60 |
-
padding: 10px;
|
61 |
-
margin: 0 10px;
|
62 |
}
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
padding: 20px 10px;
|
67 |
-
flex-shrink: 0;
|
68 |
}
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
background-color: #ffffff !important;
|
77 |
-
color: #1a1a1a !important;
|
78 |
-
border: 1px solid #e0e0e0 !important;
|
79 |
-
border-radius: 20px !important;
|
80 |
-
padding: 20px !important;
|
81 |
-
margin: 10px 0 !important;
|
82 |
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important;
|
83 |
-
}
|
84 |
-
|
85 |
-
.resposta-container pre code {
|
86 |
-
color: #1a1a1a !important;
|
87 |
-
background-color: #f8f9fa !important;
|
88 |
-
}
|
89 |
-
|
90 |
-
.pergunta-container {
|
91 |
-
background-color: #f0f8ff !important;
|
92 |
-
border-radius: 8px !important;
|
93 |
-
padding: 15px !important;
|
94 |
-
}
|
95 |
-
|
96 |
-
.modelo-dropdown {
|
97 |
-
margin-bottom: 15px !important;
|
98 |
-
width: 100%;
|
99 |
-
max-width: 300px; /* Limita a largura do dropdown */
|
100 |
}
|
101 |
|
102 |
#entrada_usuario textarea {
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
min-height: 60px !important;
|
107 |
-
width: 100%;
|
108 |
-
max-width: 800px; /* Limita a largura do campo de texto */
|
109 |
}
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
background-color: #
|
115 |
}
|
116 |
|
117 |
-
/*
|
118 |
-
.gr-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
121 |
}
|
122 |
|
123 |
-
/*
|
124 |
@media (max-width: 768px) {
|
125 |
.titulo-principal {
|
126 |
-
height: 12vh !important;
|
127 |
padding: 15px !important;
|
128 |
-
|
129 |
-
}
|
130 |
-
|
131 |
-
.chat-container {
|
132 |
-
height: 73vh !important;
|
133 |
}
|
134 |
-
|
135 |
.input-container {
|
136 |
-
height: 15vh !important;
|
137 |
-
padding: 8px !important;
|
138 |
-
}
|
139 |
-
|
140 |
-
#entrada_usuario textarea {
|
141 |
-
min-height: 50px !important;
|
142 |
-
font-size: 14px !important;
|
143 |
-
}
|
144 |
-
}
|
145 |
-
|
146 |
-
@media (max-width: 480px) {
|
147 |
-
.titulo-principal {
|
148 |
-
height: 15vh !important;
|
149 |
padding: 10px !important;
|
150 |
-
margin: 5px !important;
|
151 |
-
font-size: 14px !important;
|
152 |
-
}
|
153 |
-
|
154 |
-
.chat-container {
|
155 |
-
height: 70vh !important;
|
156 |
-
}
|
157 |
-
|
158 |
-
.input-container {
|
159 |
-
height: 15vh !important;
|
160 |
-
padding: 5px !important;
|
161 |
-
}
|
162 |
-
|
163 |
-
#entrada_usuario textarea {
|
164 |
-
min-height: 45px !important;
|
165 |
-
font-size: 13px !important;
|
166 |
-
}
|
167 |
-
|
168 |
-
.gr-button.primary {
|
169 |
-
max-width: 120px !important;
|
170 |
}
|
171 |
}
|
172 |
"""
|
173 |
|
174 |
-
def
|
175 |
-
with gr.Blocks(title="Dr. Aldo Henrique - API Externa", theme=gr.themes.Soft(), css=
|
176 |
-
# Contêiner principal
|
177 |
-
with gr.Column(elem_classes="
|
178 |
-
# Cabeçalho
|
179 |
gr.HTML("""
|
180 |
<div class="titulo-principal">
|
181 |
<h4>🤖 iAldo - Pergunte e aprenda</h4>
|
182 |
-
<p style="font-size: 14px; opacity: 0.9;">Converse com o <a href="https://aldohenrique.com.br/" style="color: white; text-decoration: underline;">Prof. Dr. Aldo Henrique</a></p>
|
183 |
</div>
|
184 |
""")
|
185 |
|
186 |
-
# Área do
|
187 |
with gr.Column(elem_classes="chat-container"):
|
188 |
chatbot = gr.Chatbot(
|
189 |
-
label="💬 Chat com Dr. Aldo",
|
190 |
elem_id="chat",
|
191 |
-
|
|
|
192 |
)
|
193 |
|
194 |
-
# Área de
|
195 |
with gr.Column(elem_classes="input-container"):
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
)
|
204 |
-
|
205 |
with gr.Row():
|
206 |
user_input = gr.Textbox(
|
207 |
show_label=False,
|
208 |
-
placeholder="Digite sua pergunta
|
209 |
lines=2,
|
210 |
-
elem_id="entrada_usuario"
|
|
|
211 |
)
|
212 |
-
enviar_btn = gr.Button("Enviar", variant="primary")
|
213 |
|
214 |
-
# Conteúdo adicional
|
215 |
-
with gr.Column(elem_classes="
|
216 |
-
# RAG
|
217 |
with gr.Accordion("⚙️ Controle do Conhecimento (RAG)", open=False):
|
218 |
status_rag = gr.Textbox(label="Status do Retreino", interactive=False)
|
219 |
botao_retreinar = gr.Button("🔄 Atualizar Conhecimento do Blog", variant="stop")
|
220 |
-
download_faiss_file = gr.File(label="Download do Índice FAISS", interactive=False
|
221 |
-
download_urls_file = gr.File(label="Download das URLs Processadas", interactive=False
|
222 |
|
223 |
-
# Exemplos
|
224 |
with gr.Accordion("📚 Exemplos de Perguntas", open=False):
|
225 |
gr.Examples(
|
226 |
examples=[
|
@@ -231,70 +165,62 @@ def criar_interface():
|
|
231 |
inputs=[user_input, modelo_select]
|
232 |
)
|
233 |
|
234 |
-
# Status API
|
235 |
with gr.Accordion("🔧 Status da API", open=False):
|
236 |
status_api = gr.Textbox(label="Status dos Modelos", interactive=False, lines=8)
|
237 |
|
238 |
-
# Informações
|
239 |
with gr.Accordion("ℹ️ Informações", open=False):
|
240 |
gr.Markdown("""
|
241 |
### Sobre o Dr. Aldo Henrique:
|
242 |
- **Especialidade**: Linguagens C, Java, Desenvolvimento Web, Inteligência Artificial
|
243 |
- **Conhecimento Adicional**: Conteúdo do blog aldohenrique.com.br
|
244 |
-
|
245 |
### Dicas para melhores respostas:
|
246 |
- Faça perguntas específicas sobre o conteúdo do blog para ver o RAG em ação!
|
247 |
- Peça resumos ou opiniões sobre temas que o professor aborda.
|
248 |
""")
|
249 |
|
|
|
250 |
def responder(chat_history, user_msg, modelo):
|
251 |
if not user_msg.strip():
|
252 |
return chat_history, ""
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
yield chat_history, ""
|
257 |
-
|
258 |
-
# Gera a resposta real
|
259 |
resposta_final = responder_como_aldo(user_msg, modelo)
|
260 |
-
|
261 |
-
# Atualiza com a resposta final
|
262 |
chat_history[-1][1] = resposta_final
|
263 |
-
yield chat_history,
|
264 |
|
265 |
enviar_btn.click(
|
266 |
fn=responder,
|
267 |
inputs=[chatbot, user_input, modelo_select],
|
268 |
-
outputs=[chatbot, user_input]
|
269 |
-
show_progress=True
|
270 |
)
|
271 |
-
|
272 |
user_input.submit(
|
273 |
fn=responder,
|
274 |
inputs=[chatbot, user_input, modelo_select],
|
275 |
-
outputs=[chatbot, user_input]
|
276 |
-
show_progress=True
|
277 |
)
|
278 |
-
|
279 |
botao_retreinar.click(
|
280 |
fn=build_and_save_vector_store,
|
281 |
-
outputs=[status_rag, download_faiss_file, download_urls_file]
|
282 |
-
show_progress=True
|
283 |
)
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
</script>
|
295 |
""")
|
296 |
|
297 |
return interface
|
298 |
|
299 |
def configurar_interface():
|
300 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
# Remova a importação de ai_logic se não tiver o arquivo.
|
3 |
+
# from ai_logic import (
|
4 |
+
# responder_como_aldo,
|
5 |
+
# build_and_save_vector_store,
|
6 |
+
# MODELS,
|
7 |
+
# DEFAULT_MODEL
|
8 |
+
# )
|
9 |
+
|
10 |
+
# --- Funções de exemplo para o código funcionar sem o ai_logic ---
|
11 |
+
def responder_como_aldo(user_msg, modelo):
|
12 |
+
return f"Resposta do modelo '{modelo}' para a pergunta: '{user_msg}'"
|
13 |
+
|
14 |
+
def build_and_save_vector_store():
|
15 |
+
return "Conhecimento atualizado com sucesso.", None, None
|
16 |
+
|
17 |
+
MODELS = {"Modelo Padrão": "gpt-3.5-turbo", "Mistral 7B": "mistral-7b", "Zephyr 7B": "zephyr-7b"}
|
18 |
+
DEFAULT_MODEL = "Modelo Padrão"
|
19 |
+
# --- Fim das funções de exemplo ---
|
20 |
+
|
21 |
+
|
22 |
+
# CSS corrigido para layout flexível e responsivo
|
23 |
+
css_corrigido = """
|
24 |
+
/* Reseta básico para garantir consistência */
|
25 |
+
html, body {
|
26 |
+
margin: 0;
|
27 |
+
padding: 0;
|
28 |
+
height: 100%;
|
29 |
+
width: 100%;
|
30 |
}
|
31 |
|
32 |
+
.gradio-container {
|
33 |
+
max-width: 1400px !important;
|
34 |
+
margin: 0 auto !important;
|
35 |
+
width: 100% !important;
|
|
|
|
|
36 |
}
|
37 |
|
38 |
+
/* Contêiner principal que ocupa a altura total da tela */
|
39 |
+
.view-port-container {
|
40 |
+
display: flex !important;
|
41 |
+
flex-direction: column !important;
|
42 |
+
height: 100vh !important; /* Ocupa 100% da altura da janela de visualização */
|
43 |
+
background-color: #f7fafc; /* Cor de fundo suave */
|
44 |
}
|
45 |
|
46 |
+
/* Estilos do Título */
|
47 |
+
.titulo-principal {
|
48 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
49 |
+
color: white !important;
|
50 |
padding: 20px !important;
|
51 |
+
text-align: center !important;
|
52 |
+
flex-shrink: 0; /* Impede que o título encolha */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
}
|
54 |
|
55 |
+
/* Contêiner do Chat - A MÁGICA ACONTECE AQUI */
|
56 |
.chat-container {
|
57 |
+
flex-grow: 1; /* Faz o chat ocupar todo o espaço vertical disponível */
|
58 |
+
overflow-y: auto; /* Adiciona scroll INTERNO se o conteúdo exceder */
|
59 |
+
padding: 10px;
|
60 |
+
min-height: 0; /* Correção para flexbox em alguns navegadores */
|
|
|
|
|
|
|
|
|
61 |
display: flex;
|
62 |
flex-direction: column;
|
|
|
|
|
|
|
|
|
|
|
63 |
}
|
64 |
|
65 |
+
#chat {
|
66 |
+
flex-grow: 1;
|
|
|
|
|
67 |
}
|
68 |
|
69 |
+
/* Contêiner da área de input */
|
70 |
+
.input-container {
|
71 |
+
padding: 15px !important;
|
72 |
+
flex-shrink: 0; /* Impede que a área de input encolha */
|
73 |
+
background-color: #ffffff;
|
74 |
+
border-top: 1px solid #e2e8f0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
}
|
76 |
|
77 |
#entrada_usuario textarea {
|
78 |
+
font-size: 16px !important;
|
79 |
+
line-height: 1.5 !important;
|
80 |
+
min-height: 50px !important;
|
|
|
|
|
|
|
81 |
}
|
82 |
|
83 |
+
/* Conteúdo adicional que fica abaixo e é rolável */
|
84 |
+
.scrollable-content {
|
85 |
+
padding: 20px;
|
86 |
+
background-color: #edf2f7;
|
87 |
}
|
88 |
|
89 |
+
/* Estilos adicionais para manter a aparência original */
|
90 |
+
.gr-textbox textarea {
|
91 |
+
font-size: 14px !important;
|
92 |
+
line-height: 1.5 !important;
|
93 |
+
}
|
94 |
+
.modelo-dropdown {
|
95 |
+
margin-bottom: 15px !important;
|
96 |
}
|
97 |
|
98 |
+
/* Responsividade */
|
99 |
@media (max-width: 768px) {
|
100 |
.titulo-principal {
|
|
|
101 |
padding: 15px !important;
|
102 |
+
font-size: 0.9em;
|
|
|
|
|
|
|
|
|
103 |
}
|
|
|
104 |
.input-container {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
padding: 10px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
}
|
107 |
}
|
108 |
"""
|
109 |
|
110 |
+
def criar_interface_corrigida():
|
111 |
+
with gr.Blocks(title="Dr. Aldo Henrique - API Externa", theme=gr.themes.Soft(), css=css_corrigido) as interface:
|
112 |
+
# Contêiner principal que ocupa toda a tela (viewport)
|
113 |
+
with gr.Column(elem_classes="view-port-container"):
|
114 |
+
# Cabeçalho (Título)
|
115 |
gr.HTML("""
|
116 |
<div class="titulo-principal">
|
117 |
<h4>🤖 iAldo - Pergunte e aprenda</h4>
|
118 |
+
<p style="font-size: 14px; opacity: 0.9; margin: 5px 0 0 0;">Converse com o <a href="https://aldohenrique.com.br/" style="color: white; text-decoration: underline;">Prof. Dr. Aldo Henrique</a></p>
|
119 |
</div>
|
120 |
""")
|
121 |
|
122 |
+
# Área do Chat (flexível)
|
123 |
with gr.Column(elem_classes="chat-container"):
|
124 |
chatbot = gr.Chatbot(
|
125 |
+
label="💬 Chat com Dr. Aldo",
|
126 |
elem_id="chat",
|
127 |
+
bubble_full_width=False,
|
128 |
+
height="100%" # Ocupa 100% do pai (.chat-container)
|
129 |
)
|
130 |
|
131 |
+
# Área de Entrada (fixa na parte inferior)
|
132 |
with gr.Column(elem_classes="input-container"):
|
133 |
+
modelo_select = gr.Dropdown(
|
134 |
+
choices=list(MODELS.keys()),
|
135 |
+
value=DEFAULT_MODEL,
|
136 |
+
label="🧠 Selecione o Modelo de IA",
|
137 |
+
info="Escolha o modelo para responder",
|
138 |
+
elem_classes="modelo-dropdown"
|
139 |
+
)
|
|
|
|
|
140 |
with gr.Row():
|
141 |
user_input = gr.Textbox(
|
142 |
show_label=False,
|
143 |
+
placeholder="Digite sua pergunta...",
|
144 |
lines=2,
|
145 |
+
elem_id="entrada_usuario",
|
146 |
+
scale=4
|
147 |
)
|
148 |
+
enviar_btn = gr.Button("Enviar", variant="primary", scale=1)
|
149 |
|
150 |
+
# Conteúdo adicional que pode ser rolado
|
151 |
+
with gr.Column(elem_classes="scrollable-content"):
|
|
|
152 |
with gr.Accordion("⚙️ Controle do Conhecimento (RAG)", open=False):
|
153 |
status_rag = gr.Textbox(label="Status do Retreino", interactive=False)
|
154 |
botao_retreinar = gr.Button("🔄 Atualizar Conhecimento do Blog", variant="stop")
|
155 |
+
download_faiss_file = gr.File(label="Download do Índice FAISS", interactive=False)
|
156 |
+
download_urls_file = gr.File(label="Download das URLs Processadas", interactive=False)
|
157 |
|
|
|
158 |
with gr.Accordion("📚 Exemplos de Perguntas", open=False):
|
159 |
gr.Examples(
|
160 |
examples=[
|
|
|
165 |
inputs=[user_input, modelo_select]
|
166 |
)
|
167 |
|
|
|
168 |
with gr.Accordion("🔧 Status da API", open=False):
|
169 |
status_api = gr.Textbox(label="Status dos Modelos", interactive=False, lines=8)
|
170 |
|
|
|
171 |
with gr.Accordion("ℹ️ Informações", open=False):
|
172 |
gr.Markdown("""
|
173 |
### Sobre o Dr. Aldo Henrique:
|
174 |
- **Especialidade**: Linguagens C, Java, Desenvolvimento Web, Inteligência Artificial
|
175 |
- **Conhecimento Adicional**: Conteúdo do blog aldohenrique.com.br
|
|
|
176 |
### Dicas para melhores respostas:
|
177 |
- Faça perguntas específicas sobre o conteúdo do blog para ver o RAG em ação!
|
178 |
- Peça resumos ou opiniões sobre temas que o professor aborda.
|
179 |
""")
|
180 |
|
181 |
+
# --- Lógica de Eventos (permanece a mesma) ---
|
182 |
def responder(chat_history, user_msg, modelo):
|
183 |
if not user_msg.strip():
|
184 |
return chat_history, ""
|
185 |
+
chat_history.append([user_msg, None])
|
186 |
+
yield chat_history, gr.update(value="", interactive=False)
|
187 |
+
|
|
|
|
|
|
|
188 |
resposta_final = responder_como_aldo(user_msg, modelo)
|
|
|
|
|
189 |
chat_history[-1][1] = resposta_final
|
190 |
+
yield chat_history, gr.update(interactive=True)
|
191 |
|
192 |
enviar_btn.click(
|
193 |
fn=responder,
|
194 |
inputs=[chatbot, user_input, modelo_select],
|
195 |
+
outputs=[chatbot, user_input]
|
|
|
196 |
)
|
|
|
197 |
user_input.submit(
|
198 |
fn=responder,
|
199 |
inputs=[chatbot, user_input, modelo_select],
|
200 |
+
outputs=[chatbot, user_input]
|
|
|
201 |
)
|
|
|
202 |
botao_retreinar.click(
|
203 |
fn=build_and_save_vector_store,
|
204 |
+
outputs=[status_rag, download_faiss_file, download_urls_file]
|
|
|
205 |
)
|
206 |
|
207 |
+
interface.load(_js="""
|
208 |
+
function () {
|
209 |
+
setTimeout(() => {
|
210 |
+
const textarea = document.querySelector("#entrada_usuario textarea");
|
211 |
+
if (textarea) {
|
212 |
+
textarea.focus();
|
213 |
+
}
|
214 |
+
}, 100);
|
215 |
+
}
|
|
|
216 |
""")
|
217 |
|
218 |
return interface
|
219 |
|
220 |
def configurar_interface():
|
221 |
+
return criar_interface_corrigida()
|
222 |
+
|
223 |
+
# Para rodar o app:
|
224 |
+
if __name__ == "__main__":
|
225 |
+
app = configurar_interface()
|
226 |
+
app.launch()
|