George Sergia commited on
Commit
7bd852d
·
1 Parent(s): b11304e

Fix Excel reading and Python runner

Browse files
Files changed (3) hide show
  1. agent.py +2 -1
  2. app.py +4 -1
  3. excel_file_reader.py +22 -20
agent.py CHANGED
@@ -48,9 +48,10 @@ async def main(query: str, file_url: str | None) -> str:
48
 
49
  ctx = Context(agent)
50
 
 
51
  message = ChatMessage(
52
  role="user",
53
- content=f"{query}. Use the URL {file_url} as the Url path of the image, audio or Excel files which are mentioned in the question and are required to answer the question."
54
  )
55
 
56
  response = await agent.run(message, ctx=ctx)
 
48
 
49
  ctx = Context(agent)
50
 
51
+ prompt = query if file_url is None else f"{query}. Use the URL {file_url} as the Url path of attached files mentioned in the question. Attached file are required to answer the question. To run Python code use Code Intrepreter tool."
52
  message = ChatMessage(
53
  role="user",
54
+ content=prompt
55
  )
56
 
57
  response = await agent.run(message, ctx=ctx)
app.py CHANGED
@@ -20,7 +20,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
20
  """
21
 
22
  load_dotenv()
23
-
24
  # --- Determine HF Space Runtime URL and Repo URL ---
25
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
26
 
@@ -79,6 +79,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
79
  continue
80
  try:
81
  file_url = f"{files_url}/{task_id}"
 
 
 
82
  agent_output = asyncio.run(agent.main(question_text, file_url))
83
  submitted_answer = str(agent_output)
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
 
20
  """
21
 
22
  load_dotenv()
23
+
24
  # --- Determine HF Space Runtime URL and Repo URL ---
25
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
26
 
 
79
  continue
80
  try:
81
  file_url = f"{files_url}/{task_id}"
82
+ if "application/json" in requests.get(file_url).headers.get("Content-Type"):
83
+ file_url = None
84
+
85
  agent_output = asyncio.run(agent.main(question_text, file_url))
86
  submitted_answer = str(agent_output)
87
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
excel_file_reader.py CHANGED
@@ -1,10 +1,11 @@
1
  from typing import Optional
2
- from llama_index.readers.remote import RemoteReader
3
  from google import genai
4
  from google.genai import types
5
- import pathlib
6
  import pandas as pd
 
7
  import os
 
 
8
 
9
  def excel_file_reader(file_url: str, analysis_prompt: Optional[str] = None) -> str:
10
  """
@@ -23,26 +24,27 @@ def excel_file_reader(file_url: str, analysis_prompt: Optional[str] = None) -> s
23
 
24
  gemini_llm = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
25
 
26
- loader = RemoteReader()
27
- documents = loader.load_data(url=file_url)
28
-
29
- if len(documents) > 0:
30
- file_path = documents[0].metadata.get("file_path", None)
31
- print(f"Converting file {file_path} ")
32
- if file_path:
33
- sheets = pd.read_excel(file_path, sheet_name=None)
34
- for sheet_name, sheet_data in sheets.items():
35
- sheet_data.to_csv(f"{sheet_name}.csv", index=False)
36
- filepath = pathlib.Path(f"{sheet_name}.csv")
 
 
 
 
 
 
 
37
 
38
- text=analysis_prompt or "Provide a detailed description of this CSV file."
39
- csv_bytes = types.Part.from_bytes(data=filepath.read_bytes(),mime_type='text/csv',)
40
- response = gemini_llm.models.generate_content(
41
- model="gemini-2.0-flash",
42
- contents=[text,csv_bytes])
43
 
44
- return response.text
45
-
46
  return ""
47
 
48
  except Exception as e:
 
1
  from typing import Optional
 
2
  from google import genai
3
  from google.genai import types
 
4
  import pandas as pd
5
+ import requests
6
  import os
7
+ import io
8
+ from io import BytesIO
9
 
10
  def excel_file_reader(file_url: str, analysis_prompt: Optional[str] = None) -> str:
11
  """
 
24
 
25
  gemini_llm = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
26
 
27
+ resp = requests.get(file_url)
28
+
29
+ sheets = pd.read_excel(BytesIO(resp.content), sheet_name=None)
30
+ for _, sheet_data in sheets.items():
31
+ csv_buffer = io.StringIO()
32
+ sheet_data.to_csv(csv_buffer, index=False)
33
+ csv_bytes = csv_buffer.getvalue().encode('utf-8')
34
+
35
+ text = analysis_prompt or "Provide a detailed description of this CSV file."
36
+ csv_part = types.Part.from_bytes(
37
+ data=csv_bytes,
38
+ mime_type='text/csv'
39
+ )
40
+
41
+ response = gemini_llm.models.generate_content(
42
+ model="gemini-2.0-flash",
43
+ contents=[text, csv_part]
44
+ )
45
 
46
+ return response.text
 
 
 
 
47
 
 
 
48
  return ""
49
 
50
  except Exception as e: