Sonu313131 commited on
Commit
f5312d4
·
verified ·
1 Parent(s): d916609

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -21,18 +21,20 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
21
  class BasicAgent:
22
  def __init__(self):
23
  print("BasicAgent initialized.")
 
24
  def __call__(self, question: str) -> str:
25
  print(f"Agent received question (first 50 chars): {question[:50]}...")
26
  fixed_answer = "This is a default answer."
27
  print(f"Agent returning fixed answer: {fixed_answer}")
28
  return fixed_answer
29
- def run_and_submit_all( profile: gr.OAuthProfile | None):
30
- """
31
- Fetches all questions, runs the BasicAgent on them, submits all answers.
32
- """
33
 
34
 
35
- # 1. Instantiate Agent ( modify this part to create your agent)
 
 
 
 
 
36
  try:
37
  agent = CodeAgent(
38
  tools=[DuckDuckGoSearchTool()],
@@ -40,31 +42,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
40
  max_steps=5,
41
  verbosity_level=2
42
  )
43
-
44
  except Exception as e:
45
  print(f"Error instantiating agent: {e}")
46
  return f"Error initializing agent: {e}", None
47
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
 
 
48
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
  print(agent_code)
50
 
51
  # 2. Fetch Questions
 
 
52
  print(f"Fetching questions from: {questions_url}")
53
  try:
54
  response = requests.get(questions_url, timeout=15)
55
  response.raise_for_status()
56
  questions_data = response.json()
57
  if not questions_data:
58
- print("Fetched questions list is empty.")
59
- return "Fetched questions list is empty or invalid format.", None
60
  print(f"Fetched {len(questions_data)} questions.")
61
  except requests.exceptions.RequestException as e:
62
  print(f"Error fetching questions: {e}")
63
  return f"Error fetching questions: {e}", None
64
  except requests.exceptions.JSONDecodeError as e:
65
- print(f"Error decoding JSON response from questions endpoint: {e}")
66
- print(f"Response text: {response.text[:500]}")
67
- return f"Error decoding server response for questions: {e}", None
68
  except Exception as e:
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
 
21
  class BasicAgent:
22
  def __init__(self):
23
  print("BasicAgent initialized.")
24
+
25
  def __call__(self, question: str) -> str:
26
  print(f"Agent received question (first 50 chars): {question[:50]}...")
27
  fixed_answer = "This is a default answer."
28
  print(f"Agent returning fixed answer: {fixed_answer}")
29
  return fixed_answer
 
 
 
 
30
 
31
 
32
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
33
+ """
34
+ Fetches all questions, runs the BasicAgent on them, submits all answers.
35
+ """
36
+
37
+ # 1. Instantiate Agent (modify this part to create your agent)
38
  try:
39
  agent = CodeAgent(
40
  tools=[DuckDuckGoSearchTool()],
 
42
  max_steps=5,
43
  verbosity_level=2
44
  )
 
45
  except Exception as e:
46
  print(f"Error instantiating agent: {e}")
47
  return f"Error initializing agent: {e}", None
48
+
49
+ # In the case of an app running as a Hugging Face space, this link points to your codebase
50
+ space_id = os.getenv("SPACE_ID")
51
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
52
  print(agent_code)
53
 
54
  # 2. Fetch Questions
55
+ api_url = DEFAULT_API_URL
56
+ questions_url = f"{api_url}/questions"
57
  print(f"Fetching questions from: {questions_url}")
58
  try:
59
  response = requests.get(questions_url, timeout=15)
60
  response.raise_for_status()
61
  questions_data = response.json()
62
  if not questions_data:
63
+ print("Fetched questions list is empty.")
64
+ return "Fetched questions list is empty or invalid format.", None
65
  print(f"Fetched {len(questions_data)} questions.")
66
  except requests.exceptions.RequestException as e:
67
  print(f"Error fetching questions: {e}")
68
  return f"Error fetching questions: {e}", None
69
  except requests.exceptions.JSONDecodeError as e:
70
+ print(f"Error decoding JSON response from questions endpoint: {e}")
71
+ print(f"Response text: {response.text[:500]}")
72
+ return f"Error decoding server response for questions: {e}", None
73
  except Exception as e:
74
  print(f"An unexpected error occurred fetching questions: {e}")
75
  return f"An unexpected error occurred fetching questions: {e}", None