Spaces:
Sleeping
Sleeping
Update sozo_gen.py
Browse files- sozo_gen.py +40 -1
sozo_gen.py
CHANGED
|
@@ -570,6 +570,33 @@ def enhance_data_context(df: pd.DataFrame, ctx_dict: Dict) -> Dict[str, Any]:
|
|
| 570 |
|
| 571 |
return enhanced
|
| 572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, bucket):
|
| 574 |
"""
|
| 575 |
Enhanced autonomous report generation with intelligent narrative creation
|
|
@@ -595,7 +622,19 @@ def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, b
|
|
| 595 |
# Extract and process charts
|
| 596 |
chart_descs = extract_chart_tags(md)[:MAX_CHARTS]
|
| 597 |
chart_urls = {}
|
| 598 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
|
| 600 |
for desc in chart_descs:
|
| 601 |
# Create a safe key for Firebase
|
|
|
|
| 570 |
|
| 571 |
return enhanced
|
| 572 |
|
| 573 |
+
def create_chart_safe_context(enhanced_ctx: Dict) -> Dict:
|
| 574 |
+
"""
|
| 575 |
+
Create a chart-generator-safe version of enhanced context
|
| 576 |
+
by ensuring all values are JSON serializable
|
| 577 |
+
"""
|
| 578 |
+
def make_json_safe(obj):
|
| 579 |
+
if isinstance(obj, bool):
|
| 580 |
+
return bool(obj)
|
| 581 |
+
elif isinstance(obj, (np.integer, np.floating)):
|
| 582 |
+
return float(obj)
|
| 583 |
+
elif isinstance(obj, np.ndarray):
|
| 584 |
+
return obj.tolist()
|
| 585 |
+
elif isinstance(obj, np.bool_):
|
| 586 |
+
return bool(obj)
|
| 587 |
+
elif isinstance(obj, dict):
|
| 588 |
+
return {k: make_json_safe(v) for k, v in obj.items()}
|
| 589 |
+
elif isinstance(obj, (list, tuple)):
|
| 590 |
+
return [make_json_safe(item) for item in obj]
|
| 591 |
+
elif pd.isna(obj):
|
| 592 |
+
return None
|
| 593 |
+
elif hasattr(obj, 'item'): # numpy scalars
|
| 594 |
+
return obj.item()
|
| 595 |
+
else:
|
| 596 |
+
return obj
|
| 597 |
+
|
| 598 |
+
return make_json_safe(enhanced_ctx)
|
| 599 |
+
|
| 600 |
def generate_report_draft(buf, name: str, ctx: str, uid: str, project_id: str, bucket):
|
| 601 |
"""
|
| 602 |
Enhanced autonomous report generation with intelligent narrative creation
|
|
|
|
| 622 |
# Extract and process charts
|
| 623 |
chart_descs = extract_chart_tags(md)[:MAX_CHARTS]
|
| 624 |
chart_urls = {}
|
| 625 |
+
|
| 626 |
+
# Create a chart-safe context
|
| 627 |
+
chart_safe_ctx = create_chart_safe_context(enhanced_ctx)
|
| 628 |
+
|
| 629 |
+
# Try to pass the safe context to ChartGenerator
|
| 630 |
+
try:
|
| 631 |
+
chart_generator = ChartGenerator(llm, df, chart_safe_ctx)
|
| 632 |
+
except TypeError:
|
| 633 |
+
# Fallback: if ChartGenerator doesn't accept enhanced_ctx parameter
|
| 634 |
+
chart_generator = ChartGenerator(llm, df)
|
| 635 |
+
# If it has an enhanced_ctx attribute, set it safely
|
| 636 |
+
if hasattr(chart_generator, 'enhanced_ctx'):
|
| 637 |
+
chart_generator.enhanced_ctx = chart_safe_ctx
|
| 638 |
|
| 639 |
for desc in chart_descs:
|
| 640 |
# Create a safe key for Firebase
|