Duke-911 commited on
Commit
9d1352a
·
1 Parent(s): c773fe9

Refactor question fetching logic to support local JSON file or remote API; add JSON import

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import gradio as gr
3
  import requests
@@ -13,7 +14,7 @@ from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
-
17
 
18
  system_prompt="""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish
19
  your answer with the following template:
@@ -64,25 +65,29 @@ async def run_and_submit_all( profile: gr.OAuthProfile | None):
64
  print(agent_code)
65
 
66
  # 2. Fetch Questions
67
- print(f"Fetching questions from: {questions_url}")
68
- try:
69
- response = requests.get(questions_url, timeout=15)
70
- response.raise_for_status()
71
- questions_data = response.json()
72
- if not questions_data:
73
- print("Fetched questions list is empty.")
74
- return "Fetched questions list is empty or invalid format.", None
75
- print(f"Fetched {len(questions_data)} questions.")
76
- except requests.exceptions.RequestException as e:
77
- print(f"Error fetching questions: {e}")
78
- return f"Error fetching questions: {e}", None
79
- except requests.exceptions.JSONDecodeError as e:
80
- print(f"Error decoding JSON response from questions endpoint: {e}")
81
- print(f"Response text: {response.text[:500]}")
82
- return f"Error decoding server response for questions: {e}", None
83
- except Exception as e:
84
- print(f"An unexpected error occurred fetching questions: {e}")
85
- return f"An unexpected error occurred fetching questions: {e}", None
 
 
 
 
86
 
87
  # 3. Run your Agent
88
  results_log = []
 
1
+ import json
2
  import os
3
  import gradio as gr
4
  import requests
 
14
  # (Keep Constants as is)
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
+ FETCH_QUESTIONS_LOCAL = True
18
 
19
  system_prompt="""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish
20
  your answer with the following template:
 
65
  print(agent_code)
66
 
67
  # 2. Fetch Questions
68
+ if FETCH_QUESTIONS_LOCAL:
69
+ with open("questions.json", "r") as f:
70
+ questions_data = json.load(f)
71
+ else:
72
+ print(f"Fetching questions from: {questions_url}")
73
+ try:
74
+ response = requests.get(questions_url, timeout=15)
75
+ response.raise_for_status()
76
+ questions_data = response.json()
77
+ if not questions_data:
78
+ print("Fetched questions list is empty.")
79
+ return "Fetched questions list is empty or invalid format.", None
80
+ print(f"Fetched {len(questions_data)} questions.")
81
+ except requests.exceptions.RequestException as e:
82
+ print(f"Error fetching questions: {e}")
83
+ return f"Error fetching questions: {e}", None
84
+ except requests.exceptions.JSONDecodeError as e:
85
+ print(f"Error decoding JSON response from questions endpoint: {e}")
86
+ print(f"Response text: {response.text[:500]}")
87
+ return f"Error decoding server response for questions: {e}", None
88
+ except Exception as e:
89
+ print(f"An unexpected error occurred fetching questions: {e}")
90
+ return f"An unexpected error occurred fetching questions: {e}", None
91
 
92
  # 3. Run your Agent
93
  results_log = []