File size: 1,503 Bytes
4325517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import tool
import os
import requests
import pandas as pd
from io import BytesIO

DEFAULT_API_URL = os.getenv("AGENT_API_URL", "https://agents-course-unit4-scoring.hf.space")

@tool
def read_excel_as_json(task_id: str) -> dict:
    """
    Fetches and parses an Excel file as structured JSON for a given task_id.
    
    Args:
        task_id: The task ID to fetch.
        
    Returns:
        {
            "task_id": str,
            "sheets": {
                "SheetName1": [ {col1: val1, col2: val2, ...}, ... ],
                ...
            },
            "status": "Success" | "Error"
        }
    """
    url = f"{DEFAULT_API_URL}/files/{task_id}"

    try:
        response = requests.get(url, timeout=10)
        if response.status_code != 200:
            return {"task_id": task_id, "sheets": {}, "status": f"{response.status_code} - Failed"}

        xls = pd.ExcelFile(BytesIO(response.content))
        sheets_json = {}

        for sheet in xls.sheet_names:
            df = xls.parse(sheet)
            df = df.dropna(how="all")  # Remove completely empty rows
            rows = df.head(20).to_dict(orient="records")  # limit to first 20 rows
            sheets_json[sheet] = rows

        return {
            "task_id": task_id,
            "sheets": sheets_json,
            "status": "Success"
        }

    except Exception as e:
        return {
            "task_id": task_id,
            "sheets": {},
            "status": f"Error: {str(e)}"
        }