natalieac commited on
Commit
c17777d
·
verified ·
1 Parent(s): 177a39e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -15
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
- if not (settings.INDEX_DIR / "meta.jsonl").exists():
35
- ingest_all()
36
- retriever.build()
37
- retriever.load()
38
- STATE_INDEXED = True
39
- return "Vá para a aba Conversar :) "
 
 
 
 
 
40
 
41
  def chat_answer(history, message):
42
- if not STATE_INDEXED:
43
- try:
 
 
 
 
44
  retriever.load()
45
- except Exception:
46
- return history + [("system","ERRO!")], ""
47
- hits = retriever.search(message, top_k=settings.TOP_K)
48
- ctx = hits[:settings.TOP_K_RERANK]
49
- ans = generate(message, ctx)
50
- history = history + [(message, ans)]
51
- return history, ""
 
 
 
 
 
 
 
 
 
 
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: