tatianija commited on
Commit
6e735ee
·
verified ·
1 Parent(s): e9d007a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -3,22 +3,43 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- import smolagents
7
- from smolagents import DuckDuckGoSearchTool
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
14
  class BasicAgent:
15
  def __init__(self):
16
  print("BasicAgent initialized.")
17
- self.search_tool = DuckDuckGoSearchTool()
 
18
  def __call__(self, question: str) -> str:
19
- print(f"Agent received question (first 50 chars): {question[:50]}...")
20
- answer = self.search_tool.run(question)
21
- print(f"Agent returning fixed answer: {answer}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  return answer
23
 
24
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents.tools import duckduckgosearch
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
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 = duckduckgosearch
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):