GattoNero commited on
Commit
1166eef
·
verified ·
1 Parent(s): 2043dcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -1
app.py CHANGED
@@ -92,7 +92,7 @@ class BasicAgent:
92
 
93
  if file_info.endswith((".png", ".jpg", ".jpeg")):
94
  print("coso Image file detected, processing with GPT-4o")
95
- image = self._load_image(file_info)
96
  response = self._ask_gpt4o_with_image(image, text)
97
  return response
98
 
@@ -157,7 +157,52 @@ class BasicAgent:
157
 
158
 
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
 
163
  def create_mock_questions():
 
92
 
93
  if file_info.endswith((".png", ".jpg", ".jpeg")):
94
  print("coso Image file detected, processing with GPT-4o")
95
+ image = get_or_download_image(file_info)
96
  response = self._ask_gpt4o_with_image(image, text)
97
  return response
98
 
 
157
 
158
 
159
 
160
+ def get_or_download_image(file_name: str) -> Image.Image:
161
+ """
162
+ Controlla se l'immagine è presente in /data. Se non lo è, la scarica da Hugging Face.
163
+ Restituisce un oggetto PIL.Image.
164
+ """
165
+ file_path = os.path.join("data", file_name)
166
+
167
+ if not os.path.exists(file_path):
168
+ print(f"[INFO] File {file_name} non trovato in /data, lo scarico...")
169
+ url = f"https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/2023/validation/{file_name}"
170
+ try:
171
+ response = requests.get(url)
172
+ response.raise_for_status()
173
+ with open(file_path, "wb") as f:
174
+ f.write(response.content)
175
+ print(f"[INFO] Scaricato e salvato in {file_path}")
176
+ except Exception as e:
177
+ print(f"[ERRORE] Impossibile scaricare l'immagine: {e}")
178
+ return None
179
 
180
+ try:
181
+ return Image.open(file_path)
182
+ except Exception as e:
183
+ print(f"[ERRORE] Impossibile aprire l'immagine {file_path}: {e}")
184
+ return None
185
+
186
+ def download_file_if_needed(file_name):
187
+ if not file_name:
188
+ return None
189
+
190
+ local_path = f"/mnt/data/{file_name}"
191
+ if os.path.exists(local_path):
192
+ return local_path
193
+
194
+ base_url = "https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve"
195
+ commit_hash = "86620fe7a265fdd074ea8d8c8b7a556a1058b0af"
196
+ full_url = f"{base_url}/{commit_hash}/2023/validation/{file_name}"
197
+
198
+ response = requests.get(full_url)
199
+ if response.status_code == 200:
200
+ with open(local_path, "wb") as f:
201
+ f.write(response.content)
202
+ return local_path
203
+ else:
204
+ print(f"Failed to download {file_name}: {response.status_code}")
205
+ return None
206
 
207
 
208
  def create_mock_questions():