Arash commited on
Commit
f1c49b9
·
1 Parent(s): 6715b3e

feat: setup agent in app

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -8,6 +8,7 @@ from agent import OptimaAgent
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
11
 
12
 
13
  def run_and_submit_all( profile: gr.OAuthProfile | None):
@@ -29,6 +30,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
29
  questions_url = f"{api_url}/questions"
30
  submit_url = f"{api_url}/submit"
31
 
 
 
 
 
 
 
 
 
32
  # 1. Instantiate Agent ( modify this part to create your agent)
33
  try:
34
  agent = OptimaAgent()
@@ -67,11 +76,33 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
67
  for item in questions_data:
68
  task_id = item.get("task_id")
69
  question_text = item.get("question")
 
 
70
  if not task_id or question_text is None:
71
  print(f"Skipping item with missing task_id or question: {item}")
72
  continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  try:
74
- submitted_answer = agent(question_text)
 
75
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
76
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
77
  except Exception as e:
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
+ APP_DOWNLOAD_DIR = "downloaded_files_app" # Directory for files downloaded by the Gradio app
12
 
13
 
14
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
30
  questions_url = f"{api_url}/questions"
31
  submit_url = f"{api_url}/submit"
32
 
33
+ # 0. Ensure download directory exists
34
+ try:
35
+ os.makedirs(APP_DOWNLOAD_DIR, exist_ok=True)
36
+ print(f"Ensured download directory exists: {APP_DOWNLOAD_DIR}")
37
+ except OSError as e:
38
+ print(f"Error creating download directory {APP_DOWNLOAD_DIR}: {e}")
39
+ return f"Error creating download directory: {e}", None
40
+
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
  agent = OptimaAgent()
 
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
79
+ file_name = item.get("file_name") # Get file_name if present
80
+
81
  if not task_id or question_text is None:
82
  print(f"Skipping item with missing task_id or question: {item}")
83
  continue
84
+
85
+ agent_file_path = None # Initialize file_path for the agent call
86
+ if file_name:
87
+ file_url = f"{api_url}/files/{task_id}"
88
+ local_file_path = os.path.join(APP_DOWNLOAD_DIR, file_name)
89
+ print(f"Attempting to download file for task {task_id}: {file_name} from {file_url} to {local_file_path}...")
90
+ try:
91
+ response_file = requests.get(file_url, stream=True, timeout=30) # Added timeout
92
+ response_file.raise_for_status()
93
+ with open(local_file_path, "wb") as f:
94
+ for chunk in response_file.iter_content(chunk_size=8192):
95
+ f.write(chunk)
96
+ print(f"Successfully downloaded to {local_file_path}")
97
+ agent_file_path = local_file_path
98
+ except requests.exceptions.RequestException as e:
99
+ print(f"Error downloading file {file_name} for task {task_id}: {e}. Agent will proceed without the file.")
100
+ except Exception as e: # Catch any other unexpected errors during file download
101
+ print(f"Unexpected error downloading file {file_name} for task {task_id}: {e}. Agent will proceed without the file.")
102
+
103
  try:
104
+ # Call the agent with question_text, task_id, and agent_file_path
105
+ submitted_answer = agent(question_text, task_id=task_id, file_path=agent_file_path)
106
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
107
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
108
  except Exception as e: