Spaces:
Sleeping
Sleeping
Commit ·
9bbaf64
1
Parent(s): 13cae1e
updated app.py
Browse files
app.py
CHANGED
|
@@ -6,19 +6,19 @@ import os
|
|
| 6 |
from document_processor.file_handler import DocumentProcessor
|
| 7 |
from retriever.builder import RetrieverBuilder
|
| 8 |
from agents.workflow import AgentWorkflow
|
| 9 |
-
from config import constants
|
| 10 |
from utils.logging import logger
|
| 11 |
|
| 12 |
# -------------------------
|
| 13 |
# Utils
|
| 14 |
# -------------------------
|
| 15 |
-
def _get_file_hashes(uploaded_files: List) ->
|
| 16 |
"""Generate SHA-256 hashes for uploaded files."""
|
| 17 |
-
hashes =
|
| 18 |
for file in uploaded_files:
|
| 19 |
with open(file.name, "rb") as f:
|
| 20 |
-
hashes.
|
| 21 |
-
return
|
| 22 |
|
| 23 |
# -------------------------
|
| 24 |
# Main App
|
|
@@ -64,8 +64,8 @@ def main():
|
|
| 64 |
|
| 65 |
# Session state
|
| 66 |
session_state = gr.State({
|
| 67 |
-
"file_hashes":
|
| 68 |
-
"
|
| 69 |
})
|
| 70 |
|
| 71 |
# -------------------------
|
|
@@ -93,19 +93,23 @@ def main():
|
|
| 93 |
|
| 94 |
current_hashes = _get_file_hashes(uploaded_files)
|
| 95 |
|
| 96 |
-
|
|
|
|
| 97 |
logger.info("Processing new/changed documents...")
|
| 98 |
chunks = processor.process(uploaded_files)
|
| 99 |
retriever = retriever_builder.build_hybrid_retriever(chunks)
|
| 100 |
-
state
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
|
|
|
|
| 105 |
result = workflow.full_pipeline(
|
| 106 |
question=question_text,
|
| 107 |
-
retriever=
|
| 108 |
)
|
|
|
|
| 109 |
return result["draft_answer"], result["verification_report"], state
|
| 110 |
|
| 111 |
except Exception as e:
|
|
|
|
| 6 |
from document_processor.file_handler import DocumentProcessor
|
| 7 |
from retriever.builder import RetrieverBuilder
|
| 8 |
from agents.workflow import AgentWorkflow
|
| 9 |
+
from config import constants
|
| 10 |
from utils.logging import logger
|
| 11 |
|
| 12 |
# -------------------------
|
| 13 |
# Utils
|
| 14 |
# -------------------------
|
| 15 |
+
def _get_file_hashes(uploaded_files: List) -> List[str]:
|
| 16 |
"""Generate SHA-256 hashes for uploaded files."""
|
| 17 |
+
hashes = []
|
| 18 |
for file in uploaded_files:
|
| 19 |
with open(file.name, "rb") as f:
|
| 20 |
+
hashes.append(hashlib.sha256(f.read()).hexdigest())
|
| 21 |
+
return hashes
|
| 22 |
|
| 23 |
# -------------------------
|
| 24 |
# Main App
|
|
|
|
| 64 |
|
| 65 |
# Session state
|
| 66 |
session_state = gr.State({
|
| 67 |
+
"file_hashes": [],
|
| 68 |
+
"retriever_ready": False # track if retriever is ready
|
| 69 |
})
|
| 70 |
|
| 71 |
# -------------------------
|
|
|
|
| 93 |
|
| 94 |
current_hashes = _get_file_hashes(uploaded_files)
|
| 95 |
|
| 96 |
+
# Initialize retriever if not ready or files changed
|
| 97 |
+
if not state.get("retriever_ready", False) or current_hashes != state["file_hashes"]:
|
| 98 |
logger.info("Processing new/changed documents...")
|
| 99 |
chunks = processor.process(uploaded_files)
|
| 100 |
retriever = retriever_builder.build_hybrid_retriever(chunks)
|
| 101 |
+
state["file_hashes"] = current_hashes
|
| 102 |
+
state["retriever_ready"] = True
|
| 103 |
+
state["_retriever_internal"] = retriever # keep internal, not for output
|
| 104 |
+
else:
|
| 105 |
+
retriever = state["_retriever_internal"]
|
| 106 |
|
| 107 |
+
# Run workflow
|
| 108 |
result = workflow.full_pipeline(
|
| 109 |
question=question_text,
|
| 110 |
+
retriever=retriever
|
| 111 |
)
|
| 112 |
+
|
| 113 |
return result["draft_answer"], result["verification_report"], state
|
| 114 |
|
| 115 |
except Exception as e:
|