Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,23 +1,29 @@
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Optional, Union
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 6 |
from smolagents import (
|
| 7 |
-
CodeAgent,
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
)
|
| 10 |
from smolagents.tools import Tool
|
| 11 |
-
from tabulate import tabulate
|
| 12 |
|
| 13 |
-
# Load environment variables
|
| 14 |
load_dotenv()
|
| 15 |
|
|
|
|
| 16 |
model = LiteLLMModel(
|
| 17 |
-
model_id="gemini-2.5-flash-preview-09-2025",
|
| 18 |
-
api_key=os.getenv("GEMINI_API_KEY"),
|
| 19 |
-
provider="google_ai",
|
| 20 |
-
|
| 21 |
)
|
| 22 |
|
| 23 |
|
|
@@ -55,9 +61,7 @@ class ExcelToTextTool(Tool):
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
sheet: Union[str, int] = (
|
| 58 |
-
int(sheet_name)
|
| 59 |
-
if sheet_name and sheet_name.isdigit()
|
| 60 |
-
else sheet_name or 0
|
| 61 |
)
|
| 62 |
|
| 63 |
df = pd.read_excel(file_path, sheet_name=sheet)
|
|
@@ -75,7 +79,7 @@ class GaiaAgent:
|
|
| 75 |
"""An agent capable of using tools to answer general questions."""
|
| 76 |
|
| 77 |
def __init__(self):
|
| 78 |
-
print("GaiaAgent initialized with tools.")
|
| 79 |
|
| 80 |
tools = [
|
| 81 |
DuckDuckGoSearchTool(),
|
|
@@ -93,12 +97,12 @@ class GaiaAgent:
|
|
| 93 |
)
|
| 94 |
|
| 95 |
def __call__(self, task_id: str, question: str) -> str:
|
| 96 |
-
print(f"Agent received task_id='{task_id}' | question='{question[:50]}...'")
|
| 97 |
try:
|
| 98 |
answer = self.agent.run(question)
|
| 99 |
if not answer:
|
| 100 |
answer = "⚠️ Sorry, I could not generate a valid response."
|
| 101 |
except Exception as e:
|
| 102 |
answer = f"⚠️ Agent failed with error: {e}"
|
| 103 |
-
print(f"Agent returning answer: {answer}")
|
| 104 |
return answer
|
|
|
|
| 1 |
import os
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Optional, Union
|
| 4 |
+
|
| 5 |
import pandas as pd
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
from tabulate import tabulate
|
| 8 |
+
|
| 9 |
from smolagents import (
|
| 10 |
+
CodeAgent,
|
| 11 |
+
DuckDuckGoSearchTool,
|
| 12 |
+
FinalAnswerTool,
|
| 13 |
+
LiteLLMModel,
|
| 14 |
+
PythonInterpreterTool,
|
| 15 |
+
WikipediaSearchTool,
|
| 16 |
)
|
| 17 |
from smolagents.tools import Tool
|
|
|
|
| 18 |
|
| 19 |
+
# Load environment variables
|
| 20 |
load_dotenv()
|
| 21 |
|
| 22 |
+
# ✅ Initialize the model with Google AI Studio Gemini
|
| 23 |
model = LiteLLMModel(
|
| 24 |
+
model_id="gemini-2.5-flash-preview-09-2025", # short name only
|
| 25 |
+
api_key=os.getenv("GEMINI_API_KEY"), # set in Hugging Face secrets
|
| 26 |
+
provider="google_ai", # force AI Studio, not Vertex
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
|
|
|
|
| 61 |
|
| 62 |
try:
|
| 63 |
sheet: Union[str, int] = (
|
| 64 |
+
int(sheet_name) if sheet_name and sheet_name.isdigit() else sheet_name or 0
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
df = pd.read_excel(file_path, sheet_name=sheet)
|
|
|
|
| 79 |
"""An agent capable of using tools to answer general questions."""
|
| 80 |
|
| 81 |
def __init__(self):
|
| 82 |
+
print("✅ GaiaAgent initialized with tools.")
|
| 83 |
|
| 84 |
tools = [
|
| 85 |
DuckDuckGoSearchTool(),
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
def __call__(self, task_id: str, question: str) -> str:
|
| 100 |
+
print(f"🔹 Agent received task_id='{task_id}' | question='{question[:50]}...'")
|
| 101 |
try:
|
| 102 |
answer = self.agent.run(question)
|
| 103 |
if not answer:
|
| 104 |
answer = "⚠️ Sorry, I could not generate a valid response."
|
| 105 |
except Exception as e:
|
| 106 |
answer = f"⚠️ Agent failed with error: {e}"
|
| 107 |
+
print(f"🔹 Agent returning answer: {answer}")
|
| 108 |
return answer
|