STARX33 commited on
Commit
3bd1fda
·
verified ·
1 Parent(s): 79a6984

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -10,7 +10,7 @@ from typing import Optional
10
  import pytz
11
  from smolagents import CodeAgent, HfApiModel, load_tool, tool
12
  from tools.final_answer import FinalAnswerTool
13
- from tools.web_search import DuckDuckGoSearchTool
14
 
15
  # -----------------------------------------------------------------------------
16
  # 1) YOUR CUSTOM TOOLS (with proper docstrings for Args)
@@ -85,11 +85,10 @@ with open("prompts.yaml", "r") as f:
85
  prompt_templates = yaml.safe_load(f)
86
 
87
  # extract the fallback map from YAML
88
- rag_fallbacks = prompt_templates.get("rag_fallbacks", {})
89
 
90
  final_answer = FinalAnswerTool()
91
- web_search = DuckDuckGoSearchTool()
92
- # If you need your text-to-image tool, uncomment:
93
  # image_generation = load_tool("agents-course/text-to-image", trust_remote_code=True)
94
 
95
  class BasicAgent:
@@ -123,21 +122,20 @@ class BasicAgent:
123
  raw_ans = self.agent.run(question)
124
  except Exception:
125
  raw_ans = ""
126
- # 2) stringify for the empty/failure check
127
  ans_str = str(raw_ans)
128
 
129
- # 3) if it failed to produce anything meaningful, try RAG fallback
130
- if not ans_str.strip() or ans_str.lower().startswith("i'm sorry"):
 
 
 
131
  q_low = question.lower()
132
  for key, val in self.rag_fallbacks.items():
133
- # extract tokens > 2 chars from the key
134
  tokens = re.findall(r"\w+", key.lower())
135
  tokens = [t for t in tokens if len(t) > 2]
136
- # if all appear in the question, use this fallback
137
  if all(tok in q_low for tok in tokens):
138
  return val
139
 
140
- # otherwise return the real answer
141
  return raw_ans
142
 
143
  # -----------------------------------------------------------------------------
@@ -186,7 +184,7 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
186
  }
187
  r2 = requests.post(submit_url, json=payload, timeout=60)
188
  r2.raise_for_status()
189
- res = r2.json()
190
  status = (
191
  f"✔ Submission Successful!\n"
192
  f"User: {res.get('username')}\n"
@@ -210,11 +208,11 @@ with gr.Blocks() as demo:
210
  2. Click “Run Evaluation & Submit All Answers” below.
211
  """)
212
  gr.LoginButton()
213
- btn = gr.Button("Run Evaluation & Submit All Answers")
214
- out_s = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
215
- out_df = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
216
 
217
  btn.click(run_and_submit_all, [], [out_s, out_df])
218
 
219
  if __name__ == "__main__":
220
- demo.launch(debug=True, share=False)
 
10
  import pytz
11
  from smolagents import CodeAgent, HfApiModel, load_tool, tool
12
  from tools.final_answer import FinalAnswerTool
13
+ from tools.web_search import DuckDuckGoSearchTool
14
 
15
  # -----------------------------------------------------------------------------
16
  # 1) YOUR CUSTOM TOOLS (with proper docstrings for Args)
 
85
  prompt_templates = yaml.safe_load(f)
86
 
87
  # extract the fallback map from YAML
88
+ rag_fallbacks = prompt_templates.get("fallbacks", {})
89
 
90
  final_answer = FinalAnswerTool()
91
+ web_search = DuckDuckGoSearchTool()
 
92
  # image_generation = load_tool("agents-course/text-to-image", trust_remote_code=True)
93
 
94
  class BasicAgent:
 
122
  raw_ans = self.agent.run(question)
123
  except Exception:
124
  raw_ans = ""
 
125
  ans_str = str(raw_ans)
126
 
127
+ # 2) determine if we need RAG fallback
128
+ is_empty = not ans_str.strip()
129
+ is_sorry = ans_str.lower().startswith("i'm sorry")
130
+ is_unanswered = any(marker in ans_str for marker in ["Unanswered", "failed", "None"])
131
+ if is_empty or is_sorry or is_unanswered:
132
  q_low = question.lower()
133
  for key, val in self.rag_fallbacks.items():
 
134
  tokens = re.findall(r"\w+", key.lower())
135
  tokens = [t for t in tokens if len(t) > 2]
 
136
  if all(tok in q_low for tok in tokens):
137
  return val
138
 
 
139
  return raw_ans
140
 
141
  # -----------------------------------------------------------------------------
 
184
  }
185
  r2 = requests.post(submit_url, json=payload, timeout=60)
186
  r2.raise_for_status()
187
+ res = r2.json()
188
  status = (
189
  f"✔ Submission Successful!\n"
190
  f"User: {res.get('username')}\n"
 
208
  2. Click “Run Evaluation & Submit All Answers” below.
209
  """)
210
  gr.LoginButton()
211
+ btn = gr.Button("Run Evaluation & Submit All Answers")
212
+ out_s = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
213
+ out_df= gr.DataFrame(label="Questions and Agent Answers", wrap=True)
214
 
215
  btn.click(run_and_submit_all, [], [out_s, out_df])
216
 
217
  if __name__ == "__main__":
218
+ demo.launch(debug=True, share=False)