Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,25 +30,45 @@ def upload_files(files):
|
|
| 30 |
return gr.update(choices=list_uploaded_files(), value=None), "Upload feito!"
|
| 31 |
|
| 32 |
def build_index():
|
|
|
|
| 33 |
global STATE_INDEXED
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def chat_answer(history, message):
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
retriever.load()
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def summarize_run(filename, pages, chapter, query, style, length):
|
| 54 |
if not STATE_INDEXED:
|
|
|
|
| 30 |
return gr.update(choices=list_uploaded_files(), value=None), "Upload feito!"
|
| 31 |
|
| 32 |
def build_index():
|
| 33 |
+
"""Ingestão (parse) + construção e carga do índice. Mostra erro detalhado no UI."""
|
| 34 |
global STATE_INDEXED
|
| 35 |
+
try:
|
| 36 |
+
if not (settings.INDEX_DIR / "meta.jsonl").exists():
|
| 37 |
+
ingest_all() # extrai textos e cria meta.jsonl
|
| 38 |
+
retriever.build() # cria/atualiza FAISS
|
| 39 |
+
retriever.load() # carrega o índice em memória
|
| 40 |
+
STATE_INDEXED = True
|
| 41 |
+
return "Índice criado/carregado. Vá para **Conversar** ou **Resumir**."
|
| 42 |
+
except Exception as e:
|
| 43 |
+
STATE_INDEXED = False
|
| 44 |
+
return f"Falha ao indexar/carregar: **{type(e).__name__}** — {e}"
|
| 45 |
+
|
| 46 |
|
| 47 |
def chat_answer(history, message):
|
| 48 |
+
"""Carrega o índice se preciso e responde; se falhar, mostra o motivo em vez de 'ERRO!'."""
|
| 49 |
+
if not history:
|
| 50 |
+
history = []
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
if not STATE_INDEXED:
|
| 54 |
retriever.load()
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return history + [("system", f"Índice não está pronto: **{type(e).__name__}** — {e}\n"
|
| 57 |
+
"Vá na aba **Upload & Indexar** e clique em **Indexar**.")], ""
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
hits = retriever.search(message, top_k=settings.TOP_K)
|
| 62 |
+
if not hits:
|
| 63 |
+
ans = "Não encontrei trechos relevantes. Verifique se você **indexou** os arquivos."
|
| 64 |
+
else:
|
| 65 |
+
ctx = hits[:settings.TOP_K_RERANK]
|
| 66 |
+
ans = generate(message, ctx)
|
| 67 |
+
history = history + [(message, ans)]
|
| 68 |
+
return history, ""
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return history + [("system", f"Falha na busca/resposta: **{type(e).__name__}** — {e}")], ""
|
| 71 |
+
|
| 72 |
|
| 73 |
def summarize_run(filename, pages, chapter, query, style, length):
|
| 74 |
if not STATE_INDEXED:
|