Spaces:
Sleeping
Sleeping
add excel
Browse files- app.py +74 -6
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,13 +1,17 @@
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
-
import inspect
|
5 |
import openai
|
6 |
-
import
|
7 |
-
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, PythonInterpreterTool, CodeAgent, WikipediaSearchTool
|
8 |
from pathlib import Path
|
9 |
import tempfile
|
10 |
-
from smolagents.tools import PipelineTool
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# (Keep Constants as is)
|
13 |
# --- Constants ---
|
@@ -67,6 +71,71 @@ class SpeechToTextTool(PipelineTool):
|
|
67 |
# For response_format="text", `response` is already the raw transcript
|
68 |
return response
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
71 |
"""
|
72 |
Try GET /files/{task_id}.
|
@@ -89,7 +158,6 @@ def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
|
89 |
cdisp = resp.headers.get("content-disposition", "")
|
90 |
filename = task_id # default base name
|
91 |
if "filename=" in cdisp:
|
92 |
-
import re
|
93 |
m = re.search(r'filename="([^"]+)"', cdisp)
|
94 |
if m:
|
95 |
filename = m.group(1) # keep provided name
|
@@ -107,7 +175,7 @@ class BasicAgent:
|
|
107 |
def __init__(self):
|
108 |
self.agent = CodeAgent(
|
109 |
model=OpenAIServerModel(model_id="gpt-4o"),
|
110 |
-
tools=[DuckDuckGoSearchTool(), WikipediaSearchTool(), SpeechToTextTool()],
|
111 |
add_base_tools=True,
|
112 |
additional_authorized_imports=['pandas','numpy','csv','subprocess']
|
113 |
)
|
|
|
1 |
+
# app.py
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
import requests
|
|
|
5 |
import openai
|
6 |
+
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
|
|
|
7 |
from pathlib import Path
|
8 |
import tempfile
|
9 |
+
from smolagents.tools import PipelineTool, Tool
|
10 |
+
import pathlib
|
11 |
+
from typing import Union, Optional
|
12 |
+
import pandas as pd
|
13 |
+
from tabulate import tabulate # pragma: no cover – fallback path
|
14 |
+
import re
|
15 |
|
16 |
# (Keep Constants as is)
|
17 |
# --- Constants ---
|
|
|
71 |
# For response_format="text", `response` is already the raw transcript
|
72 |
return response
|
73 |
|
74 |
+
class ExcelToTextTool(Tool):
|
75 |
+
"""Render an Excel worksheet as Markdown text."""
|
76 |
+
|
77 |
+
# ------------------------------------------------------------------
|
78 |
+
# Required smol‑agents metadata
|
79 |
+
# ------------------------------------------------------------------
|
80 |
+
name = "excel_to_text"
|
81 |
+
description = (
|
82 |
+
"Read an Excel file and return a Markdown table of the requested sheet. "
|
83 |
+
"Accepts either the sheet name or the zero-based index."
|
84 |
+
)
|
85 |
+
|
86 |
+
inputs = {
|
87 |
+
"excel_path": {
|
88 |
+
"type": "string",
|
89 |
+
"description": "Path to the Excel file (.xlsx / .xls).",
|
90 |
+
},
|
91 |
+
"sheet_name": {
|
92 |
+
"type": "string",
|
93 |
+
"description": (
|
94 |
+
"Worksheet name or zero‑based index *as a string* (optional; default first sheet)."
|
95 |
+
),
|
96 |
+
"nullable": True,
|
97 |
+
},
|
98 |
+
}
|
99 |
+
|
100 |
+
output_type = "string"
|
101 |
+
|
102 |
+
# ------------------------------------------------------------------
|
103 |
+
# Core logic
|
104 |
+
# ------------------------------------------------------------------
|
105 |
+
def forward(
|
106 |
+
self,
|
107 |
+
excel_path: str,
|
108 |
+
sheet_name: Optional[str] = None,
|
109 |
+
) -> str:
|
110 |
+
"""Load *excel_path* and return the sheet as a Markdown table."""
|
111 |
+
|
112 |
+
path = pathlib.Path(excel_path).expanduser().resolve()
|
113 |
+
if not path.exists():
|
114 |
+
return f"Error: Excel file not found at {path}"
|
115 |
+
|
116 |
+
try:
|
117 |
+
# Interpret sheet identifier -----------------------------------
|
118 |
+
sheet: Union[str, int]
|
119 |
+
if sheet_name is None or sheet_name == "":
|
120 |
+
sheet = 0 # first sheet
|
121 |
+
else:
|
122 |
+
# If the user passed a numeric string (e.g. "1"), cast to int
|
123 |
+
sheet = int(sheet_name) if sheet_name.isdigit() else sheet_name
|
124 |
+
|
125 |
+
# Load worksheet ----------------------------------------------
|
126 |
+
df = pd.read_excel(path, sheet_name=sheet)
|
127 |
+
|
128 |
+
# Render to Markdown; fall back to tabulate if needed ---------
|
129 |
+
if hasattr(pd.DataFrame, "to_markdown"):
|
130 |
+
return df.to_markdown(index=False)
|
131 |
+
from tabulate import tabulate # pragma: no cover – fallback path
|
132 |
+
|
133 |
+
return tabulate(df, headers="keys", tablefmt="github", showindex=False)
|
134 |
+
|
135 |
+
except Exception as exc: # broad catch keeps the agent chat‑friendly
|
136 |
+
return f"Error reading Excel file: {exc}"
|
137 |
+
|
138 |
+
|
139 |
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
140 |
"""
|
141 |
Try GET /files/{task_id}.
|
|
|
158 |
cdisp = resp.headers.get("content-disposition", "")
|
159 |
filename = task_id # default base name
|
160 |
if "filename=" in cdisp:
|
|
|
161 |
m = re.search(r'filename="([^"]+)"', cdisp)
|
162 |
if m:
|
163 |
filename = m.group(1) # keep provided name
|
|
|
175 |
def __init__(self):
|
176 |
self.agent = CodeAgent(
|
177 |
model=OpenAIServerModel(model_id="gpt-4o"),
|
178 |
+
tools=[DuckDuckGoSearchTool(), WikipediaSearchTool(), SpeechToTextTool(), ExcelToTextTool()],
|
179 |
add_base_tools=True,
|
180 |
additional_authorized_imports=['pandas','numpy','csv','subprocess']
|
181 |
)
|
requirements.txt
CHANGED
@@ -5,4 +5,6 @@ smolagents[audio]
|
|
5 |
smolagents
|
6 |
wikipedia-api
|
7 |
transformers
|
8 |
-
smolagents[transformers]
|
|
|
|
|
|
5 |
smolagents
|
6 |
wikipedia-api
|
7 |
transformers
|
8 |
+
smolagents[transformers]
|
9 |
+
tabulate
|
10 |
+
openpyxl
|