nismamjad commited on
Commit
8d5df3d
·
verified ·
1 Parent(s): da58ac4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -10,6 +10,7 @@ import json
10
  import base64 # <-- ADDED IMPORT for image handling
11
  import re
12
  import time
 
13
  # Attempt to login using environment token
14
  try:
15
  HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
@@ -287,10 +288,29 @@ def my_agent_logic(task_id: str, question: str, files_metadata: list = None): #
287
  except Exception as e_txt:
288
  print(f"Error decoding text file {file_name} for task {task_id}: {e_txt}")
289
  gemini_parts.append({"text": user_question_text + f"\n[Agent note: A text file '{file_name}' was associated but could not be decoded: {e_txt}]"})
290
- else: # Other file types, just mention them
291
- user_question_text += f"\n\nNote: A file named '{file_name}' (type: {detected_mime_type or 'unknown'}) is associated with this question. Its content is not directly viewable in this text prompt."
292
- gemini_parts.append({"text": user_question_text})
293
- print(f"Noted non-image/text file {file_name} ({detected_mime_type}) in Gemini prompt for task {task_id}.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  else: # No file content fetched or no files associated
295
  gemini_parts.append({"text": user_question_text})
296
 
 
10
  import base64 # <-- ADDED IMPORT for image handling
11
  import re
12
  import time
13
+ import pandas as pd
14
  # Attempt to login using environment token
15
  try:
16
  HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
 
288
  except Exception as e_txt:
289
  print(f"Error decoding text file {file_name} for task {task_id}: {e_txt}")
290
  gemini_parts.append({"text": user_question_text + f"\n[Agent note: A text file '{file_name}' was associated but could not be decoded: {e_txt}]"})
291
+ ###########################################################################################################################
292
+ elif detected_mime_type in [
293
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
294
+ "application/vnd.ms-excel",
295
+ "text/csv"]:
296
+ try:
297
+ from io import BytesIO
298
+ if "csv" in detected_mime_type:
299
+ df = pd.read_csv(BytesIO(file_content_bytes))
300
+ else:
301
+ df = pd.read_excel(BytesIO(file_content_bytes))
302
+
303
+ preview = df.head(10).to_string(index=False)
304
+ user_question_text += f"\n\nContent preview from the attached spreadsheet '{file_name}':\n{preview}"
305
+ gemini_parts.append({"text": user_question_text})
306
+ print(f"Added spreadsheet content preview for {file_name} to Gemini prompt.")
307
+ except Exception as e_xls:
308
+ print(f"Error reading spreadsheet file {file_name} for task {task_id}: {e_xls}")
309
+ user_question_text += f"\n\n[Agent note: Unable to parse spreadsheet '{file_name}': {e_xls}]"
310
+ gemini_parts.append({"text": user_question_text})
311
+
312
+
313
+ ###########################################################################################################################
314
  else: # No file content fetched or no files associated
315
  gemini_parts.append({"text": user_question_text})
316