bvantuan commited on
Commit
ddbbdbf
·
1 Parent(s): 81917a3

gemini-2.0-flash-lite for CodeAgent

Browse files
Files changed (2) hide show
  1. app.py +99 -5
  2. utils.py +51 -0
app.py CHANGED
@@ -1,8 +1,83 @@
1
  import os
 
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -13,11 +88,28 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  class BasicAgent:
14
  def __init__(self):
15
  print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -76,6 +168,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
 
 
79
  if not task_id or question_text is None:
80
  print(f"Skipping item with missing task_id or question: {item}")
81
  continue
 
1
  import os
2
+ from pathlib import Path
3
  import gradio as gr
4
  import requests
 
5
  import pandas as pd
6
+ from utils import process_file
7
+ from typing import Optional, Union
8
+ from dotenv import load_dotenv
9
+ from smolagents import (CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool,
10
+ LiteLLMModel, PythonInterpreterTool,
11
+ WikipediaSearchTool)
12
+ from smolagents.tools import Tool
13
+ from tabulate import tabulate
14
+
15
+
16
+ # Load environment variables
17
+ load_dotenv()
18
+
19
+ # Initialize the model
20
+ model = LiteLLMModel(
21
+ model_id=os.getenv("GEMINI_MODEL"), api_key=os.getenv("GEMINI_API_KEY")
22
+ )
23
+
24
+
25
+ class ExcelToTextTool(Tool):
26
+ """Render an Excel worksheet as a Markdown table."""
27
+
28
+ name = "excel_to_text"
29
+ description = (
30
+ "Read an Excel file and return a Markdown table of the requested sheet. "
31
+ "Accepts either the sheet name or a zero-based index (as a string)."
32
+ )
33
+
34
+ inputs = {
35
+ "excel_path": {
36
+ "type": "string",
37
+ "description": "Path to the Excel file (.xlsx or .xls).",
38
+ },
39
+ "sheet_name": {
40
+ "type": "string",
41
+ "description": (
42
+ "Worksheet name or zero-based index (as a string). "
43
+ "Optional; defaults to the first sheet."
44
+ ),
45
+ "nullable": True,
46
+ },
47
+ }
48
+
49
+ output_type = "string"
50
+
51
+ def forward(self, excel_path: str, sheet_name: Optional[str] = None) -> str:
52
+ """Load the Excel file and return the sheet as a Markdown table.
53
+ Args:
54
+ excel_path: Path to the Excel file.
55
+ sheet_name: Optional name or index of the sheet to read. If None, reads the first sheet.
56
+ Returns:
57
+ A Markdown table representing the Excel sheet, or an error message if the file is not found or cannot be read.
58
+ """
59
+
60
+ file_path = Path(excel_path).expanduser().resolve()
61
+ if not file_path.is_file():
62
+ return f"Error: Excel file not found at {file_path}"
63
+
64
+ try:
65
+ sheet: Union[str, int] = (
66
+ int(sheet_name)
67
+ if sheet_name and sheet_name.isdigit()
68
+ else sheet_name or 0
69
+ )
70
+
71
+ df = pd.read_excel(file_path, sheet_name=sheet)
72
+
73
+ if hasattr(df, "to_markdown"):
74
+ return df.to_markdown(index=False)
75
+
76
+ return tabulate(df, headers="keys", tablefmt="github", showindex=False)
77
+
78
+ except Exception as e:
79
+ return f"Error reading Excel file: {e}"
80
+
81
 
82
  # (Keep Constants as is)
83
  # --- Constants ---
 
88
  class BasicAgent:
89
  def __init__(self):
90
  print("BasicAgent initialized.")
91
+ tools = [
92
+ DuckDuckGoSearchTool(),
93
+ WikipediaSearchTool(),
94
+ ExcelToTextTool(),
95
+ PythonInterpreterTool(),
96
+ FinalAnswerTool(),
97
+ ]
98
+
99
+ self.agent = CodeAgent(
100
+ model=model,
101
+ tools=tools,
102
+ add_base_tools=True,
103
+ additional_authorized_imports=["pandas", "numpy", "csv", "subprocess"],
104
+ )
105
+
106
  def __call__(self, question: str) -> str:
107
+ print(f"Agent received question='{question}...'")
108
+ # fixed_answer = "This is a default answer."
109
+ answer = self.agent.run(question)
110
+ print(f"Agent returning answer: {answer}")
111
+ return answer
112
+
113
 
114
  def run_and_submit_all( profile: gr.OAuthProfile | None):
115
  """
 
168
  for item in questions_data:
169
  task_id = item.get("task_id")
170
  question_text = item.get("question")
171
+ if item.get("file_name"):
172
+ question_text = process_file(item.get("file_name"), question_text)
173
  if not task_id or question_text is None:
174
  print(f"Skipping item with missing task_id or question: {item}")
175
  continue
utils.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Tuple
2
+ import re
3
+ import tempfile
4
+ from pathlib import Path
5
+ import pandas as pd
6
+ import requests
7
+ from pandas import DataFrame
8
+
9
+ # --- Constants ---
10
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
+ QUESTIONS_URL = f"{DEFAULT_API_URL}/questions"
12
+ SUBMIT_URL = f"{DEFAULT_API_URL}/submit"
13
+ FILE_PATH = f"{DEFAULT_API_URL}/files/"
14
+
15
+
16
+ def process_file(task_id: str, question_text: str) -> str:
17
+ """
18
+ Attempt to download a file associated with a task from the API.
19
+ - If the file exists (HTTP 200), it is saved to a temp directory and the local file path is returned.
20
+ - If no file is found (HTTP 404), returns None.
21
+ - For all other HTTP errors, the exception is propagated to the caller.
22
+ """
23
+ file_url = f"{FILE_PATH}{task_id}"
24
+
25
+ try:
26
+ response = requests.get(file_url, timeout=30)
27
+ response.raise_for_status()
28
+ except requests.exceptions.RequestException as exc:
29
+ print(f"Exception in download_file>> {str(exc)}")
30
+ return question_text # Unable to get the file
31
+
32
+ # Determine filename from 'Content-Disposition' header, fallback to task_id
33
+ content_disposition = response.headers.get("content-disposition", "")
34
+ filename = task_id
35
+ match = re.search(r'filename="([^"]+)"', content_disposition)
36
+ if match:
37
+ filename = match.group(1)
38
+
39
+ # Save file in a temp directory
40
+ temp_storage_dir = Path(tempfile.gettempdir()) / "gaia_cached_files"
41
+ temp_storage_dir.mkdir(parents=True, exist_ok=True)
42
+
43
+ file_path = temp_storage_dir / filename
44
+ file_path.write_bytes(response.content)
45
+ return (
46
+ f"{question_text}\n\n"
47
+ f"---\n"
48
+ f"A file was downloaded for this task and saved locally at:\n"
49
+ f"{str(file_path)}\n"
50
+ f"---\n\n"
51
+ )