Spaces:
Sleeping
Sleeping
File size: 3,030 Bytes
be45560 78683dc 722b585 78683dc 142b5ba d8b8502 7828173 33402b3 e1086aa 062f80b 2a39f0c 062f80b 2a39f0c 062f80b 2a39f0c 062f80b c1d27b0 9323159 90c6fb1 52d7eb6 90c6fb1 52d7eb6 533550b 33402b3 52d7eb6 90c6fb1 6470a23 722b585 36dac61 8f8fb45 6470a23 7cb9551 6470a23 133de3a 90c6fb1 39ec4fe 133de3a 6470a23 7828173 36dac61 0e701d6 7828173 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import smolagents, numpy, math, xlrd, os
import pandas as pd
from typing import Union
from smolagents import (
tool,
CodeAgent,
HfApiModel,
InferenceClientModel,
WebSearchTool,
PythonInterpreterTool,
FinalAnswerTool,
DuckDuckGoSearchTool,
GoogleSearchTool
)
#*
@tool
def analyze_excel_file(file_path: str, query: str) -> str:
"""
Analyze an Excel file using pandas and answer a question about it.
Args:
file_path: Path to the Excel file
query: Question about the data
Returns:
Analysis result or error message
"""
try:
import pandas as pd
# Read the Excel file
df = pd.read_excel(file_path)
# Run various analyses based on the query
result = f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
result += f"Columns: {', '.join(df.columns)}\n\n"
# Add summary statistics
result += "Summary statistics:\n"
result += str(df.describe())
return result
except ImportError:
return "Error: pandas and openpyxl are not installed. Please install them with 'pip install pandas openpyxl'."
except Exception as e:
return f"Error analyzing Excel file: {str(e)}"
#
class newAgent:
"""Adapts smolagents.CodeAgent to the HF course template API."""
def __init__(self):
model_id = "meta-llama/Meta-Llama-3-70B-Instruct" # correct repo name
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN") # read real secret
if not hf_token:
raise RuntimeError("HUGGINGFACEHUB_API_TOKEN not set in Space secrets")
#*
system_prompt=(
"You are an agent that answers exam questions."
"Your answers should contain only what is asked for in the question, without any other content."
"Be exact and concise in your answers to get a good score on the exam questions. Do not add explanation to the answers."
"If you are ask for a list of items, reply with ONLY those items separated by commas, no other text."
"You have a tool to read Excel files. The Excel file tool is named read_excel"
)
#*
model = HfApiModel(model_id=model_id, token=hf_token)
# include FinalAnswerTool in tools so agent knows when to stop
tools = [FinalAnswerTool(), read_excel]
self.agent = CodeAgent(
tools=tools,
model=model,
add_base_tools=True,
max_steps=5 # limit reasoning time
)
def __call__(self, question: str) -> str:
"""ONE question in β ONE pure-text answer out."""
#β Replace .run with whatever method actually returns the answer string.
result = self.agent.run(question)
return result
#answer = self.run
#agent.run(
# "At what temperature and for how long should I bake French baguettes made with type 65 flour?",
#) |