olaf-bu commited on
Commit
cf6e09d
·
1 Parent(s): 3b0a72a

try to download the files

Browse files
Files changed (2) hide show
  1. agent.py +3 -3
  2. app.py +31 -2
agent.py CHANGED
@@ -24,7 +24,7 @@ class Agent:
24
  logger.info('Initializing Agent')
25
  self.model = model
26
  self.tools = tools
27
- self.imports = ['pandas', 'numpy', 'os', 'requests',
28
  'tempfile', 'datetime', 'json', 'time', 're',
29
  'openpyxl', 'pathlib', 'sys', 'duckduckgo_search'
30
  ]
@@ -54,8 +54,8 @@ class Agent:
54
  Returns:
55
  str: The agent's answer as a string.
56
  """
57
- logger.debug('question is [%s]', question)
58
  answer = self.agent.run(self.prompt.format(question=question, context=file_path))
59
- logger.debug('got answer [%s]', answer)
60
  answer = str(answer).strip("'").strip('"').strip()
61
  return answer
 
24
  logger.info('Initializing Agent')
25
  self.model = model
26
  self.tools = tools
27
+ self.imports = ['pandas', 'numpy', 'os', 'requests', 'markdownify ',
28
  'tempfile', 'datetime', 'json', 'time', 're',
29
  'openpyxl', 'pathlib', 'sys', 'duckduckgo_search'
30
  ]
 
54
  Returns:
55
  str: The agent's answer as a string.
56
  """
57
+ logger.info('question is [%s], file_path %s', question, file_path)
58
  answer = self.agent.run(self.prompt.format(question=question, context=file_path))
59
+ logger.info('got answer [%s]', answer)
60
  answer = str(answer).strip("'").strip('"').strip()
61
  return answer
app.py CHANGED
@@ -50,7 +50,7 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
50
 
51
  # 1. Instantiate Agent ( modify this part to create your agent)
52
  try:
53
- agent = Agent(model=get_model('OpenAIServerModel', 'gpt-4.1-mini'), tools=get_tools())
54
  except Exception as e:
55
  print(f'Error instantiating agent: {e}')
56
  return f'Error initializing agent: {e}', None
@@ -89,8 +89,37 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
89
  if not task_id or question_text is None:
90
  print(f'Skipping item with missing task_id or question: {item}')
91
  continue
 
92
  try:
93
- submitted_answer = agent(question_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  answers_payload.append({'task_id': task_id,
95
  'submitted_answer': submitted_answer}
96
  )
 
50
 
51
  # 1. Instantiate Agent ( modify this part to create your agent)
52
  try:
53
+ agent = Agent(model=get_model('OpenAIServerModel', 'gpt-4.1'), tools=get_tools())
54
  except Exception as e:
55
  print(f'Error instantiating agent: {e}')
56
  return f'Error initializing agent: {e}', None
 
89
  if not task_id or question_text is None:
90
  print(f'Skipping item with missing task_id or question: {item}')
91
  continue
92
+ logger.info('working for task_id %s', task_id)
93
  try:
94
+ file_path: Optional[str] = None
95
+ try:
96
+ file_response = requests.get(f'{files_url}/{task_id}', timeout=15)
97
+ if file_response.status_code == 200 and file_response.content:
98
+ # Get filename from Content-Disposition header or URL
99
+ filename = None
100
+ content_disposition = file_response.headers.get('Content-Disposition')
101
+ if content_disposition and 'filename=' in content_disposition:
102
+ filename = content_disposition.split('filename=')[-1].strip('"')
103
+ else:
104
+ # Try to get filename from URL
105
+ url = file_response.url
106
+ filename = url.split('/')[-1]
107
+ if not filename or filename == str(task_id):
108
+ filename = f'file_{task_id}'
109
+
110
+ # Create temp directory and save file with original name
111
+ temp_dir = tempfile.mkdtemp()
112
+ file_path = os.path.join(temp_dir, filename)
113
+ with open(file_path, 'wb') as f:
114
+ f.write(file_response.content)
115
+ print(f'Downloaded file for task {task_id} to {file_path}')
116
+ else:
117
+ print(f'No file for task {task_id} or file is empty.')
118
+ except Exception as e:
119
+ print(f'Error downloading file for task {task_id}: {e}')
120
+ file_path = None
121
+
122
+ submitted_answer = agent(question_text, file_path)
123
  answers_payload.append({'task_id': task_id,
124
  'submitted_answer': submitted_answer}
125
  )