Mustafa-albakkar commited on
Commit
9fbd769
·
verified ·
1 Parent(s): db23983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -25
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, file: Optional[str] = None) -> str:
306
- import time
307
- start = time.time()
308
-
309
- prompt = SYSTEM_INSTRUCTIONS + "\n\n" + question_text
310
  try:
311
- # حماية استدعاء الـ agent (langchain / llama-cpp) من الوصول المتوازي
312
- with llama_lock:
313
-
314
- result = self.agent.invoke({"input": prompt})
315
- if isinstance(result, dict):
316
- output = result.get("output") or result.get("text") or str(result)
317
- else:
318
- output = getattr(result, "output", str(result))
319
- except Exception as e:
320
- logger.exception("Agent execution failed")
321
- output = f"AGENT_ERROR: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
 
323
- try:
324
- final_out = final_client.predict_text(output)
325
  except Exception as e:
326
- logger.exception("Final client failed")
327
- final_out = f"FINAL_AGENT_ERROR: {e}"
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"