Spaces:
Sleeping
Sleeping
Update app.py
#5
by
caarleexx
- opened
- app.py +70 -12
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,13 +1,9 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
-
import time
|
| 5 |
-
import hashlib
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 8 |
-
|
| 9 |
-
import gradio as gr
|
| 10 |
-
import google.generativeai as genai
|
| 11 |
|
| 12 |
# Dependências para PDF
|
| 13 |
try:
|
|
@@ -237,8 +233,8 @@ def automacao_upload_processamento(files, history, config_json):
|
|
| 237 |
|
| 238 |
try:
|
| 239 |
config_agentes = json.loads(config_json)
|
| 240 |
-
except:
|
| 241 |
-
|
| 242 |
|
| 243 |
if history is None:
|
| 244 |
history = []
|
|
@@ -477,7 +473,6 @@ Responda de forma concisa e direta, focando apenas na sua missão.
|
|
| 477 |
# Não exibe o conteúdo da resposta do modelo no chat, apenas a confirmação
|
| 478 |
novo_trecho = f"✅ **[{nome_agente}]** concluiu sua análise em ({duracao:.1f}s).\n"
|
| 479 |
history[-1][1] = msg_atual + novo_trecho
|
| 480 |
-
|
| 481 |
yield history, timeline_execucao, pipeline_state
|
| 482 |
|
| 483 |
except Exception as e:
|
|
@@ -566,5 +561,68 @@ def ui_v29_stop_logic():
|
|
| 566 |
|
| 567 |
return app
|
| 568 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
if __name__ == "__main__":
|
| 570 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, Form, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from typing import List
|
| 4 |
+
import uvicorn
|
| 5 |
import os
|
| 6 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Dependências para PDF
|
| 9 |
try:
|
|
|
|
| 233 |
|
| 234 |
try:
|
| 235 |
config_agentes = json.loads(config_json)
|
| 236 |
+
except json.JSONDecodeError:
|
| 237 |
+
raise HTTPException(status_code=400, detail="Configuração JSON inválida.")
|
| 238 |
|
| 239 |
if history is None:
|
| 240 |
history = []
|
|
|
|
| 473 |
# Não exibe o conteúdo da resposta do modelo no chat, apenas a confirmação
|
| 474 |
novo_trecho = f"✅ **[{nome_agente}]** concluiu sua análise em ({duracao:.1f}s).\n"
|
| 475 |
history[-1][1] = msg_atual + novo_trecho
|
|
|
|
| 476 |
yield history, timeline_execucao, pipeline_state
|
| 477 |
|
| 478 |
except Exception as e:
|
|
|
|
| 561 |
|
| 562 |
return app
|
| 563 |
|
| 564 |
+
# if __name__ == "__main__":
|
| 565 |
+
# ui_v29_stop_logic().launch()
|
| 566 |
+
from fastapi import FastAPI, UploadFile, Form
|
| 567 |
+
from fastapi.responses import JSONResponse
|
| 568 |
+
from typing import List
|
| 569 |
+
import uvicorn
|
| 570 |
+
|
| 571 |
+
# Criação da aplicação FastAPI
|
| 572 |
+
app = FastAPI(title="AI Forensics API", description="API para análise de casos de indenizações por morte ou invalidez.")
|
| 573 |
+
|
| 574 |
+
# Endpoint para upload e processamento de arquivos
|
| 575 |
+
@app.post("/upload/")
|
| 576 |
+
async def upload_files(files: List[UploadFile], config_json: str = Form(...)):
|
| 577 |
+
history = []
|
| 578 |
+
try:
|
| 579 |
+
config_agentes = json.loads(config_json)
|
| 580 |
+
except json.JSONDecodeError:
|
| 581 |
+
return JSONResponse(content={"error": "Configuração JSON inválida."}, status_code=400)
|
| 582 |
+
|
| 583 |
+
for file in files:
|
| 584 |
+
nome = file.filename
|
| 585 |
+
if nome.lower().endswith('.pdf'):
|
| 586 |
+
pdf_proc, erro = processar_pdf_completo(file.file)
|
| 587 |
+
if erro:
|
| 588 |
+
history.append({"file": nome, "status": "error", "message": erro})
|
| 589 |
+
else:
|
| 590 |
+
history.append({"file": nome, "status": "processed", "data": pdf_proc})
|
| 591 |
+
else:
|
| 592 |
+
res = ler_arquivo_texto(file.file)
|
| 593 |
+
if res:
|
| 594 |
+
history.append({"file": nome, "status": "processed", "data": res})
|
| 595 |
+
else:
|
| 596 |
+
history.append({"file": nome, "status": "error", "message": "Falha ao ler o arquivo."})
|
| 597 |
+
|
| 598 |
+
return history
|
| 599 |
+
|
| 600 |
+
# Endpoint para iniciar uma análise com base em uma mensagem
|
| 601 |
+
@app.post("/analyze/")
|
| 602 |
+
async def analyze_message(message: str, config_json: str):
|
| 603 |
+
try:
|
| 604 |
+
protocolo = json.loads(config_json)
|
| 605 |
+
except json.JSONDecodeError:
|
| 606 |
+
return JSONResponse(content={"error": "Configuração JSON inválida."}, status_code=400)
|
| 607 |
+
|
| 608 |
+
try:
|
| 609 |
+
prompt_contexto = gerenciador.gerar_prompt_com_transcricoes(message)
|
| 610 |
+
except Exception as e:
|
| 611 |
+
raise HTTPException(status_code=500, detail=f"Erro ao gerar contexto: {str(e)}")
|
| 612 |
+
|
| 613 |
+
timeline_execucao = [{"passo": 1, "tipo": "prompt_usuario", "conteudo": prompt_contexto}]
|
| 614 |
+
pipeline_state = {"is_paused": False, "timeline": timeline_execucao, "remaining_agents": protocolo}
|
| 615 |
+
|
| 616 |
+
history = []
|
| 617 |
+
for response in executar_pipeline(history, timeline_execucao, protocolo, pipeline_state):
|
| 618 |
+
pass # Processa os passos da pipeline
|
| 619 |
+
|
| 620 |
+
return {"history": history, "timeline": timeline_execucao}
|
| 621 |
+
|
| 622 |
+
# Endpoint para consultar o estado dos arquivos processados
|
| 623 |
+
@app.get("/files/")
|
| 624 |
+
async def list_files():
|
| 625 |
+
return gerenciador.arquivos
|
| 626 |
+
|
| 627 |
if __name__ == "__main__":
|
| 628 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
CHANGED
|
@@ -5,4 +5,6 @@ google-genai
|
|
| 5 |
pypdf
|
| 6 |
PyPDF2
|
| 7 |
pdf2image
|
| 8 |
-
pillow
|
|
|
|
|
|
|
|
|
| 5 |
pypdf
|
| 6 |
PyPDF2
|
| 7 |
pdf2image
|
| 8 |
+
pillow
|
| 9 |
+
fastapi
|
| 10 |
+
uvicorn
|