MasterOfHugs commited on
Commit
6d34e9e
·
verified ·
1 Parent(s): ddf54b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -39,7 +39,7 @@ class ReasoningAgent:
39
  )
40
 
41
  def __call__(self, question: str) -> str:
42
- print(f"Agent received question: {question}")
43
 
44
  # Prompt structuré
45
  prompt = f"""
@@ -59,34 +59,40 @@ Question: {question}
59
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
60
  output = model.generate(**inputs, max_new_tokens=300)
61
  text_output = tokenizer.decode(output[0], skip_special_tokens=True)
62
- print(f"Raw model output: {text_output[:300]}...") # pour debug
63
 
64
  # Parser le JSON
65
  try:
66
  parsed = json.loads(text_output)
67
- action = parsed.get("action", "None")
68
- observation = ""
69
- if action.startswith("AddTwoNumbers"):
70
- # extraire les deux nombres
71
- try:
72
- numbers = action[action.find("(")+1:action.find(")")].split(",")
73
- a, b = int(numbers[0].strip()), int(numbers[1].strip())
74
- observation = AddTwoNumbers(a, b)
75
- parsed["observation"] = str(observation)
76
- print(f"Performed tool action: {action} = {observation}")
77
- except Exception as e:
78
- observation = f"Error parsing numbers: {e}"
79
- parsed["observation"] = observation
80
-
81
- final_answer = parsed.get("answer", "No answer returned.")
82
- reasoning = parsed.get("thought", "")
83
- except json.JSONDecodeError:
84
- final_answer = text_output
85
- reasoning = ""
86
-
87
- print(f"Thought: {reasoning}")
88
- print(f"Answer: {final_answer}")
89
- return final_answer
 
 
 
 
 
 
90
 
91
  # --- Main Evaluation & Submission Function ---
92
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
39
  )
40
 
41
  def __call__(self, question: str) -> str:
42
+ print(f"\n=== New Question ===\n{question}\n")
43
 
44
  # Prompt structuré
45
  prompt = f"""
 
59
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
60
  output = model.generate(**inputs, max_new_tokens=300)
61
  text_output = tokenizer.decode(output[0], skip_special_tokens=True)
62
+ print(f"Raw model output:\n{text_output[:1000]}...\n") # on affiche un gros morceau du texte
63
 
64
  # Parser le JSON
65
  try:
66
  parsed = json.loads(text_output)
67
+ except json.JSONDecodeError as e:
68
+ print(f"⚠️ JSON decode error: {e}")
69
+ parsed = {"thought": "", "action": "None", "observation": "", "answer": text_output}
70
+
71
+ thought = parsed.get("thought", "")
72
+ action = parsed.get("action", "None")
73
+ observation = parsed.get("observation", "")
74
+ answer = parsed.get("answer", "No answer returned.")
75
+
76
+ # Exécution de l'outil si nécessaire
77
+ if action.startswith("AddTwoNumbers"):
78
+ try:
79
+ numbers = action[action.find("(")+1:action.find(")")].split(",")
80
+ a, b = int(numbers[0].strip()), int(numbers[1].strip())
81
+ observation = AddTwoNumbers(a, b)
82
+ parsed["observation"] = str(observation)
83
+ print(f"✅ Tool executed: {action} -> {observation}")
84
+ except Exception as e:
85
+ observation = f"⚠️ Error executing tool: {e}"
86
+ parsed["observation"] = observation
87
+ print(observation)
88
+
89
+ # Print du raisonnement actuel
90
+ print(f"💭 Thought: {thought}")
91
+ print(f"🔧 Action: {action}")
92
+ print(f"👀 Observation: {observation}")
93
+ print(f"📝 Answer: {answer}\n{'-'*50}\n")
94
+
95
+ return answer
96
 
97
  # --- Main Evaluation & Submission Function ---
98
  def run_and_submit_all(profile: gr.OAuthProfile | None):