Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
-
# β PIPELINE
|
| 3 |
-
# β 1ΒΊ:
|
| 4 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
|
| 6 |
import os
|
|
@@ -11,324 +11,251 @@ from datetime import datetime
|
|
| 11 |
import gradio as gr
|
| 12 |
import google.generativeai as genai
|
| 13 |
|
| 14 |
-
# ==================== 1. CONFIGURAΓΓO
|
| 15 |
-
api_key = os.getenv("GOOGLE_API_KEY")
|
| 16 |
if api_key:
|
| 17 |
genai.configure(api_key=api_key)
|
| 18 |
model_flash = genai.GenerativeModel("gemini-flash-latest")
|
| 19 |
model_pro = genai.GenerativeModel("gemini-pro-latest")
|
| 20 |
else:
|
| 21 |
model_flash = model_pro = None
|
| 22 |
-
print("β οΈ GOOGLE_API_KEY nΓ£o encontrada. Configure via export GOOGLE_API_KEY=...")
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# ==================== 2. UTILIDADES DE PERSISTΓNCIA ====================
|
| 27 |
def carregar_history():
|
| 28 |
-
"""Carrega histΓ³rico persistente"""
|
| 29 |
try:
|
| 30 |
with open(ARQUIVO_HISTORY, "r", encoding="utf-8") as f:
|
| 31 |
return json.load(f)
|
| 32 |
-
except:
|
| 33 |
return []
|
| 34 |
|
| 35 |
def salvar_history(history):
|
| 36 |
-
"""Salva histΓ³rico automaticamente"""
|
| 37 |
try:
|
| 38 |
with open(ARQUIVO_HISTORY, "w", encoding="utf-8") as f:
|
| 39 |
json.dump(history, f, ensure_ascii=False, indent=2)
|
| 40 |
return True
|
| 41 |
-
except:
|
| 42 |
return False
|
| 43 |
|
| 44 |
def ler_anexo(arquivo):
|
| 45 |
-
|
| 46 |
-
if not arquivo: return ""
|
| 47 |
try:
|
| 48 |
with open(arquivo.name, "r", encoding="utf-8") as f:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
except:
|
| 52 |
-
return "β Erro lendo anexo"
|
| 53 |
-
|
| 54 |
-
def exportar_chat(history):
|
| 55 |
-
"""Gera TXT para download"""
|
| 56 |
-
if not history: return ""
|
| 57 |
-
timestamp = datetime.now().strftime('%Y-%m-%d_%H%M%S')
|
| 58 |
-
chat_txt = f"MEHON v29 EXPORT - {timestamp}\n\n"
|
| 59 |
-
for i, msg in enumerate(history, 1):
|
| 60 |
-
role = "π€ USUΓRIO" if i % 2 == 1 else "π€ MEHON"
|
| 61 |
-
chat_txt += f"{role}:\n{msg}\n\n{'-'*80}\n\n"
|
| 62 |
-
return chat_txt
|
| 63 |
|
| 64 |
-
# ==================== 3.
|
| 65 |
-
def
|
| 66 |
-
"""
|
| 67 |
if not model_pro:
|
| 68 |
return None, "β Sem API_KEY"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
f"π€: {h[0][:200]}... | π€: {h[1][:200]}..."
|
| 75 |
-
for h in history_contexto[-5:]
|
| 76 |
-
])
|
| 77 |
|
| 78 |
-
prompt_planejador = f"""
|
| 79 |
-
|
| 80 |
-
CONTEXTO HISTΓRICO DO USUΓRIO:
|
| 81 |
{history_resumo}
|
| 82 |
|
| 83 |
-
INPUT ATUAL: {
|
| 84 |
-
|
| 85 |
-
---
|
| 86 |
|
| 87 |
-
CRIE PLANO
|
| 88 |
|
| 89 |
-
|
| 90 |
[
|
| 91 |
-
{{"nome": "
|
| 92 |
-
|
| 93 |
]
|
| 94 |
|
| 95 |
-
β
|
| 96 |
-
β’ DECOMPONHA: problema β anΓ‘lise β cenΓ‘rios β validaΓ§Γ£o β sΓntese
|
| 97 |
-
β’ IDENTIFIQUE ambiguidades/possibilidades do input
|
| 98 |
-
β’ PERSONALIZE com histΓ³rico do usuΓ‘rio
|
| 99 |
-
β’ ΓLTIMO agente SEMPRE gera saΓda FINAL pro usuΓ‘rio
|
| 100 |
-
β’ Evite resposta direta - USE CADEIA DE PENSAMENTO
|
| 101 |
-
|
| 102 |
-
EXEMPLO PLANO BOM:
|
| 103 |
-
[
|
| 104 |
-
{{"nome": "AnalisadorInput", "missao": "Identifique elementos principais, ambiguidades e contexto", "modelo": "flash", "tipo_saida": "json"}},
|
| 105 |
-
{{"nome": "ExploradorCenΓ‘rios", "missao": "Liste 3-5 cenΓ‘rios possΓveis com probabilidades", "modelo": "pro", "tipo_saida": "json"}},
|
| 106 |
-
{{"nome": "SintetizadorFinal", "missao": "Converja tudo em resposta ΓΊnica e acionΓ‘vel", "modelo": "pro", "tipo_saida": "texto"}}
|
| 107 |
-
]
|
| 108 |
-
|
| 109 |
-
APENAS JSON VΓLIDO - SEM TEXTO EXTRA!"""
|
| 110 |
|
| 111 |
try:
|
| 112 |
-
resp = model_pro.generate_content(prompt_planejador, temperature=0.
|
| 113 |
-
plano_raw = resp.text.strip().replace('``````','')
|
| 114 |
plano = json.loads(plano_raw)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
return
|
| 121 |
-
except Exception as e:
|
| 122 |
-
return None, f"β Erro plano: {str(e)[:100]}"
|
| 123 |
|
| 124 |
-
# ==================== 4.
|
| 125 |
-
def
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
return {"role": "system", "error": "Sem API"}, "(ERRO: API)", "Sem Google API Key"
|
| 129 |
-
|
| 130 |
-
modelo = model_pro if agente_config.get("modelo") == "pro" else model_flash
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
history_resumo = "\n".join([f"π€: {h[0][:100]}... | π€: {h[1][:100]}..." for h in history_contexto[-3:]])
|
| 135 |
|
| 136 |
-
prompt = f"""
|
| 137 |
-
{
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
{
|
| 141 |
|
| 142 |
-
|
| 143 |
-
MISSΓO ESPECΓFICA: {agente_config['missao']}
|
| 144 |
-
|
| 145 |
-
{ 'JSON estruturado e vΓ‘lido' if agente_config['tipo_saida']=='json' else 'Resposta clara, completa e finalizada pro usuΓ‘rio' }:"""
|
| 146 |
-
|
| 147 |
-
log = f"πΈ {agente_config['nome']} ({agente_config['modelo']})"
|
| 148 |
try:
|
| 149 |
inicio = time.time()
|
| 150 |
-
resp = modelo.generate_content(prompt
|
| 151 |
-
out = resp.text
|
| 152 |
|
| 153 |
-
# Parse output
|
| 154 |
-
content = json.loads(out.replace('``````','')) if agente_config['tipo_saida']=='json' else out
|
| 155 |
tempo = time.time() - inicio
|
|
|
|
| 156 |
|
| 157 |
-
log += f"
|
| 158 |
-
return {"role": "assistant", "agent":
|
| 159 |
-
|
| 160 |
except Exception as e:
|
| 161 |
-
|
| 162 |
-
return {"role": "system", "error": str(e)}, log, str(e)
|
| 163 |
|
| 164 |
-
# ==================== 5. ORQUESTRADOR
|
| 165 |
-
def
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
# Processa input
|
| 169 |
anexo = ler_anexo(arquivo)
|
| 170 |
-
full_input = f"{texto}{anexo}".strip()
|
| 171 |
|
| 172 |
if not full_input:
|
| 173 |
-
yield history,
|
| 174 |
return
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
history = history + [[
|
| 178 |
-
timeline = [{"role": "user", "content": full_input
|
| 179 |
-
logs = f"π
|
| 180 |
-
|
| 181 |
-
yield history, timeline, logs
|
| 182 |
|
| 183 |
-
|
| 184 |
-
history[-1][1] = "π― Planejador criando agentes sob medida..."
|
| 185 |
yield history, timeline, logs
|
| 186 |
|
| 187 |
-
|
|
|
|
| 188 |
if not plano:
|
| 189 |
-
history[-1][1] =
|
| 190 |
yield history, timeline, logs
|
| 191 |
return
|
| 192 |
|
| 193 |
logs += f"{log_plano}\n"
|
| 194 |
-
timeline.append({"role": "system", "
|
|
|
|
|
|
|
| 195 |
yield history, timeline, logs
|
| 196 |
|
| 197 |
-
#
|
| 198 |
-
|
| 199 |
-
for i,
|
| 200 |
-
history[-1][1] = f"βοΈ [{i+1}/{len(plano)}] {
|
| 201 |
yield history, timeline, logs
|
| 202 |
|
| 203 |
-
|
| 204 |
-
timeline.append(
|
| 205 |
-
logs +=
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
preview = resposta_acumulada[:900] + "..." if len(resposta_acumulada) > 900 else resposta_acumulada
|
| 211 |
-
history[-1][1] = preview
|
| 212 |
yield history, timeline, logs
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
salvar_history(history) # Auto-save
|
| 217 |
-
|
| 218 |
yield history, timeline, logs
|
| 219 |
|
| 220 |
-
# ==================== 6.
|
| 221 |
-
def
|
| 222 |
-
"""UI completa e funcional"""
|
| 223 |
css = """
|
| 224 |
-
footer {
|
| 225 |
-
.
|
| 226 |
-
.mehon-title { font-size: 1.6em; font-weight: bold; color: #1e40af; text-align: center; }
|
| 227 |
-
.status-bar { background: linear-gradient(90deg, #3b82f6, #1e40af); color: white; padding: 8px; border-radius: 8px; }
|
| 228 |
"""
|
| 229 |
|
| 230 |
-
|
|
|
|
| 231 |
|
| 232 |
-
with gr.Blocks(title="
|
| 233 |
-
|
| 234 |
-
# HEADER
|
| 235 |
-
gr.Markdown("### π§ PIPELINE MEHON v29 - RaciocΓnio Estruturado AutomΓ‘tico")
|
| 236 |
-
gr.Markdown("*O modelo planeja β executa β converge. Supera respostas probabilΓsticas diretas.*")
|
| 237 |
-
|
| 238 |
with gr.Tabs():
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
with gr.Tab("π RaciocΓnio MEHON", icon="π§ "):
|
| 242 |
chatbot = gr.Chatbot(
|
| 243 |
-
value=
|
| 244 |
-
|
| 245 |
-
show_copy_button=True,
|
| 246 |
show_label=False,
|
|
|
|
|
|
|
| 247 |
render_markdown=True
|
| 248 |
)
|
| 249 |
|
| 250 |
with gr.Row():
|
| 251 |
-
with gr.Column(scale=
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
lines=
|
|
|
|
| 256 |
container=False
|
| 257 |
)
|
| 258 |
-
with gr.Column(scale=1):
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
file_types=[".txt", ".
|
|
|
|
| 262 |
)
|
| 263 |
-
with gr.Column(scale=1):
|
| 264 |
-
|
| 265 |
|
| 266 |
-
|
|
|
|
| 267 |
|
| 268 |
-
# ABA 2:
|
| 269 |
-
with gr.Tab("
|
| 270 |
with gr.Row():
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
json_plano = gr.JSON(label="Agentes DinΓ’micos", lines=15)
|
| 274 |
-
with gr.Column(scale=1):
|
| 275 |
-
gr.Markdown("### π Timeline Completa")
|
| 276 |
-
timeline_json = gr.JSON(label="Cadeia de Pensamento", lines=15)
|
| 277 |
-
|
| 278 |
-
gr.Markdown("### π Logs de ExecuΓ§Γ£o")
|
| 279 |
-
logs_execucao = gr.Textbox(label="Runtime Logs", lines=12)
|
| 280 |
|
| 281 |
-
# ABA 3:
|
| 282 |
-
with gr.Tab("
|
| 283 |
-
gr.Markdown("**HistΓ³rico persistente em `history_v29.json`**")
|
| 284 |
with gr.Row():
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
btn_salvar_manual = gr.Button("πΎ Salvar Manual", variant="secondary")
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
for resultado in pipeline_mehon_completo(txt, file, hist):
|
| 296 |
-
yield resultado # [chatbot, timeline, logs]
|
| 297 |
-
|
| 298 |
-
# Trigger principal
|
| 299 |
-
btn_executar.click(
|
| 300 |
-
executar_pipeline_completo,
|
| 301 |
-
inputs=[txt_input, file_input, chatbot],
|
| 302 |
-
outputs=[chatbot, timeline_json, logs_execucao]
|
| 303 |
-
).then(
|
| 304 |
-
lambda: gr.update(value=""),
|
| 305 |
-
outputs=[txt_input]
|
| 306 |
-
)
|
| 307 |
-
|
| 308 |
-
txt_input.submit(
|
| 309 |
-
executar_pipeline_completo,
|
| 310 |
-
inputs=[txt_input, file_input, chatbot],
|
| 311 |
-
outputs=[chatbot, timeline_json, logs_execucao]
|
| 312 |
-
)
|
| 313 |
|
| 314 |
-
#
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
return app
|
| 320 |
|
| 321 |
-
# ==================== LAUNCH ====================
|
| 322 |
if __name__ == "__main__":
|
| 323 |
-
print("π
|
| 324 |
-
print("π‘
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
app = criar_app_mehon_v29()
|
| 328 |
-
app.launch(
|
| 329 |
-
server_name="0.0.0.0",
|
| 330 |
-
server_port=7861,
|
| 331 |
-
share=False,
|
| 332 |
-
show_error=True,
|
| 333 |
-
quiet=False
|
| 334 |
-
)
|
|
|
|
| 1 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
+
# β PIPELINE v30: AUTO-PLAN | MESMA UI v27 | COMPATΓVEL HF SPACES β
|
| 3 |
+
# β 1ΒΊ: MODELO CRIA PLANO β ITERA PELO PRΓPRIO PLANO | SEM 'icon' β
|
| 4 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 11 |
import gradio as gr
|
| 12 |
import google.generativeai as genai
|
| 13 |
|
| 14 |
+
# ==================== 1. CONFIGURAΓΓO ====================
|
| 15 |
+
api_key = os.getenv("GOOGLE_API_KEY", "SUA_API_KEY_AQUI")
|
| 16 |
if api_key:
|
| 17 |
genai.configure(api_key=api_key)
|
| 18 |
model_flash = genai.GenerativeModel("gemini-flash-latest")
|
| 19 |
model_pro = genai.GenerativeModel("gemini-pro-latest")
|
| 20 |
else:
|
| 21 |
model_flash = model_pro = None
|
|
|
|
| 22 |
|
| 23 |
+
ARQUIVO_CONFIG = "protocolo.json"
|
| 24 |
+
ARQUIVO_HISTORY = "history_v30.json"
|
| 25 |
+
|
| 26 |
+
# ==================== 2. UTILIDADES (IDΓNTICAS AO v27) ====================
|
| 27 |
+
def carregar_protocolo():
|
| 28 |
+
try:
|
| 29 |
+
with open(ARQUIVO_CONFIG, "r", encoding="utf-8") as f:
|
| 30 |
+
return f.read()
|
| 31 |
+
except:
|
| 32 |
+
return "[]"
|
| 33 |
+
|
| 34 |
+
def salvar_protocolo(conteudo):
|
| 35 |
+
try:
|
| 36 |
+
json.loads(conteudo)
|
| 37 |
+
with open(ARQUIVO_CONFIG, "w", encoding="utf-8") as f:
|
| 38 |
+
f.write(conteudo)
|
| 39 |
+
return "β
Salvo"
|
| 40 |
+
except:
|
| 41 |
+
return "β Erro JSON"
|
| 42 |
|
|
|
|
| 43 |
def carregar_history():
|
|
|
|
| 44 |
try:
|
| 45 |
with open(ARQUIVO_HISTORY, "r", encoding="utf-8") as f:
|
| 46 |
return json.load(f)
|
| 47 |
+
except:
|
| 48 |
return []
|
| 49 |
|
| 50 |
def salvar_history(history):
|
|
|
|
| 51 |
try:
|
| 52 |
with open(ARQUIVO_HISTORY, "w", encoding="utf-8") as f:
|
| 53 |
json.dump(history, f, ensure_ascii=False, indent=2)
|
| 54 |
return True
|
| 55 |
+
except:
|
| 56 |
return False
|
| 57 |
|
| 58 |
def ler_anexo(arquivo):
|
| 59 |
+
if arquivo is None: return ""
|
|
|
|
| 60 |
try:
|
| 61 |
with open(arquivo.name, "r", encoding="utf-8") as f:
|
| 62 |
+
return f"\n\n[ANEXO: {os.path.basename(arquivo.name)}]\n{f.read()}\n[FIM ANEXO]\n"
|
| 63 |
+
except: return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# ==================== 3. PLANEJADOR AUTO (1Βͺ AΓΓO) ====================
|
| 66 |
+
def criar_plano_auto(full_input, history_contexto):
|
| 67 |
+
"""MODELO CRIA SEU PRΓPRIO PLANO dinamicamente"""
|
| 68 |
if not model_pro:
|
| 69 |
return None, "β Sem API_KEY"
|
| 70 |
|
| 71 |
+
history_resumo = "\n".join([
|
| 72 |
+
f"π€: {h[0][:150]}... | π€: {h[1][:150]}..."
|
| 73 |
+
for h in history_contexto[-3:]
|
| 74 |
+
][:300]) if history_contexto else ""
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
prompt_planejador = f"""CONTEXTO HISTΓRICO:
|
|
|
|
|
|
|
| 77 |
{history_resumo}
|
| 78 |
|
| 79 |
+
INPUT ATUAL: {full_input}
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
CRIE PLANO OTIMIZADO de 3-5 agentes:
|
| 82 |
|
| 83 |
+
FORMATO JSON VΓLIDO:
|
| 84 |
[
|
| 85 |
+
{{"nome": "AnalisadorInput", "missao": "Identifique elementos principais", "modelo": "flash", "tipo_saida": "json"}},
|
| 86 |
+
{{"nome": "ExecutorFinal", "missao": "Produza resposta final completa", "modelo": "pro", "tipo_saida": "texto"}}
|
| 87 |
]
|
| 88 |
|
| 89 |
+
β
ΓLTIMO agente = saΓda FINAL pro usuΓ‘rio"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
resp = model_pro.generate_content(prompt_planejador, temperature=0.2)
|
| 93 |
+
plano_raw = resp.text.strip().replace('``````','')
|
| 94 |
plano = json.loads(plano_raw)
|
| 95 |
|
| 96 |
+
if isinstance(plano, list) and len(plano) >= 2:
|
| 97 |
+
return plano, f"β
Plano: {len(plano)} agentes"
|
| 98 |
+
return None, "β Plano invΓ‘lido"
|
| 99 |
+
except:
|
| 100 |
+
return None, "β Erro plano"
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# ==================== 4. ENGINE EXECUΓΓO (ORIGINAL v27) ====================
|
| 103 |
+
def executar_no(timeline, config):
|
| 104 |
+
if not (model_flash or model_pro):
|
| 105 |
+
return {"role": "system", "error": "Sem API"}, "(ERRO: API)", "Sem key"
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
modelo = model_pro if config.get("modelo") == "pro" else model_flash
|
| 108 |
+
contexto = json.dumps(timeline[-10:], ensure_ascii=False, indent=2)
|
|
|
|
| 109 |
|
| 110 |
+
prompt = f"""--- TIMELINE ---
|
| 111 |
+
{contexto}
|
| 112 |
+
----------------
|
| 113 |
+
AGENTE: {config['nome']}
|
| 114 |
+
MISSΓO: {config['missao']}"""
|
| 115 |
|
| 116 |
+
log = f"\nπΈ {config['nome']}..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
try:
|
| 118 |
inicio = time.time()
|
| 119 |
+
resp = modelo.generate_content(prompt)
|
| 120 |
+
out = resp.text
|
| 121 |
|
|
|
|
|
|
|
| 122 |
tempo = time.time() - inicio
|
| 123 |
+
content = json.loads(out.strip().replace('``````','')) if config['tipo_saida']=='json' else out
|
| 124 |
|
| 125 |
+
log += f" (OK - {tempo:.2f}s)"
|
| 126 |
+
return {"role": "assistant", "agent": config['nome'], "content": content}, log, out
|
|
|
|
| 127 |
except Exception as e:
|
| 128 |
+
return {"role": "system", "error": str(e)}, f" (ERRO: {e})", str(e)
|
|
|
|
| 129 |
|
| 130 |
+
# ==================== 5. ORQUESTRADOR v30 (PLANO AUTO) ====================
|
| 131 |
+
def orquestrador(texto, arquivo, history, json_config):
|
| 132 |
+
# 1. Input Check (IDΓNTICO v27)
|
|
|
|
|
|
|
| 133 |
anexo = ler_anexo(arquivo)
|
| 134 |
+
full_input = f"{texto}\n{anexo}".strip()
|
| 135 |
|
| 136 |
if not full_input:
|
| 137 |
+
yield history, {}, "Sem input."
|
| 138 |
return
|
| 139 |
|
| 140 |
+
# 2. Setup (IDΓNTICO v27)
|
| 141 |
+
history = history + [[texto + (" π" if arquivo else ""), None]]
|
| 142 |
+
timeline = [{"role": "user", "content": full_input}]
|
| 143 |
+
logs = f"π AUTO-PLAN v30: {datetime.now().strftime('%H:%M:%S')}\n"
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
history[-1][1] = "π― Criando plano dinΓ’mico..."
|
|
|
|
| 146 |
yield history, timeline, logs
|
| 147 |
|
| 148 |
+
# 3. 1Βͺ AΓΓO: CRIA PLANO
|
| 149 |
+
plano, log_plano = criar_plano_auto(full_input, history)
|
| 150 |
if not plano:
|
| 151 |
+
history[-1][1] = log_plano
|
| 152 |
yield history, timeline, logs
|
| 153 |
return
|
| 154 |
|
| 155 |
logs += f"{log_plano}\n"
|
| 156 |
+
timeline.append({"role": "system", "plano_auto": plano})
|
| 157 |
+
|
| 158 |
+
history[-1][1] = f"β
Plano criado: {len(plano)} agentes"
|
| 159 |
yield history, timeline, logs
|
| 160 |
|
| 161 |
+
# 4. ITERA PELO PLANO (IDΓNTICO v27)
|
| 162 |
+
final_response = ""
|
| 163 |
+
for i, cfg in enumerate(plano):
|
| 164 |
+
history[-1][1] = f"βοΈ [{i+1}/{len(plano)}] {cfg['nome']}..."
|
| 165 |
yield history, timeline, logs
|
| 166 |
|
| 167 |
+
res, log_add, raw = executar_no(timeline, cfg)
|
| 168 |
+
timeline.append(res)
|
| 169 |
+
logs += log_add + "\n"
|
| 170 |
|
| 171 |
+
if cfg['tipo_saida'] == 'texto':
|
| 172 |
+
final_response = res['content']
|
| 173 |
+
history[-1][1] = final_response[:800] + "..." if len(final_response) > 800 else final_response
|
|
|
|
|
|
|
| 174 |
yield history, timeline, logs
|
| 175 |
|
| 176 |
+
logs += "β
AUTO-PLAN concluΓdo."
|
| 177 |
+
salvar_history(history)
|
|
|
|
|
|
|
| 178 |
yield history, timeline, logs
|
| 179 |
|
| 180 |
+
# ==================== 6. UI LIMPA v27 (SEM 'icon') ====================
|
| 181 |
+
def ui_clean():
|
|
|
|
| 182 |
css = """
|
| 183 |
+
footer {display: none !important;}
|
| 184 |
+
.contain {border: none !important;}
|
|
|
|
|
|
|
| 185 |
"""
|
| 186 |
|
| 187 |
+
config_init = carregar_protocolo()
|
| 188 |
+
history_init = carregar_history()
|
| 189 |
|
| 190 |
+
with gr.Blocks(title="PIPELINE v30 - AUTO-PLAN", css=css, theme=gr.themes.Soft()) as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
with gr.Tabs():
|
| 192 |
+
# === ABA 1: CHAT (EXATA v27) ===
|
| 193 |
+
with gr.Tab("π¬ Pipeline"):
|
|
|
|
| 194 |
chatbot = gr.Chatbot(
|
| 195 |
+
value=history_init,
|
| 196 |
+
label="",
|
|
|
|
| 197 |
show_label=False,
|
| 198 |
+
height=600,
|
| 199 |
+
show_copy_button=True,
|
| 200 |
render_markdown=True
|
| 201 |
)
|
| 202 |
|
| 203 |
with gr.Row():
|
| 204 |
+
with gr.Column(scale=10):
|
| 205 |
+
txt_in = gr.Textbox(
|
| 206 |
+
show_label=False,
|
| 207 |
+
placeholder="Digite input... Modelo criarΓ‘ plano automaticamente!",
|
| 208 |
+
lines=1,
|
| 209 |
+
max_lines=5,
|
| 210 |
container=False
|
| 211 |
)
|
| 212 |
+
with gr.Column(scale=1, min_width=50):
|
| 213 |
+
file_in = gr.UploadButton(
|
| 214 |
+
"π",
|
| 215 |
+
file_types=[".txt", ".md", ".csv", ".json", ".py", ".yaml"],
|
| 216 |
+
size="sm"
|
| 217 |
)
|
| 218 |
+
with gr.Column(scale=1, min_width=80):
|
| 219 |
+
btn_send = gr.Button("Enviar", variant="primary", size="sm")
|
| 220 |
|
| 221 |
+
file_status = gr.Markdown("", visible=True)
|
| 222 |
+
file_in.upload(lambda x: f"π Anexo: {os.path.basename(x.name)}", file_in, file_status)
|
| 223 |
|
| 224 |
+
# === ABA 2: DEPURAΓΓO (EXATA v27) ===
|
| 225 |
+
with gr.Tab("π΅οΈ DepuraΓ§Γ£o"):
|
| 226 |
with gr.Row():
|
| 227 |
+
out_dna = gr.JSON(label="DNA (Timeline)")
|
| 228 |
+
out_logs = gr.Textbox(label="Logs do Sistema", lines=20)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
# === ABA 3: CONFIG (EXATA v27) ===
|
| 231 |
+
with gr.Tab("βοΈ Config"):
|
|
|
|
| 232 |
with gr.Row():
|
| 233 |
+
btn_save = gr.Button("Salvar Config")
|
| 234 |
+
lbl_save = gr.Label(show_label=False)
|
|
|
|
| 235 |
|
| 236 |
+
code_json = gr.Code(
|
| 237 |
+
value=config_init,
|
| 238 |
+
language="json",
|
| 239 |
+
label="protocolo.json (placeholder - usa auto-plan)"
|
| 240 |
+
)
|
| 241 |
+
btn_save.click(salvar_protocolo, code_json, lbl_save)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
# === TRIGGERS (EXATOS v27) ===
|
| 244 |
+
triggers = [btn_send.click, txt_in.submit]
|
| 245 |
+
for trig in triggers:
|
| 246 |
+
trig(
|
| 247 |
+
orquestrador,
|
| 248 |
+
inputs=[txt_in, file_in, chatbot, code_json],
|
| 249 |
+
outputs=[chatbot, out_dna, out_logs]
|
| 250 |
+
).then(
|
| 251 |
+
lambda: (None, ""),
|
| 252 |
+
outputs=[txt_in, file_status]
|
| 253 |
+
)
|
| 254 |
|
| 255 |
return app
|
| 256 |
|
|
|
|
| 257 |
if __name__ == "__main__":
|
| 258 |
+
print("π PIPELINE v30 - AUTO-PLAN (HF Spaces compatΓvel)")
|
| 259 |
+
print("π‘ Sem 'icon' | History persistente | Mesmo UI v27")
|
| 260 |
+
app = ui_clean()
|
| 261 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|