GoodLuckChuck commited on
Commit
e00d74a
·
verified ·
1 Parent(s): 923ace2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py CHANGED
@@ -10,6 +10,70 @@ from dotenv import load_dotenv # Keep for potential local testing, but HF uses s
10
  API_BASE_URL = "https://agents-course-unit4-scoring.hf.space"
11
  HEADERS = {"Content-Type": "application/json", "accept": "application/json"}
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # --- OpenAI Client Configuration ---
14
  OPENAI_API_KEY_VALUE = os.environ.get("OPENAI_API_KEY")
15
  if not OPENAI_API_KEY_VALUE:
 
10
  API_BASE_URL = "https://agents-course-unit4-scoring.hf.space"
11
  HEADERS = {"Content-Type": "application/json", "accept": "application/json"}
12
 
13
+ # --- API Interaction Functions ---
14
+ def api_get_questions() -> List[Dict[str, Any]]:
15
+ """Fetch questions from the course API."""
16
+ try:
17
+ response = requests.get(f"{API_BASE_URL}/questions", headers=HEADERS)
18
+ response.raise_for_status() # Raises an HTTPError for bad responses (4xx, 5xx)
19
+ questions = response.json()
20
+ return questions
21
+ except requests.exceptions.RequestException as e:
22
+ print(f"Error fetching questions from API: {e}")
23
+ return []
24
+
25
+ def api_download_file(task_id: str, file_name: str, download_dir: str = "downloaded_gaia_files") -> Optional[str]:
26
+ """Download a file associated with a task from the course API."""
27
+ try:
28
+ # Create download directory if it doesn't exist
29
+ os.makedirs(download_dir, exist_ok=True)
30
+
31
+ # Construct the file download URL
32
+ download_url = f"{API_BASE_URL}/file/{task_id}"
33
+
34
+ # Download the file
35
+ response = requests.get(download_url, headers=HEADERS)
36
+ response.raise_for_status()
37
+
38
+ # Save the file
39
+ local_file_path = os.path.join(download_dir, file_name)
40
+ with open(local_file_path, 'wb') as f:
41
+ f.write(response.content)
42
+
43
+ print(f"Successfully downloaded file to: {local_file_path}")
44
+ return local_file_path
45
+
46
+ except requests.exceptions.RequestException as e:
47
+ print(f"Error downloading file for task {task_id}: {e}")
48
+ return None
49
+ except IOError as e:
50
+ print(f"Error saving file for task {task_id}: {e}")
51
+ return None
52
+
53
+ def api_submit_answers(username: str, agent_code_identifier: str, processed_answers: List[Dict[str, str]]) -> Dict[str, Any]:
54
+ """Submit processed answers to the course API."""
55
+ try:
56
+ submission_data = {
57
+ "username": username,
58
+ "agent_code": agent_code_identifier,
59
+ "answers": processed_answers
60
+ }
61
+
62
+ response = requests.post(
63
+ f"{API_BASE_URL}/submit",
64
+ headers=HEADERS,
65
+ json=submission_data
66
+ )
67
+ response.raise_for_status()
68
+
69
+ result = response.json()
70
+ print(f"Submission successful! API Response: {result}")
71
+ return result
72
+
73
+ except requests.exceptions.RequestException as e:
74
+ print(f"Error submitting answers to API: {e}")
75
+ return {}
76
+
77
  # --- OpenAI Client Configuration ---
78
  OPENAI_API_KEY_VALUE = os.environ.get("OPENAI_API_KEY")
79
  if not OPENAI_API_KEY_VALUE: