ai-data-analyst 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ai_data_analyst/__init__.py +3 -0
- ai_data_analyst/_cli.py +26 -0
- ai_data_analyst/agent/__init__.py +0 -0
- ai_data_analyst/agent/graph.py +200 -0
- ai_data_analyst/agent/llm.py +42 -0
- ai_data_analyst/agent/prompts.py +44 -0
- ai_data_analyst/agent/state.py +22 -0
- ai_data_analyst/chatbot.py +841 -0
- ai_data_analyst/config.py +36 -0
- ai_data_analyst/tools/__init__.py +0 -0
- ai_data_analyst/tools/analysis_tool.py +25 -0
- ai_data_analyst/tools/code_generator.py +421 -0
- ai_data_analyst/tools/dashboard_layout.py +136 -0
- ai_data_analyst/tools/dashboard_spec.py +373 -0
- ai_data_analyst/tools/data_cleaner.py +285 -0
- ai_data_analyst/tools/file_reader.py +205 -0
- ai_data_analyst/tools/powerbi_tool.py +1283 -0
- ai_data_analyst/tools/powerbpy_patch.py +152 -0
- ai_data_analyst/tools/python_runner.py +253 -0
- ai_data_analyst/utils/__init__.py +0 -0
- ai_data_analyst/utils/session_manager.py +196 -0
- ai_data_analyst-0.1.0.dist-info/METADATA +149 -0
- ai_data_analyst-0.1.0.dist-info/RECORD +27 -0
- ai_data_analyst-0.1.0.dist-info/WHEEL +5 -0
- ai_data_analyst-0.1.0.dist-info/entry_points.txt +2 -0
- ai_data_analyst-0.1.0.dist-info/licenses/LICENSE +201 -0
- ai_data_analyst-0.1.0.dist-info/top_level.txt +1 -0
ai_data_analyst/_cli.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""CLI entry point for the `ai-data-analyst` command.
|
|
2
|
+
|
|
3
|
+
After `pip install ai-data-analyst`, running `ai-data-analyst` in any terminal
|
|
4
|
+
finds the installed chatbot.py and hands it to Streamlit's own launcher —
|
|
5
|
+
equivalent to: streamlit run .../site-packages/ai_data_analyst/chatbot.py
|
|
6
|
+
|
|
7
|
+
Extra Streamlit flags are forwarded, e.g.:
|
|
8
|
+
ai-data-analyst --server.port 8080
|
|
9
|
+
"""
|
|
10
|
+
import sys
|
|
11
|
+
from importlib.resources import files
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main() -> None:
|
|
15
|
+
# Locate chatbot.py inside the installed package (works in any install mode)
|
|
16
|
+
app_path = str(files("ai_data_analyst").joinpath("chatbot.py"))
|
|
17
|
+
|
|
18
|
+
# Build sys.argv so Streamlit's CLI sees it correctly
|
|
19
|
+
sys.argv = ["streamlit", "run", app_path] + sys.argv[1:]
|
|
20
|
+
|
|
21
|
+
from streamlit.web import cli as stcli
|
|
22
|
+
sys.exit(stcli.main())
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
if __name__ == "__main__":
|
|
26
|
+
main()
|
|
File without changes
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from langgraph.graph import StateGraph, START, END
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .state import AgentState
|
|
7
|
+
from ..tools.file_reader import read_data, extract_excel_formatting
|
|
8
|
+
from ..tools.analysis_tool import get_dataset_schema
|
|
9
|
+
from ..tools.python_runner import run_python_code
|
|
10
|
+
from ..tools.code_generator import generate_python_code, fix_python_code
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
import pandas as pd
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def load_data_node(state: AgentState):
|
|
18
|
+
print("\n[1] Loading multiple datasets...")
|
|
19
|
+
dataframes = {}
|
|
20
|
+
|
|
21
|
+
for path in state["file_paths"]:
|
|
22
|
+
path = Path(path)
|
|
23
|
+
if not path.exists():
|
|
24
|
+
continue
|
|
25
|
+
|
|
26
|
+
if path.suffix.lower() in [".xlsx", ".xls"]:
|
|
27
|
+
# paste same helper import or duplicate call
|
|
28
|
+
from ..tools.file_reader import load_excel_all_sheets
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
tables = load_excel_all_sheets(path)
|
|
32
|
+
for table_name, df in tables.items():
|
|
33
|
+
key = f"df_{table_name}"
|
|
34
|
+
dataframes[key] = df
|
|
35
|
+
print(f" → Loading {path.name} sheet as {key}")
|
|
36
|
+
else:
|
|
37
|
+
name = re.sub(r"[^a-z0-9_]", "_", path.stem.lower())
|
|
38
|
+
key = f"df_{name}"
|
|
39
|
+
dataframes[key] = pd.read_csv(path)
|
|
40
|
+
print(f" → Loading {path.name} as {key}")
|
|
41
|
+
|
|
42
|
+
# Extract cell-level formatting (colours, merges, widths) for Excel files
|
|
43
|
+
formatting: dict = {}
|
|
44
|
+
for path in state["file_paths"]:
|
|
45
|
+
path = Path(path)
|
|
46
|
+
if path.suffix.lower() in [".xlsx", ".xlsm"]:
|
|
47
|
+
fmt = extract_excel_formatting(path)
|
|
48
|
+
base = re.sub(r"[^a-z0-9_]", "_", path.stem.lower())
|
|
49
|
+
base = re.sub(r"_+", "_", base).strip("_") or "excel"
|
|
50
|
+
for sheet_name, sheet_fmt in fmt.items():
|
|
51
|
+
safe_sheet = re.sub(r"[^a-z0-9_]", "_", str(sheet_name).lower())
|
|
52
|
+
safe_sheet = re.sub(r"_+", "_", safe_sheet).strip("_") or "sheet"
|
|
53
|
+
key = f"df_{base}__{safe_sheet}"
|
|
54
|
+
formatting[key] = sheet_fmt
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
"dataframes": dataframes,
|
|
58
|
+
"formatting": formatting,
|
|
59
|
+
"retry_count": 0,
|
|
60
|
+
}
|
|
61
|
+
# def load_data_node(state: AgentState):
|
|
62
|
+
# print("\n[1] Loading multiple datasets...")
|
|
63
|
+
|
|
64
|
+
# file_paths = state["file_paths"]
|
|
65
|
+
# dataframes = {}
|
|
66
|
+
|
|
67
|
+
# for path in file_paths:
|
|
68
|
+
# path_obj = Path(path)
|
|
69
|
+
|
|
70
|
+
# # Clean name → make it a valid Python variable
|
|
71
|
+
# raw_name = path_obj.stem.lower()
|
|
72
|
+
# # Replace any character that is not letter, number or underscore
|
|
73
|
+
# clean_name = re.sub(r'[^a-z0-9_]', '_', raw_name)
|
|
74
|
+
# # Remove multiple underscores
|
|
75
|
+
# clean_name = re.sub(r'_+', '_', clean_name).strip('_')
|
|
76
|
+
|
|
77
|
+
# print(f" → Loading {path_obj.name} as df_{clean_name}")
|
|
78
|
+
|
|
79
|
+
# df = read_data(path)
|
|
80
|
+
# dataframes[clean_name] = df
|
|
81
|
+
|
|
82
|
+
# print(f"Loaded {len(dataframes)} datasets.")
|
|
83
|
+
# return {
|
|
84
|
+
# "dataframes": dataframes,
|
|
85
|
+
# "retry_count": 0
|
|
86
|
+
# }
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def profile_data_node(state: AgentState):
|
|
90
|
+
print("\n[2] Creating schemas for all datasets...")
|
|
91
|
+
|
|
92
|
+
dataframes = state["dataframes"]
|
|
93
|
+
schema = get_dataset_schema(dataframes)
|
|
94
|
+
|
|
95
|
+
for name, cols in schema.items():
|
|
96
|
+
print(f"\n df_{name}:")
|
|
97
|
+
for col in cols:
|
|
98
|
+
print(f" - {col['column']} ({col['dtype']})")
|
|
99
|
+
|
|
100
|
+
return {"schema": schema}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def generate_code_node(state: AgentState):
|
|
104
|
+
print("\n[3] Asking Gemini to generate Python code...")
|
|
105
|
+
|
|
106
|
+
code = generate_python_code(
|
|
107
|
+
user_request=state["user_request"],
|
|
108
|
+
dataset_schema=state["schema"],
|
|
109
|
+
outputs_dir=state["outputs_dir"],
|
|
110
|
+
formatting=state.get("formatting", {}),
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
print("\nGenerated code:")
|
|
114
|
+
print("--------------------------------")
|
|
115
|
+
print(code)
|
|
116
|
+
print("--------------------------------")
|
|
117
|
+
|
|
118
|
+
return {"code": code, "error": ""}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def execute_code_node(state: AgentState):
|
|
122
|
+
print("\n[4] Validating and executing Python...")
|
|
123
|
+
|
|
124
|
+
try:
|
|
125
|
+
result = run_python_code(
|
|
126
|
+
code=state["code"],
|
|
127
|
+
dataframes=state["dataframes"],outputs_dir=state["outputs_dir"]
|
|
128
|
+
)
|
|
129
|
+
print("\nExecution successful.")
|
|
130
|
+
return {"result": result, "error": ""}
|
|
131
|
+
except Exception as e:
|
|
132
|
+
error_message = str(e)
|
|
133
|
+
print("\nExecution failed.")
|
|
134
|
+
print(error_message)
|
|
135
|
+
return {"result": "", "error": error_message}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def should_retry(state: AgentState):
|
|
139
|
+
error = state.get("error", "")
|
|
140
|
+
retry_count = state.get("retry_count", 0)
|
|
141
|
+
|
|
142
|
+
if not error:
|
|
143
|
+
print("\n[5] No error → END")
|
|
144
|
+
return "end"
|
|
145
|
+
|
|
146
|
+
if retry_count >= 2:
|
|
147
|
+
print("\n[5] Maximum retries reached → END")
|
|
148
|
+
return "end"
|
|
149
|
+
|
|
150
|
+
print(f"\n[5] Error detected → Retry {retry_count + 1}")
|
|
151
|
+
return "retry"
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def fix_code_node(state: AgentState):
|
|
155
|
+
retry_count = state.get("retry_count", 0) + 1
|
|
156
|
+
print(f"\n[6] Asking Gemini to fix code (attempt {retry_count})...")
|
|
157
|
+
|
|
158
|
+
fixed_code = fix_python_code(
|
|
159
|
+
user_request=state["user_request"],
|
|
160
|
+
dataset_schema=state["schema"],
|
|
161
|
+
previous_code=state["code"],
|
|
162
|
+
error=state["error"],
|
|
163
|
+
outputs_dir=state["outputs_dir"],
|
|
164
|
+
formatting=state.get("formatting", {}),
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
print("\nFixed code:")
|
|
168
|
+
print("--------------------------------")
|
|
169
|
+
print(fixed_code)
|
|
170
|
+
print("--------------------------------")
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
"code": fixed_code,
|
|
174
|
+
"retry_count": retry_count,
|
|
175
|
+
"error": ""
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def build_graph():
|
|
180
|
+
graph = StateGraph(AgentState)
|
|
181
|
+
|
|
182
|
+
graph.add_node("load_data", load_data_node)
|
|
183
|
+
graph.add_node("profile_data", profile_data_node)
|
|
184
|
+
graph.add_node("generate_code", generate_code_node)
|
|
185
|
+
graph.add_node("execute_code", execute_code_node)
|
|
186
|
+
graph.add_node("fix_code", fix_code_node)
|
|
187
|
+
|
|
188
|
+
graph.add_edge(START, "load_data")
|
|
189
|
+
graph.add_edge("load_data", "profile_data")
|
|
190
|
+
graph.add_edge("profile_data", "generate_code")
|
|
191
|
+
graph.add_edge("generate_code", "execute_code")
|
|
192
|
+
|
|
193
|
+
graph.add_conditional_edges(
|
|
194
|
+
"execute_code",
|
|
195
|
+
should_retry,
|
|
196
|
+
{"retry": "fix_code", "end": END}
|
|
197
|
+
)
|
|
198
|
+
graph.add_edge("fix_code", "execute_code")
|
|
199
|
+
|
|
200
|
+
return graph.compile()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""LLM provider module.
|
|
2
|
+
|
|
3
|
+
The LLM instance is NOT created at import time — call get_llm() wherever
|
|
4
|
+
an LLM is needed. The singleton is created on the first call and reused.
|
|
5
|
+
|
|
6
|
+
If the API key changes (e.g. user entered it in the sidebar) call reset_llm()
|
|
7
|
+
to force re-creation on the next get_llm() call.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
|
|
13
|
+
_llm = None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_llm():
|
|
17
|
+
"""Return the shared Gemini LLM instance, creating it lazily on first call."""
|
|
18
|
+
global _llm
|
|
19
|
+
if _llm is None:
|
|
20
|
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
21
|
+
from ai_data_analyst.config import get_api_key
|
|
22
|
+
|
|
23
|
+
api_key = os.environ.get("GEMINI_API_KEY") or get_api_key()
|
|
24
|
+
if not api_key:
|
|
25
|
+
raise RuntimeError(
|
|
26
|
+
"GEMINI_API_KEY is not configured. "
|
|
27
|
+
"Enter your key in the app sidebar or set the GEMINI_API_KEY environment variable."
|
|
28
|
+
)
|
|
29
|
+
_llm = ChatGoogleGenerativeAI(
|
|
30
|
+
model="gemini-2.0-flash-lite",
|
|
31
|
+
google_api_key=api_key,
|
|
32
|
+
)
|
|
33
|
+
return _llm
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def reset_llm() -> None:
|
|
37
|
+
"""Force the next get_llm() call to create a fresh LLM instance.
|
|
38
|
+
|
|
39
|
+
Call this after the user saves a new API key in the sidebar.
|
|
40
|
+
"""
|
|
41
|
+
global _llm
|
|
42
|
+
_llm = None
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
SYSTEM_PROMPT = """
|
|
2
|
+
You are a professional data analyst AI agent.
|
|
3
|
+
|
|
4
|
+
Your job is to analyze CSV and Excel datasets.
|
|
5
|
+
|
|
6
|
+
You have access to a pandas DataFrame called `df`.
|
|
7
|
+
|
|
8
|
+
You can generate Python code to analyze the dataset.
|
|
9
|
+
|
|
10
|
+
Available libraries:
|
|
11
|
+
|
|
12
|
+
- pandas
|
|
13
|
+
- numpy
|
|
14
|
+
- matplotlib
|
|
15
|
+
|
|
16
|
+
Full-data profile (use this; do not invent categories):
|
|
17
|
+
{json.dumps(profile, indent=2)}
|
|
18
|
+
|
|
19
|
+
Rules:
|
|
20
|
+
|
|
21
|
+
1. Use only the DataFrame `df`.
|
|
22
|
+
2. Do not read files yourself.
|
|
23
|
+
3. Do not write files until explicitly asked.
|
|
24
|
+
4. Do not use the operating system.
|
|
25
|
+
5. Do not use subprocess.
|
|
26
|
+
6. Do not use networking.
|
|
27
|
+
7. Do not install packages.
|
|
28
|
+
8. Do not use eval().
|
|
29
|
+
9. Do not use exec().
|
|
30
|
+
10. Do not use open().
|
|
31
|
+
11. Return analysis results using print().
|
|
32
|
+
12. If creating a chart, save it under the outputs directory.
|
|
33
|
+
13. Never invent column names.
|
|
34
|
+
14. Use the provided dataset schema.
|
|
35
|
+
|
|
36
|
+
Generate only Python code when asked for analysis code.
|
|
37
|
+
|
|
38
|
+
If you create a chart, use:
|
|
39
|
+
plt.savefig("outputs/chart_name.png", bbox_inches="tight")
|
|
40
|
+
plt.close()
|
|
41
|
+
print("Chart saved to outputs/chart_name.png")
|
|
42
|
+
Always print the final numerical/table result clearly.
|
|
43
|
+
If a chart is needed, save it to the outputs/ folder and print the path.
|
|
44
|
+
"""
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typing import TypedDict, Optional, Any, List, Dict
|
|
2
|
+
class AgentState(TypedDict, total=False):
|
|
3
|
+
|
|
4
|
+
# User input
|
|
5
|
+
user_request: str
|
|
6
|
+
file_paths: List[str]
|
|
7
|
+
|
|
8
|
+
# Datasets
|
|
9
|
+
dataframes: Dict[str, Any] # {"orders": df1, "customers": df2}
|
|
10
|
+
schema: Dict[str, Any] # schema for every file
|
|
11
|
+
formatting: Dict[str, Any] # Excel formatting metadata per table key
|
|
12
|
+
|
|
13
|
+
# Generated code
|
|
14
|
+
code: str
|
|
15
|
+
|
|
16
|
+
# Execution
|
|
17
|
+
result: str
|
|
18
|
+
|
|
19
|
+
# Error handling
|
|
20
|
+
error: str
|
|
21
|
+
retry_count: int
|
|
22
|
+
outputs_dir: str
|