Spaces:
Running
Running
Update interface.py
Browse files- interface.py +96 -250
interface.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from ai_logic import (
|
| 3 |
responder_como_aldo,
|
| 4 |
build_and_save_vector_store,
|
|
@@ -7,277 +6,124 @@ from ai_logic import (
|
|
| 7 |
DEFAULT_MODEL
|
| 8 |
)
|
| 9 |
|
| 10 |
-
# CSS personalizado
|
| 11 |
css_customizado = """
|
| 12 |
-
.gradio-container {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
/* Estilo do chat */
|
| 20 |
-
.chat-container {
|
| 21 |
-
display: flex;
|
| 22 |
-
flex-direction: column;
|
| 23 |
-
height: 70vh;
|
| 24 |
-
background: #f9f9f9;
|
| 25 |
-
border-radius: 12px;
|
| 26 |
-
padding: 20px;
|
| 27 |
-
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
.chat-messages {
|
| 31 |
-
flex: 1;
|
| 32 |
-
overflow-y: auto;
|
| 33 |
-
padding: 15px;
|
| 34 |
-
margin-bottom: 20px;
|
| 35 |
-
background: white;
|
| 36 |
-
border-radius: 8px;
|
| 37 |
-
border: 1px solid #e0e0e0;
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
.message {
|
| 41 |
-
padding: 12px 16px;
|
| 42 |
-
margin-bottom: 12px;
|
| 43 |
-
border-radius: 18px;
|
| 44 |
-
max-width: 80%;
|
| 45 |
-
line-height: 1.4;
|
| 46 |
-
position: relative;
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
.user-message {
|
| 50 |
-
background: #0078d4;
|
| 51 |
-
color: white;
|
| 52 |
-
margin-left: auto;
|
| 53 |
-
border-bottom-right-radius: 4px;
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
.bot-message {
|
| 57 |
-
background: #f1f1f1;
|
| 58 |
-
color: #333;
|
| 59 |
-
margin-right: auto;
|
| 60 |
-
border-bottom-left-radius: 4px;
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
.message-header {
|
| 64 |
-
font-weight: bold;
|
| 65 |
-
margin-bottom: 4px;
|
| 66 |
-
font-size: 0.9em;
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
.user-message .message-header {
|
| 70 |
-
color: #e6f2ff;
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
.bot-message .message-header {
|
| 74 |
-
color: #666;
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
/* Área de entrada */
|
| 78 |
-
.input-area {
|
| 79 |
-
display: flex;
|
| 80 |
-
gap: 10px;
|
| 81 |
-
align-items: center;
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
.textbox {
|
| 85 |
-
flex: 1;
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
.send-button {
|
| 89 |
-
height: 40px !important;
|
| 90 |
-
min-width: 100px !important;
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
.model-selector {
|
| 94 |
-
margin-top: 15px;
|
| 95 |
-
background: white;
|
| 96 |
-
padding: 15px;
|
| 97 |
-
border-radius: 8px;
|
| 98 |
-
border: 1px solid #e0e0e0;
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
/* Cabeçalho */
|
| 102 |
-
.header {
|
| 103 |
-
text-align: center;
|
| 104 |
-
margin-bottom: 20px;
|
| 105 |
-
padding: 15px;
|
| 106 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 107 |
-
color: white;
|
| 108 |
-
border-radius: 10px;
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
/* Componentes de controle */
|
| 112 |
-
.control-section {
|
| 113 |
-
margin-top: 20px;
|
| 114 |
-
background: white;
|
| 115 |
-
padding: 15px;
|
| 116 |
-
border-radius: 8px;
|
| 117 |
-
border: 1px solid #e0e0e0;
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
/* Scrollbar personalizada */
|
| 121 |
-
::-webkit-scrollbar {
|
| 122 |
-
width: 8px;
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
::-webkit-scrollbar-track {
|
| 126 |
-
background: #f1f1f1;
|
| 127 |
-
border-radius: 10px;
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
::-webkit-scrollbar-thumb {
|
| 131 |
-
background: #c1c1c1;
|
| 132 |
-
border-radius: 10px;
|
| 133 |
-
}
|
| 134 |
-
|
| 135 |
-
::-webkit-scrollbar-thumb:hover {
|
| 136 |
-
background: #a8a8a8;
|
| 137 |
-
}
|
| 138 |
"""
|
| 139 |
|
| 140 |
-
def format_message(text, is_user=False, model_name=None):
|
| 141 |
-
"""Formata a mensagem para exibição no chat"""
|
| 142 |
-
role = "Você" if is_user else "Dr. Aldo"
|
| 143 |
-
model_info = f"<small>(Modelo: {model_name})</small>" if model_name and not is_user else ""
|
| 144 |
-
return f"""
|
| 145 |
-
<div class="message {'user-message' if is_user else 'bot-message'}">
|
| 146 |
-
<div class="message-header">{role} {model_info}</div>
|
| 147 |
-
{text}
|
| 148 |
-
</div>
|
| 149 |
-
"""
|
| 150 |
-
|
| 151 |
def criar_interface():
|
| 152 |
-
"""Cria e retorna a interface Gradio
|
| 153 |
|
| 154 |
-
with gr.Blocks(title="
|
| 155 |
-
# Estado para armazenar o histórico do chat
|
| 156 |
-
chat_history = gr.State([])
|
| 157 |
-
|
| 158 |
# Cabeçalho
|
| 159 |
gr.HTML("""
|
| 160 |
-
<div class="
|
| 161 |
-
<h1>🤖
|
| 162 |
-
<p>Conhecimento enriquecido com o conteúdo do <a href="https://aldohenrique.com.br/" style="color: white; text-decoration: underline;">Blog do Prof. Dr. Aldo Henrique</a></p>
|
| 163 |
</div>
|
| 164 |
""")
|
| 165 |
|
| 166 |
-
#
|
| 167 |
-
with gr.
|
| 168 |
-
#
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
user_input = gr.Textbox(
|
| 177 |
-
placeholder="Digite sua mensagem...",
|
| 178 |
-
show_label=False,
|
| 179 |
-
container=False,
|
| 180 |
-
elem_classes="textbox"
|
| 181 |
)
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
value=DEFAULT_MODEL,
|
| 189 |
-
label="🧠 Modelo de IA",
|
| 190 |
-
info="Selecione o modelo para gerar respostas"
|
| 191 |
)
|
| 192 |
-
|
| 193 |
-
# Seção de controle (colapsada por padrão)
|
| 194 |
-
with gr.Accordion("⚙️ Controles e Configurações", open=False):
|
| 195 |
-
with gr.Column(elem_classes="control-section"):
|
| 196 |
-
# Botão para limpar o chat
|
| 197 |
-
clear_button = gr.Button("🧹 Limpar Conversa")
|
| 198 |
-
|
| 199 |
-
# Controle do RAG
|
| 200 |
-
with gr.Accordion("🔄 Atualizar Conhecimento (RAG)", open=False):
|
| 201 |
-
status_rag = gr.Textbox(label="Status do Retreino", interactive=False)
|
| 202 |
-
retrain_button = gr.Button("Atualizar Conhecimento do Blog", variant="stop")
|
| 203 |
-
download_faiss = gr.File(label="Download do Índice FAISS", interactive=False)
|
| 204 |
-
download_urls = gr.File(label="Download das URLs Processadas", interactive=False)
|
| 205 |
-
|
| 206 |
-
# Status e informações
|
| 207 |
-
with gr.Accordion("ℹ️ Informações e Status", open=False):
|
| 208 |
-
gr.Markdown("""
|
| 209 |
-
### Sobre o Dr. Aldo Henrique:
|
| 210 |
-
- **Especialidade**: Linguagens C, Java, Desenvolvimento Web, Inteligência Artificial
|
| 211 |
-
- **Conhecimento Adicional**: Conteúdo do blog aldohenrique.com.br
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
#
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
#
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
inputs=[user_input, chat_history, model_select],
|
| 241 |
-
outputs=[chat_display, chat_history],
|
| 242 |
-
show_progress=True
|
| 243 |
-
).then(
|
| 244 |
-
lambda: "", # Limpa a caixa de entrada
|
| 245 |
-
outputs=user_input
|
| 246 |
-
)
|
| 247 |
|
| 248 |
-
#
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
show_progress=True
|
| 254 |
-
).then(
|
| 255 |
-
lambda: "", # Limpa a caixa de entrada
|
| 256 |
-
outputs=user_input
|
| 257 |
)
|
| 258 |
|
| 259 |
-
#
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
|
| 264 |
-
fn=clear_chat,
|
| 265 |
-
outputs=[chat_display, chat_history]
|
| 266 |
-
)
|
| 267 |
-
|
| 268 |
-
# Evento de atualizar conhecimento
|
| 269 |
-
retrain_button.click(
|
| 270 |
fn=build_and_save_vector_store,
|
| 271 |
-
outputs=[status_rag,
|
| 272 |
show_progress=True
|
| 273 |
)
|
| 274 |
-
|
| 275 |
-
# Evento de teste de modelos (opcional)
|
| 276 |
-
# test_button.click(
|
| 277 |
-
# fn=testar_todos_modelos,
|
| 278 |
-
# outputs=api_status,
|
| 279 |
-
# show_progress=True
|
| 280 |
-
# )
|
| 281 |
|
| 282 |
return interface
|
| 283 |
|
|
|
|
|
|
|
| 1 |
from ai_logic import (
|
| 2 |
responder_como_aldo,
|
| 3 |
build_and_save_vector_store,
|
|
|
|
| 6 |
DEFAULT_MODEL
|
| 7 |
)
|
| 8 |
|
| 9 |
+
# CSS personalizado
|
| 10 |
css_customizado = """
|
| 11 |
+
.gradio-container { max-width: 1400px !important; margin: 0 auto; width: 99%; }
|
| 12 |
+
.gr-textbox textarea { font-size: 14px !important; line-height: 1.5 !important; }
|
| 13 |
+
.resposta-container { background-color: #ffffff !important; color: #1a1a1a !important; border: 1px solid #e0e0e0 !important; border-radius: 20px !important; padding: 20px !important; margin: 20px 0 !important; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important; }
|
| 14 |
+
.resposta-container pre code { color: #1a1a1a !important; background-color: #f8f9fa !important; }
|
| 15 |
+
.pergunta-container { background-color: #f0f8ff !important; border-radius: 8px !important; padding: 15px !important; }
|
| 16 |
+
.titulo-principal { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; color: white !important; padding: 20px !important; border-radius: 10px !important; margin-bottom: 20px !important; text-align: center !important; }
|
| 17 |
+
.modelo-dropdown { margin-bottom: 15px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def criar_interface():
|
| 21 |
+
"""Cria e retorna a interface Gradio"""
|
| 22 |
|
| 23 |
+
with gr.Blocks(title="Dr. Aldo Henrique - API Externa", theme=gr.themes.Soft(), css=css_customizado) as interface:
|
|
|
|
|
|
|
|
|
|
| 24 |
# Cabeçalho
|
| 25 |
gr.HTML("""
|
| 26 |
+
<div class="titulo-principal">
|
| 27 |
+
<h1>🤖 Dr. Aldo Henrique - Foco em TI com diferentes modelos de IA </h1>
|
| 28 |
+
<p style="font-size: 14px; opacity: 0.9;">Conhecimento enriquecido com o conteúdo do <a href="https://aldohenrique.com.br/" style="color: white; text-decoration: underline;">Blog do Prof. Dr. Aldo Henrique</a></p>
|
| 29 |
</div>
|
| 30 |
""")
|
| 31 |
|
| 32 |
+
# Interface principal
|
| 33 |
+
with gr.Row():
|
| 34 |
+
# Coluna de entrada
|
| 35 |
+
with gr.Column(scale=2):
|
| 36 |
+
gr.Markdown("### 📝 Faça sua pergunta:")
|
| 37 |
+
entrada = gr.Textbox(
|
| 38 |
+
label="",
|
| 39 |
+
placeholder="Digite sua pergunta aqui.",
|
| 40 |
+
lines=6,
|
| 41 |
+
elem_classes="pergunta-container"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
+
modelo_select = gr.Dropdown(
|
| 44 |
+
choices=list(MODELS.keys()),
|
| 45 |
+
value=DEFAULT_MODEL,
|
| 46 |
+
label="🧠 Selecione o Modelo de IA",
|
| 47 |
+
info="Escolha o modelo para responder",
|
| 48 |
+
elem_classes="modelo-dropdown"
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
with gr.Row():
|
| 52 |
+
botao_perguntar = gr.Button("🤔 Perguntar ao Dr. Aldo", variant="primary", size="lg")
|
| 53 |
+
#botao_testar = gr.Button("🔍 Testar Modelos", variant="secondary")
|
| 54 |
+
|
| 55 |
+
# Coluna de saída
|
| 56 |
+
with gr.Column(scale=3):
|
| 57 |
+
gr.Markdown("### 💬 Resposta do Dr. Aldo Henrique:")
|
| 58 |
+
saida = gr.HTML(
|
| 59 |
+
label="",
|
| 60 |
+
value="<div style='padding: 20px; text-align: center; color: #1a1a1a;'>Aguardando sua pergunta...</div>",
|
| 61 |
+
elem_classes="resposta-container"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# --- SEÇÃO PARA CONTROLE DO RAG ---
|
| 65 |
+
with gr.Accordion("⚙️ Controle do Conhecimento (RAG)", open=False):
|
| 66 |
+
status_rag = gr.Textbox(label="Status do Retreino", interactive=False)
|
| 67 |
+
botao_retreinar = gr.Button("🔄 Atualizar Conhecimento do Blog", variant="stop")
|
| 68 |
+
# Componentes para download
|
| 69 |
+
download_faiss_file = gr.File(
|
| 70 |
+
label="Download do Índice FAISS",
|
| 71 |
+
interactive=False,
|
| 72 |
+
file_count="single",
|
| 73 |
+
file_types=[".pkl"]
|
| 74 |
+
)
|
| 75 |
+
download_urls_file = gr.File(
|
| 76 |
+
label="Download das URLs Processadas",
|
| 77 |
+
interactive=False,
|
| 78 |
+
file_count="single",
|
| 79 |
+
file_types=[".pkl"]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Exemplos
|
| 83 |
+
with gr.Accordion("📚 Exemplos de Perguntas", open=False):
|
| 84 |
+
gr.Examples(
|
| 85 |
+
examples=[
|
| 86 |
+
["Como implementar uma lista ligada em C com todas as operações básicas?", DEFAULT_MODEL],
|
| 87 |
+
["Qual a sua opinião sobre o uso de ponteiros em C++ moderno, baseada no seu blog?", "Mistral 7B"],
|
| 88 |
+
["Resuma o que você escreveu sobre machine learning no seu blog.", "Zephyr 7B"],
|
| 89 |
+
],
|
| 90 |
+
inputs=[entrada, modelo_select]
|
| 91 |
+
)
|
| 92 |
|
| 93 |
+
# Status da API
|
| 94 |
+
with gr.Accordion("🔧 Status da API", open=False):
|
| 95 |
+
status_api = gr.Textbox(label="Status dos Modelos", interactive=False, lines=8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# Informações
|
| 98 |
+
with gr.Accordion("ℹ️ Informações", open=False):
|
| 99 |
+
gr.Markdown("""
|
| 100 |
+
### Sobre o Dr. Aldo Henrique:
|
| 101 |
+
- **Especialidade**: Linguagens C, Java, Desenvolvimento Web, Inteligência Artificial
|
| 102 |
+
- **Conhecimento Adicional**: Conteúdo do blog aldohenrique.com.br
|
| 103 |
+
### Dicas para melhores respostas:
|
| 104 |
+
- Faça perguntas específicas sobre o conteúdo do blog para ver o RAG em ação!
|
| 105 |
+
- Peça resumos ou opiniões sobre temas que o professor aborda.
|
| 106 |
+
""")
|
| 107 |
+
|
| 108 |
+
# Configuração dos eventos
|
| 109 |
+
botao_perguntar.click(
|
| 110 |
+
fn=responder_como_aldo,
|
| 111 |
+
inputs=[entrada, modelo_select],
|
| 112 |
+
outputs=saida,
|
| 113 |
show_progress=True
|
|
|
|
|
|
|
|
|
|
| 114 |
)
|
| 115 |
|
| 116 |
+
#botao_testar.click(
|
| 117 |
+
# fn=testar_todos_modelos,
|
| 118 |
+
# outputs=status_api,
|
| 119 |
+
# show_progress=True
|
| 120 |
+
#)
|
| 121 |
|
| 122 |
+
botao_retreinar.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
fn=build_and_save_vector_store,
|
| 124 |
+
outputs=[status_rag, download_faiss_file, download_urls_file],
|
| 125 |
show_progress=True
|
| 126 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
return interface
|
| 129 |
|