Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -302,34 +302,83 @@ class GaiaRunner:
|
|
| 302 |
self.agent = agent_executor
|
| 303 |
self.username = username
|
| 304 |
|
| 305 |
-
def run_on_question(self, question_text: str,
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
try:
|
| 311 |
-
#
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
-
try:
|
| 324 |
-
final_out = final_client.predict_text(output)
|
| 325 |
except Exception as e:
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
end = time.time()
|
| 330 |
-
logger.info(f"⏱️ Time for question: {end - start:.2f} sec")
|
| 331 |
-
|
| 332 |
-
return final_out
|
| 333 |
|
| 334 |
def run_all_and_submit(self) -> Dict[str, Any]:
|
| 335 |
questions_url =f"{GAIA_API_BASE}/questions"
|
|
|
|
| 302 |
self.agent = agent_executor
|
| 303 |
self.username = username
|
| 304 |
|
| 305 |
+
def run_on_question(self, question_text: str, file_path: Optional[str] = None) -> str:
|
| 306 |
+
"""
|
| 307 |
+
تشغيل الوكيل الرئيسي (ReAct agent) على سؤال واحد مع إمكانية وجود مرفق.
|
| 308 |
+
يجمع كل خطوات التفكير (Thought → Action → Observation) ويرسلها كوحدة واحدة إلى وكيل الإجابة النهائية.
|
| 309 |
+
"""
|
| 310 |
try:
|
| 311 |
+
# ==========================
|
| 312 |
+
# 1️⃣ إعداد الدخل للنموذج
|
| 313 |
+
# ==========================
|
| 314 |
+
SYSTEM_INSTRUCTIONS = (
|
| 315 |
+
"You are a reasoning agent that follows the ReAct pattern (Thought, Action, Observation). "
|
| 316 |
+
"Use available tools if necessary, and finish with 'Final Answer: ... <<END>>'"
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
prompt = SYSTEM_INSTRUCTIONS + "\n\nQuestion:\n" + question_text
|
| 320 |
+
if file_path:
|
| 321 |
+
prompt += f"\n[Attached file path: {file_path}]"
|
| 322 |
+
|
| 323 |
+
print(f"\n🚀 Running ReAct agent on question:\n{question_text}")
|
| 324 |
+
if file_path:
|
| 325 |
+
print(f"📎 With attachment: {file_path}")
|
| 326 |
+
|
| 327 |
+
# ==========================
|
| 328 |
+
# 2️⃣ تنفيذ الوكيل الرئيسي
|
| 329 |
+
# ==========================
|
| 330 |
+
result = self.agent.invoke({"input": prompt})
|
| 331 |
+
|
| 332 |
+
# ==========================
|
| 333 |
+
# 3️⃣ بناء سجل التفكير الكامل
|
| 334 |
+
# ==========================
|
| 335 |
+
full_log = []
|
| 336 |
+
full_log.append(f"Question: {question_text}\n")
|
| 337 |
+
|
| 338 |
+
# إذا كان هناك مرفق
|
| 339 |
+
if file_path:
|
| 340 |
+
full_log.append(f"Attachment: {file_path}\n")
|
| 341 |
+
|
| 342 |
+
# إضافة جميع خطوات ReAct الداخلية
|
| 343 |
+
if "intermediate_steps" in result:
|
| 344 |
+
for step in result["intermediate_steps"]:
|
| 345 |
+
try:
|
| 346 |
+
action, observation = step
|
| 347 |
+
full_log.append(
|
| 348 |
+
f"Action: {action.tool}\n"
|
| 349 |
+
f"Action Input: {action.tool_input}\n"
|
| 350 |
+
f"Observation: {observation}\n"
|
| 351 |
+
)
|
| 352 |
+
except Exception as e:
|
| 353 |
+
print(f"[⚠️] Failed to parse intermediate step: {e}")
|
| 354 |
+
|
| 355 |
+
# إضافة الإجابة النهائية
|
| 356 |
+
output = result.get("output", "")
|
| 357 |
+
full_log.append(f"Final Answer: {output}\n")
|
| 358 |
+
|
| 359 |
+
# دمج كل السجل في نص واحد
|
| 360 |
+
conversation_log = "\n".join(full_log)
|
| 361 |
+
|
| 362 |
+
print("\n🧠 === FULL REACT LOG ===")
|
| 363 |
+
print(conversation_log)
|
| 364 |
+
print("==========================")
|
| 365 |
+
|
| 366 |
+
# ==========================
|
| 367 |
+
# 4️⃣ إرسال السجل الكامل إلى وكيل الإجابة النهائية
|
| 368 |
+
# ==========================
|
| 369 |
+
final_out = None
|
| 370 |
+
try:
|
| 371 |
+
final_out = final_client.predict_text(conversation_log)
|
| 372 |
+
print(f"✅ Final Answer Agent Output: {final_out}")
|
| 373 |
+
except Exception as e:
|
| 374 |
+
print(f"[⚠️] Failed to contact Final Answer Agent: {e}")
|
| 375 |
+
final_out = output # fallback إلى الناتج المحلي إن فشل
|
| 376 |
+
|
| 377 |
+
return final_out or output
|
| 378 |
|
|
|
|
|
|
|
| 379 |
except Exception as e:
|
| 380 |
+
print(f"[❌] Error while running on question: {e}")
|
| 381 |
+
return "Error: unable to process this question."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
def run_all_and_submit(self) -> Dict[str, Any]:
|
| 384 |
questions_url =f"{GAIA_API_BASE}/questions"
|