tatianija commited on
Commit
8b49454
·
verified ·
1 Parent(s): c275bbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -11,35 +11,53 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
 
 
14
  class BasicAgent:
15
- def __init__(self):
16
- print("BasicAgent initialized.")
17
  self.search = DuckDuckGoSearchTool()
 
 
 
18
 
19
  def __call__(self, question: str) -> str:
20
- print(f"Agent received question: {question}")
 
 
 
 
 
21
 
22
  try:
23
- # Perform the search; returns a list of results
24
  results = self.search(question)
 
 
25
  if not results:
26
  return "No results found for that query."
27
 
28
- # Results is a sequence of dicts, assume it has 'title' and 'snippet'
29
  top = results[0]
30
- title = top.get("title", "No title")
31
  snippet = top.get("snippet", "").strip()
32
  link = top.get("link", "")
33
 
34
- # Compose a clean answer
35
- answer = f"**{title}**\n{snippet}"
 
 
36
  if link:
37
- answer += f"\nSource: {link}"
 
 
38
 
 
 
 
39
  except Exception as e:
40
  answer = f"Sorry, I couldn't fetch results due to: {e}"
41
 
42
- print(f"Agent returning answer: {answer}")
 
 
43
  return answer
44
 
45
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
 
14
+
15
  class BasicAgent:
16
+ def __init__(self, debug: bool = False):
 
17
  self.search = DuckDuckGoSearchTool()
18
+ self.debug = debug
19
+ if self.debug:
20
+ print("BasicAgent initialized.")
21
 
22
  def __call__(self, question: str) -> str:
23
+ if self.debug:
24
+ print(f"Agent received question: {question}")
25
+
26
+ # Early validation
27
+ if not question or not question.strip():
28
+ return "Please provide a valid question."
29
 
30
  try:
 
31
  results = self.search(question)
32
+
33
+ # Use truthiness check and early return
34
  if not results:
35
  return "No results found for that query."
36
 
37
+ # Direct access with get() method chaining
38
  top = results[0]
39
+ title = top.get("title") or "No title"
40
  snippet = top.get("snippet", "").strip()
41
  link = top.get("link", "")
42
 
43
+ # Build answer more efficiently
44
+ parts = [f"**{title}**"]
45
+ if snippet:
46
+ parts.append(snippet)
47
  if link:
48
+ parts.append(f"Source: {link}")
49
+
50
+ answer = "\n".join(parts)
51
 
52
+ except (IndexError, KeyError, AttributeError):
53
+ # More specific exception handling
54
+ answer = "Sorry, I couldn't process the search results properly."
55
  except Exception as e:
56
  answer = f"Sorry, I couldn't fetch results due to: {e}"
57
 
58
+ if self.debug:
59
+ print(f"Agent returning answer: {answer}")
60
+
61
  return answer
62
 
63
  def run_and_submit_all( profile: gr.OAuthProfile | None):