Update sozo_gen.py
Browse files- sozo_gen.py +24 -23
sozo_gen.py
CHANGED
|
@@ -175,41 +175,42 @@ class ChartGenerator:
|
|
| 175 |
|
| 176 |
# --- Main Business Logic Functions ---
|
| 177 |
|
| 178 |
-
|
| 179 |
-
"""
|
| 180 |
-
Generates a markdown report draft and a list of chart specifications.
|
| 181 |
-
This function does NOT interact with storage.
|
| 182 |
-
"""
|
| 183 |
-
logging.info("Generating report draft and chart specifications.")
|
| 184 |
-
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", google_api_key=API_KEY, temperature=0.1)
|
| 185 |
-
ctx_dict = {"shape": df.shape, "columns": list(df.columns), "user_ctx": user_context}
|
| 186 |
-
enhanced_ctx = enhance_data_context(df, ctx_dict)
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
report_prompt = f"""
|
| 189 |
-
You are a senior data analyst. Analyze the provided dataset
|
| 190 |
**Dataset Analysis Context:** {json.dumps(enhanced_ctx, indent=2)}
|
| 191 |
**Instructions:**
|
| 192 |
1. **Executive Summary**: Start with a high-level summary of key findings.
|
| 193 |
-
2. **Key Insights**: Provide 3-5 key insights
|
| 194 |
-
3. **Visual Support**:
|
| 195 |
Valid chart types: bar, pie, line, scatter, hist.
|
| 196 |
Generate insights that would be valuable to C-level executives.
|
| 197 |
"""
|
| 198 |
md = llm.invoke(report_prompt).content
|
| 199 |
chart_descs = extract_chart_tags(md)[:MAX_CHARTS]
|
| 200 |
-
|
| 201 |
-
|
| 202 |
chart_generator = ChartGenerator(llm, df)
|
| 203 |
for desc in chart_descs:
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
def generate_single_chart(df: pd.DataFrame, description: str) -> Dict[str, Any]:
|
| 215 |
"""Generates a single chart specification dictionary."""
|
|
|
|
| 175 |
|
| 176 |
# --- Main Business Logic Functions ---
|
| 177 |
|
| 178 |
+
# In sozo_gen.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, bucket):
|
| 181 |
+
logging.info(f"Generating report draft for project {project_id}")
|
| 182 |
+
df = load_dataframe_safely(buf, name)
|
| 183 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", google_api_key=API_KEY, temperature=0.1)
|
| 184 |
+
ctx_dict = {"shape": df.shape, "columns": list(df.columns), "user_ctx": ctx}
|
| 185 |
+
enhanced_ctx = enhance_data_context(df, ctx_dict)
|
| 186 |
report_prompt = f"""
|
| 187 |
+
You are a senior data analyst and business intelligence expert. Analyze the provided dataset and write a comprehensive executive-level Markdown report.
|
| 188 |
**Dataset Analysis Context:** {json.dumps(enhanced_ctx, indent=2)}
|
| 189 |
**Instructions:**
|
| 190 |
1. **Executive Summary**: Start with a high-level summary of key findings.
|
| 191 |
+
2. **Key Insights**: Provide 3-5 key insights, each with its own chart tag.
|
| 192 |
+
3. **Visual Support**: Insert chart tags like: `<generate_chart: "chart_type | specific description">`.
|
| 193 |
Valid chart types: bar, pie, line, scatter, hist.
|
| 194 |
Generate insights that would be valuable to C-level executives.
|
| 195 |
"""
|
| 196 |
md = llm.invoke(report_prompt).content
|
| 197 |
chart_descs = extract_chart_tags(md)[:MAX_CHARTS]
|
| 198 |
+
chart_urls = {}
|
|
|
|
| 199 |
chart_generator = ChartGenerator(llm, df)
|
| 200 |
for desc in chart_descs:
|
| 201 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 202 |
+
img_path = Path(temp_file.name)
|
| 203 |
+
try:
|
| 204 |
+
chart_spec = chart_generator.generate_chart_spec(desc)
|
| 205 |
+
if execute_chart_spec(chart_spec, df, img_path):
|
| 206 |
+
blob_name = f"sozo_projects/{uid}/{project_id}/charts/{uuid.uuid4().hex}.png"
|
| 207 |
+
blob = bucket.blob(blob_name)
|
| 208 |
+
blob.upload_from_filename(str(img_path))
|
| 209 |
+
chart_urls[desc] = blob.public_url
|
| 210 |
+
logging.info(f"Uploaded chart '{desc}' to {blob.public_url}")
|
| 211 |
+
finally:
|
| 212 |
+
os.unlink(img_path)
|
| 213 |
+
return {"raw_md": md, "chartUrls": chart_urls}
|
| 214 |
|
| 215 |
def generate_single_chart(df: pd.DataFrame, description: str) -> Dict[str, Any]:
|
| 216 |
"""Generates a single chart specification dictionary."""
|