Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -127,30 +127,27 @@ def ask_bot_alternative(question):
|
|
| 127 |
question = question[:500]
|
| 128 |
print(f"\nProcessing question: {question}")
|
| 129 |
|
| 130 |
-
# Retrieve
|
| 131 |
-
retriever = db.as_retriever(search_kwargs={"k":
|
| 132 |
context_docs = retriever.get_relevant_documents(question)
|
|
|
|
| 133 |
print("\n--- Retrieved Context ---")
|
| 134 |
for i, d in enumerate(context_docs, 1):
|
| 135 |
-
print(f"[Doc {i}] {d.page_content[:
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
if isinstance(response, dict):
|
| 140 |
-
answer = response.get("result") or response.get("answer") or str(response)
|
| 141 |
-
else:
|
| 142 |
-
answer = str(response)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
answer = answer[:1000] + "..."
|
| 147 |
|
| 148 |
-
return answer
|
| 149 |
|
| 150 |
except Exception as e:
|
| 151 |
print(f"Error in ask_bot_alternative: {e}")
|
| 152 |
return f"Sorry, I encountered an error: {str(e)[:200]}"
|
| 153 |
|
|
|
|
| 154 |
# -------------------------------
|
| 155 |
# 9. Gradio Interface
|
| 156 |
# -------------------------------
|
|
|
|
| 127 |
question = question[:500]
|
| 128 |
print(f"\nProcessing question: {question}")
|
| 129 |
|
| 130 |
+
# Retrieve top documents
|
| 131 |
+
retriever = db.as_retriever(search_kwargs={"k": 1}) # only fetch the best match
|
| 132 |
context_docs = retriever.get_relevant_documents(question)
|
| 133 |
+
|
| 134 |
print("\n--- Retrieved Context ---")
|
| 135 |
for i, d in enumerate(context_docs, 1):
|
| 136 |
+
print(f"[Doc {i}] {d.page_content[:300]}...\n")
|
| 137 |
|
| 138 |
+
if not context_docs:
|
| 139 |
+
return "I could not find an answer in the portfolio content."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Always return the first match
|
| 142 |
+
answer = context_docs[0].page_content.strip()
|
|
|
|
| 143 |
|
| 144 |
+
return answer
|
| 145 |
|
| 146 |
except Exception as e:
|
| 147 |
print(f"Error in ask_bot_alternative: {e}")
|
| 148 |
return f"Sorry, I encountered an error: {str(e)[:200]}"
|
| 149 |
|
| 150 |
+
|
| 151 |
# -------------------------------
|
| 152 |
# 9. Gradio Interface
|
| 153 |
# -------------------------------
|