Spaces:
Sleeping
Sleeping
modified graph.py
Browse files
graph.py
CHANGED
|
@@ -7,26 +7,38 @@ from transformers import pipeline
|
|
| 7 |
# 🧠 Retrieval Node
|
| 8 |
def retrieve(state: Dict[str, Any]) -> Dict[str, Any]:
|
| 9 |
query = state["query"]
|
| 10 |
-
retriever = state
|
| 11 |
top_k = state.get("top_k", 3)
|
| 12 |
summarizer = state.get("summarizer")
|
| 13 |
-
raw_text = state.get("raw_text")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# ---
|
| 19 |
-
if
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
context = "\n\n".join([doc.page_content[:500] for doc in retrieved_docs])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# Summarize if context too
|
| 30 |
if len(context) > 1500 and summarizer:
|
| 31 |
context = summarizer(
|
| 32 |
context,
|
|
@@ -42,7 +54,7 @@ def retrieve(state: Dict[str, Any]) -> Dict[str, Any]:
|
|
| 42 |
"context": context,
|
| 43 |
"llm": state["llm"],
|
| 44 |
"temperature": state.get("temperature", 0.7),
|
| 45 |
-
"raw_text": raw_text
|
| 46 |
}
|
| 47 |
|
| 48 |
# 🧠 Generation Node
|
|
|
|
| 7 |
# 🧠 Retrieval Node
|
| 8 |
def retrieve(state: Dict[str, Any]) -> Dict[str, Any]:
|
| 9 |
query = state["query"]
|
| 10 |
+
retriever = state.get("retriever") # optional
|
| 11 |
top_k = state.get("top_k", 3)
|
| 12 |
summarizer = state.get("summarizer")
|
| 13 |
+
raw_text = state.get("raw_text") # uploaded file
|
| 14 |
+
|
| 15 |
+
retrieved_docs = []
|
| 16 |
+
context = ""
|
| 17 |
+
|
| 18 |
+
# --- Use FAISS retriever if available ---
|
| 19 |
+
if retriever:
|
| 20 |
+
try:
|
| 21 |
+
retrieved_docs = retriever.invoke(query, k=top_k)
|
| 22 |
+
except Exception:
|
| 23 |
+
retrieved_docs = []
|
| 24 |
+
|
| 25 |
+
# --- Determine context ---
|
| 26 |
+
if retrieved_docs:
|
| 27 |
+
# Limit each chunk to 500 chars
|
| 28 |
context = "\n\n".join([doc.page_content[:500] for doc in retrieved_docs])
|
| 29 |
+
elif raw_text:
|
| 30 |
+
# Use uploaded file directly if no retrieved docs
|
| 31 |
+
context = raw_text
|
| 32 |
+
else:
|
| 33 |
+
# Fallback to sample.txt
|
| 34 |
+
sample_path = "data/sample.txt"
|
| 35 |
+
if os.path.exists(sample_path):
|
| 36 |
+
with open(sample_path, "r", encoding="utf-8") as f:
|
| 37 |
+
context = f.read().strip()
|
| 38 |
+
else:
|
| 39 |
+
context = "No content available."
|
| 40 |
|
| 41 |
+
# --- Summarize if context too long ---
|
| 42 |
if len(context) > 1500 and summarizer:
|
| 43 |
context = summarizer(
|
| 44 |
context,
|
|
|
|
| 54 |
"context": context,
|
| 55 |
"llm": state["llm"],
|
| 56 |
"temperature": state.get("temperature", 0.7),
|
| 57 |
+
"raw_text": raw_text
|
| 58 |
}
|
| 59 |
|
| 60 |
# 🧠 Generation Node
|