WaysAhead Global commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -381,36 +381,39 @@ agent_with_memory = initialize_agent(
|
|
| 381 |
verbose=True
|
| 382 |
)
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
@app.post("/ask")
|
| 385 |
-
async def ask(
|
| 386 |
-
|
| 387 |
-
question = payload.get("question", "").strip()
|
| 388 |
|
| 389 |
if not question:
|
| 390 |
return JSONResponse(status_code=400, content={"error": "Question cannot be empty."})
|
| 391 |
|
| 392 |
try:
|
| 393 |
-
# Run the
|
| 394 |
agent_result = agent_with_memory.invoke({"input": question})
|
| 395 |
raw_answer = agent_result.get("output", "") if isinstance(agent_result, dict) else str(agent_result)
|
| 396 |
|
| 397 |
-
# Clean
|
| 398 |
import re
|
| 399 |
cleaned_answer = re.sub(r"\[(.*?)\]\((https?://.*?)\)", r"\1", raw_answer)
|
| 400 |
|
| 401 |
-
#
|
| 402 |
video_results = fetch_youtube_videos.invoke({"query": question})
|
| 403 |
doc_sources = retrieve_documents.invoke({"query": question})
|
| 404 |
people_images = detect_people_and_images.invoke({"answer": cleaned_answer})
|
| 405 |
|
| 406 |
-
# Insights tab
|
| 407 |
insights = []
|
| 408 |
seen = set()
|
| 409 |
for p in people_images:
|
| 410 |
name = p["name"].lower().strip()
|
| 411 |
if name not in seen:
|
| 412 |
-
seen.add(name)
|
| 413 |
insights.append(f"{name} was detected in the answer and linked to visual/media content.")
|
|
|
|
| 414 |
|
| 415 |
if doc_sources:
|
| 416 |
insights.append(f"{len(doc_sources)} source documents contributed to this answer.")
|
|
@@ -429,4 +432,4 @@ async def ask(request: Request):
|
|
| 429 |
|
| 430 |
except Exception as e:
|
| 431 |
logging.error(f"Tool-based agent error: {e}")
|
| 432 |
-
return JSONResponse(status_code=500, content={"error": "Failed to process question."})
|
|
|
|
| 381 |
verbose=True
|
| 382 |
)
|
| 383 |
|
| 384 |
+
from pydantic import BaseModel
|
| 385 |
+
|
| 386 |
+
class AskRequest(BaseModel):
|
| 387 |
+
question: str
|
| 388 |
+
|
| 389 |
@app.post("/ask")
|
| 390 |
+
async def ask(payload: AskRequest):
|
| 391 |
+
question = payload.question.strip()
|
|
|
|
| 392 |
|
| 393 |
if not question:
|
| 394 |
return JSONResponse(status_code=400, content={"error": "Question cannot be empty."})
|
| 395 |
|
| 396 |
try:
|
| 397 |
+
# Run the agent
|
| 398 |
agent_result = agent_with_memory.invoke({"input": question})
|
| 399 |
raw_answer = agent_result.get("output", "") if isinstance(agent_result, dict) else str(agent_result)
|
| 400 |
|
| 401 |
+
# Clean answer (remove markdown links)
|
| 402 |
import re
|
| 403 |
cleaned_answer = re.sub(r"\[(.*?)\]\((https?://.*?)\)", r"\1", raw_answer)
|
| 404 |
|
| 405 |
+
# Call tools
|
| 406 |
video_results = fetch_youtube_videos.invoke({"query": question})
|
| 407 |
doc_sources = retrieve_documents.invoke({"query": question})
|
| 408 |
people_images = detect_people_and_images.invoke({"answer": cleaned_answer})
|
| 409 |
|
|
|
|
| 410 |
insights = []
|
| 411 |
seen = set()
|
| 412 |
for p in people_images:
|
| 413 |
name = p["name"].lower().strip()
|
| 414 |
if name not in seen:
|
|
|
|
| 415 |
insights.append(f"{name} was detected in the answer and linked to visual/media content.")
|
| 416 |
+
seen.add(name)
|
| 417 |
|
| 418 |
if doc_sources:
|
| 419 |
insights.append(f"{len(doc_sources)} source documents contributed to this answer.")
|
|
|
|
| 432 |
|
| 433 |
except Exception as e:
|
| 434 |
logging.error(f"Tool-based agent error: {e}")
|
| 435 |
+
return JSONResponse(status_code=500, content={"error": "Failed to process question."})
|