Spaces:
Running
Running
Update interface.py
Browse files- interface.py +130 -62
interface.py
CHANGED
|
@@ -8,65 +8,77 @@ from ai_logic import (
|
|
| 8 |
)
|
| 9 |
|
| 10 |
css_customizado = """
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
margin: 0 auto;
|
| 21 |
-
height: 100vh;
|
| 22 |
display: flex;
|
| 23 |
flex-direction: column;
|
|
|
|
| 24 |
}
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
display: flex;
|
| 29 |
-
flex-direction: column;
|
| 30 |
-
justify-content: space-between;
|
| 31 |
-
overflow: hidden;
|
| 32 |
-
padding: 10px;
|
| 33 |
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
margin
|
| 41 |
-
|
| 42 |
}
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
overflow-y: auto;
|
| 47 |
-
margin-bottom: 10px;
|
| 48 |
}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
-
|
| 54 |
#entrada_usuario textarea {
|
| 55 |
-
color: white !important;
|
| 56 |
-
font-size: x-large !important;
|
| 57 |
-
background-color: #1a1a1a !important;
|
| 58 |
}
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
color:
|
| 63 |
-
background-color: #1a1a1a !important;
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
def criar_interface():
|
| 68 |
with gr.Blocks(title="Dr. Aldo Henrique - API Externa", theme=gr.themes.Soft(), css=css_customizado) as interface:
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
gr.HTML("""
|
| 71 |
<div class="titulo-principal">
|
| 72 |
<h1>🤖 iAldo - Pergunte e aprenda</h1>
|
|
@@ -74,39 +86,98 @@ def criar_interface():
|
|
| 74 |
</div>
|
| 75 |
""")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
chatbot = gr.Chatbot(label="💬 Chat com Dr. Aldo", elem_id="chat")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
user_input = gr.Textbox(
|
| 90 |
show_label=False,
|
| 91 |
placeholder="Digite sua pergunta e pressione Enter ou clique em Enviar",
|
| 92 |
-
lines=
|
| 93 |
elem_id="entrada_usuario"
|
| 94 |
)
|
| 95 |
enviar_btn = gr.Button("Enviar", variant="primary")
|
| 96 |
|
| 97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def responder(chat_history, user_msg, modelo):
|
| 99 |
if not user_msg.strip():
|
| 100 |
return chat_history, ""
|
|
|
|
|
|
|
| 101 |
chat_history = chat_history + [[user_msg, "Dr. Aldo Henrique está digitando..."]]
|
| 102 |
yield chat_history, ""
|
|
|
|
|
|
|
| 103 |
resposta_final = responder_como_aldo(user_msg, modelo)
|
|
|
|
|
|
|
| 104 |
chat_history[-1][1] = resposta_final
|
| 105 |
yield chat_history, ""
|
| 106 |
|
| 107 |
-
enviar_btn.click(
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
gr.HTML("""
|
| 111 |
<script>
|
| 112 |
window.addEventListener("load", function() {
|
|
@@ -121,7 +192,4 @@ def criar_interface():
|
|
| 121 |
return interface
|
| 122 |
|
| 123 |
def configurar_interface():
|
| 124 |
-
return criar_interface()
|
| 125 |
-
|
| 126 |
-
if __name__ == "__main__":
|
| 127 |
-
configurar_interface().launch()
|
|
|
|
| 8 |
)
|
| 9 |
|
| 10 |
css_customizado = """
|
| 11 |
+
.gradio-container {
|
| 12 |
+
max-width: 1400px !important;
|
| 13 |
+
margin: 0 auto;
|
| 14 |
+
width: 99%;
|
| 15 |
+
height: 100vh !important; /* Define a altura total da janela */
|
| 16 |
+
display: flex;
|
| 17 |
+
flex-direction: column;
|
| 18 |
}
|
| 19 |
+
.main-content {
|
| 20 |
+
flex: 1; /* Faz o conteúdo principal ocupar o espaço disponível */
|
| 21 |
+
overflow-y: auto; /* Adiciona rolagem vertical se necessário */
|
|
|
|
|
|
|
| 22 |
display: flex;
|
| 23 |
flex-direction: column;
|
| 24 |
+
min-height: 0; /* Evita problemas de altura em flex containers */
|
| 25 |
}
|
| 26 |
+
.gr-textbox textarea {
|
| 27 |
+
font-size: 14px !important;
|
| 28 |
+
line-height: 1.5 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
+
.resposta-container {
|
| 31 |
+
background-color: #ffffff !important;
|
| 32 |
+
color: #1a1a1a !important;
|
| 33 |
+
border: 1px solid #e0e0e0 !important;
|
| 34 |
+
border-radius: 20px !important;
|
| 35 |
+
padding: 20px !important;
|
| 36 |
+
margin: 20px 0 !important;
|
| 37 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important;
|
| 38 |
}
|
| 39 |
+
.resposta-container pre code {
|
| 40 |
+
color: #1a1a1a !important;
|
| 41 |
+
background-color: #f8f9fa !important;
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
+
.pergunta-container {
|
| 44 |
+
background-color: #f0f8ff !important;
|
| 45 |
+
border-radius: 8px !important;
|
| 46 |
+
padding: 15px !important;
|
| 47 |
+
}
|
| 48 |
+
.titulo-principal {
|
| 49 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 50 |
+
color: white !important;
|
| 51 |
+
padding: 20px !important; /* Ajustado para padding consistente */
|
| 52 |
+
border-radius: 10px !important;
|
| 53 |
+
margin-bottom: 10px !important;
|
| 54 |
+
text-align: center !important;
|
| 55 |
+
}
|
| 56 |
+
.modelo-dropdown {
|
| 57 |
+
margin-bottom: 15px !important;
|
| 58 |
+
}
|
| 59 |
+
.unequal-height.svelte-1xp0cw7 {
|
| 60 |
+
align-items: normal !important;
|
| 61 |
+
}
|
| 62 |
+
div.svelte-1xp0cw7 {
|
| 63 |
+
display: grid !important;
|
| 64 |
}
|
|
|
|
| 65 |
#entrada_usuario textarea {
|
| 66 |
+
color: white !important;
|
| 67 |
+
font-size: x-large !important;
|
| 68 |
+
background-color: #1a1a1a !important;
|
| 69 |
}
|
| 70 |
+
.message-content {
|
| 71 |
+
font-size: larger;
|
| 72 |
+
color: white !important;
|
| 73 |
+
background-color: #1a1a1a !important;
|
|
|
|
| 74 |
}
|
| 75 |
"""
|
| 76 |
|
| 77 |
def criar_interface():
|
| 78 |
with gr.Blocks(title="Dr. Aldo Henrique - API Externa", theme=gr.themes.Soft(), css=css_customizado) as interface:
|
| 79 |
+
# Contêiner principal para o conteúdo visível
|
| 80 |
+
with gr.Column(elem_classes="main-content"):
|
| 81 |
+
# Cabeçalho
|
| 82 |
gr.HTML("""
|
| 83 |
<div class="titulo-principal">
|
| 84 |
<h1>🤖 iAldo - Pergunte e aprenda</h1>
|
|
|
|
| 86 |
</div>
|
| 87 |
""")
|
| 88 |
|
| 89 |
+
chatbot = gr.Chatbot(label="💬 Chat com Dr. Aldo", elem_id="chat")
|
|
|
|
| 90 |
|
| 91 |
+
with gr.Row():
|
| 92 |
+
modelo_select = gr.Dropdown(
|
| 93 |
+
choices=list(MODELS.keys()),
|
| 94 |
+
value=DEFAULT_MODEL,
|
| 95 |
+
label="🧠 Selecione o Modelo de IA",
|
| 96 |
+
info="Escolha o modelo para responder",
|
| 97 |
+
elem_classes="modelo-dropdown"
|
| 98 |
+
)
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
user_input = gr.Textbox(
|
| 102 |
show_label=False,
|
| 103 |
placeholder="Digite sua pergunta e pressione Enter ou clique em Enviar",
|
| 104 |
+
lines=5,
|
| 105 |
elem_id="entrada_usuario"
|
| 106 |
)
|
| 107 |
enviar_btn = gr.Button("Enviar", variant="primary")
|
| 108 |
|
| 109 |
+
# Elementos fora da área visível inicial (acordions abaixo do botão Enviar)
|
| 110 |
+
with gr.Column():
|
| 111 |
+
# RAG
|
| 112 |
+
with gr.Accordion("⚙️ Controle do Conhecimento (RAG)", open=False):
|
| 113 |
+
status_rag = gr.Textbox(label="Status do Retreino", interactive=False)
|
| 114 |
+
botao_retreinar = gr.Button("🔄 Atualizar Conhecimento do Blog", variant="stop")
|
| 115 |
+
download_faiss_file = gr.File(label="Download do Índice FAISS", interactive=False, file_count="single", file_types=[".pkl"])
|
| 116 |
+
download_urls_file = gr.File(label="Download das URLs Processadas", interactive=False, file_count="single", file_types=[".pkl"])
|
| 117 |
+
|
| 118 |
+
# Exemplos
|
| 119 |
+
with gr.Accordion("📚 Exemplos de Perguntas", open=False):
|
| 120 |
+
gr.Examples(
|
| 121 |
+
examples=[
|
| 122 |
+
["Como implementar uma lista ligada em C com todas as operações básicas?", DEFAULT_MODEL],
|
| 123 |
+
["Qual a sua opinião sobre o uso de ponteiros em C++ moderno, baseada no seu blog?", "Mistral 7B"],
|
| 124 |
+
["Resuma o que você escreveu sobre machine learning no seu blog.", "Zephyr 7B"],
|
| 125 |
+
],
|
| 126 |
+
inputs=[user_input, modelo_select]
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Status API
|
| 130 |
+
with gr.Accordion("🔧 Status da API", open=False):
|
| 131 |
+
status_api = gr.Textbox(label="Status dos Modelos", interactive=False, lines=8)
|
| 132 |
+
|
| 133 |
+
# Informações
|
| 134 |
+
with gr.Accordion("ℹ️ Informações", open=False):
|
| 135 |
+
gr.Markdown("""
|
| 136 |
+
### Sobre o Dr. Aldo Henrique:
|
| 137 |
+
- **Especialidade**: Linguagens C, Java, Desenvolvimento Web, Inteligência Artificial
|
| 138 |
+
- **Conhecimento Adicional**: Conteúdo do blog aldohenrique.com.br
|
| 139 |
+
|
| 140 |
+
### Dicas para melhores respostas:
|
| 141 |
+
- Faça perguntas específicas sobre o conteúdo do blog para ver o RAG em ação!
|
| 142 |
+
- Peça resumos ou opiniões sobre temas que o professor aborda.
|
| 143 |
+
""")
|
| 144 |
+
|
| 145 |
def responder(chat_history, user_msg, modelo):
|
| 146 |
if not user_msg.strip():
|
| 147 |
return chat_history, ""
|
| 148 |
+
|
| 149 |
+
# Adiciona a mensagem do usuário e uma resposta temporária
|
| 150 |
chat_history = chat_history + [[user_msg, "Dr. Aldo Henrique está digitando..."]]
|
| 151 |
yield chat_history, ""
|
| 152 |
+
|
| 153 |
+
# Gera a resposta real
|
| 154 |
resposta_final = responder_como_aldo(user_msg, modelo)
|
| 155 |
+
|
| 156 |
+
# Atualiza com a resposta final
|
| 157 |
chat_history[-1][1] = resposta_final
|
| 158 |
yield chat_history, ""
|
| 159 |
|
| 160 |
+
enviar_btn.click(
|
| 161 |
+
fn=responder,
|
| 162 |
+
inputs=[chatbot, user_input, modelo_select],
|
| 163 |
+
outputs=[chatbot, user_input],
|
| 164 |
+
show_progress=True
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
user_input.submit(
|
| 168 |
+
fn=responder,
|
| 169 |
+
inputs=[chatbot, user_input, modelo_select],
|
| 170 |
+
outputs=[chatbot, user_input],
|
| 171 |
+
show_progress=True
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
botao_retreinar.click(
|
| 175 |
+
fn=build_and_save_vector_store,
|
| 176 |
+
outputs=[status_rag, download_faiss_file, download_urls_file],
|
| 177 |
+
show_progress=True
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Foco automático no campo de texto ao abrir
|
| 181 |
gr.HTML("""
|
| 182 |
<script>
|
| 183 |
window.addEventListener("load", function() {
|
|
|
|
| 192 |
return interface
|
| 193 |
|
| 194 |
def configurar_interface():
|
| 195 |
+
return criar_interface()
|
|
|
|
|
|
|
|
|