Spaces:
Build error
Build error
File size: 1,006 Bytes
837e221 e4c7240 837e221 e4c7240 837e221 e4c7240 837e221 e4c7240 |
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 |
import io
from typing import Any, Dict
import pandas as pd
import requests
from smolagents import tool
@tool
def parse_csv(csv_url: str) -> Dict[str, Any]:
"""
Parse a CSV file and return its content as structured data.
Args:
csv_url: URL of the CSV file to parse
Returns:
Dictionary containing parsed CSV data
"""
try:
# Download the CSV
response = requests.get(csv_url)
response.raise_for_status()
# Parse the CSV
df = pd.read_csv(io.StringIO(response.text))
# Convert to dictionary format
columns = df.columns.tolist()
data = df.to_dict(orient="records")
# Return basic statistics and preview
return {
"columns": columns,
"row_count": len(data),
"preview": data[:5] if len(data) > 5 else data,
"column_dtypes": {col: str(df[col].dtype) for col in columns},
}
except Exception as e:
return {"error": str(e)}
|