Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,8 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import sqlite3
|
| 4 |
import os
|
| 5 |
-
import io
|
| 6 |
import json
|
| 7 |
from pathlib import Path
|
| 8 |
-
import tempfile
|
| 9 |
-
from fpdf import FPDF
|
| 10 |
import plotly.express as px
|
| 11 |
from datetime import datetime, timezone
|
| 12 |
from crewai import Agent, Crew, Process, Task
|
|
@@ -86,94 +83,6 @@ if st.session_state.df is not None and st.session_state.show_preview:
|
|
| 86 |
st.subheader("π Dataset Preview")
|
| 87 |
st.dataframe(st.session_state.df.head())
|
| 88 |
|
| 89 |
-
# Helper Function to Create a PDF Report with Visualizations and Descriptions
|
| 90 |
-
def create_pdf_report_with_viz(report, conclusion, visualizations):
|
| 91 |
-
pdf = FPDF()
|
| 92 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
| 93 |
-
pdf.add_page()
|
| 94 |
-
pdf.set_font("Arial", size=12)
|
| 95 |
-
|
| 96 |
-
# Title
|
| 97 |
-
pdf.set_font("Arial", style="B", size=18)
|
| 98 |
-
pdf.cell(0, 10, "π Analysis Report", ln=True, align="C")
|
| 99 |
-
pdf.ln(10)
|
| 100 |
-
|
| 101 |
-
# Report Content
|
| 102 |
-
pdf.set_font("Arial", style="B", size=14)
|
| 103 |
-
pdf.cell(0, 10, "Analysis", ln=True)
|
| 104 |
-
pdf.set_font("Arial", size=12)
|
| 105 |
-
pdf.multi_cell(0, 10, report)
|
| 106 |
-
|
| 107 |
-
pdf.ln(10)
|
| 108 |
-
pdf.set_font("Arial", style="B", size=14)
|
| 109 |
-
pdf.cell(0, 10, "Conclusion", ln=True)
|
| 110 |
-
pdf.set_font("Arial", size=12)
|
| 111 |
-
pdf.multi_cell(0, 10, conclusion)
|
| 112 |
-
|
| 113 |
-
# Add Visualizations
|
| 114 |
-
pdf.add_page()
|
| 115 |
-
pdf.set_font("Arial", style="B", size=16)
|
| 116 |
-
pdf.cell(0, 10, "π Visualizations", ln=True)
|
| 117 |
-
pdf.ln(5)
|
| 118 |
-
|
| 119 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
| 120 |
-
for i, fig in enumerate(visualizations, start=1):
|
| 121 |
-
fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
|
| 122 |
-
x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
|
| 123 |
-
y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
|
| 124 |
-
|
| 125 |
-
# Save each visualization as a PNG image
|
| 126 |
-
img_path = os.path.join(temp_dir, f"viz_{i}.png")
|
| 127 |
-
fig.write_image(img_path)
|
| 128 |
-
|
| 129 |
-
# Insert Title and Description
|
| 130 |
-
pdf.set_font("Arial", style="B", size=14)
|
| 131 |
-
pdf.multi_cell(0, 10, f"{i}. {fig_title}")
|
| 132 |
-
pdf.set_font("Arial", size=12)
|
| 133 |
-
pdf.multi_cell(0, 10, f"X-axis: {x_axis} | Y-axis: {y_axis}")
|
| 134 |
-
pdf.ln(3)
|
| 135 |
-
|
| 136 |
-
# Embed Visualization
|
| 137 |
-
pdf.image(img_path, w=170)
|
| 138 |
-
pdf.ln(10)
|
| 139 |
-
|
| 140 |
-
# Save PDF
|
| 141 |
-
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 142 |
-
pdf.output(temp_pdf.name)
|
| 143 |
-
|
| 144 |
-
return temp_pdf
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
# Helper function to create a plain text report with visualization summaries
|
| 148 |
-
def create_text_report_with_viz(report, conclusion, visualizations):
|
| 149 |
-
content = f"### Analysis Report\n\n{report}\n\n### Conclusion\n\n{conclusion}\n\n### Visualizations\n"
|
| 150 |
-
|
| 151 |
-
# Dynamically add descriptions for each visualization
|
| 152 |
-
for i, fig in enumerate(visualizations, start=1):
|
| 153 |
-
# Extract the title of the Plotly figure if available
|
| 154 |
-
fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
|
| 155 |
-
|
| 156 |
-
# Add title and figure details
|
| 157 |
-
content += f"\n{i}. {fig_title}\n"
|
| 158 |
-
|
| 159 |
-
# Extract x and y axis titles if available
|
| 160 |
-
x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
|
| 161 |
-
y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
|
| 162 |
-
|
| 163 |
-
content += f" - X-axis: {x_axis}\n"
|
| 164 |
-
content += f" - Y-axis: {y_axis}\n"
|
| 165 |
-
|
| 166 |
-
# If figure has data, summarize it
|
| 167 |
-
if fig.data:
|
| 168 |
-
trace_types = set(trace.type for trace in fig.data)
|
| 169 |
-
content += f" - Chart Type(s): {', '.join(trace_types)}\n"
|
| 170 |
-
else:
|
| 171 |
-
content += " - No data available in this visualization.\n"
|
| 172 |
-
|
| 173 |
-
# Return the content as a downloadable text stream
|
| 174 |
-
return io.BytesIO(content.encode("utf-8"))
|
| 175 |
-
|
| 176 |
-
|
| 177 |
# SQL-RAG Analysis
|
| 178 |
if st.session_state.df is not None:
|
| 179 |
temp_dir = tempfile.TemporaryDirectory()
|
|
@@ -220,7 +129,7 @@ if st.session_state.df is not None:
|
|
| 220 |
|
| 221 |
report_writer = Agent(
|
| 222 |
role="Technical Report Writer",
|
| 223 |
-
goal="Write a structured report with Introduction
|
| 224 |
backstory="Specializes in detailed analytical reports without conclusions.",
|
| 225 |
llm=llm,
|
| 226 |
)
|
|
@@ -247,16 +156,16 @@ if st.session_state.df is not None:
|
|
| 247 |
)
|
| 248 |
|
| 249 |
write_report = Task(
|
| 250 |
-
description="Write the analysis report with Introduction
|
| 251 |
expected_output="Markdown-formatted report excluding Conclusion.",
|
| 252 |
agent=report_writer,
|
| 253 |
context=[analyze_data],
|
| 254 |
)
|
| 255 |
|
| 256 |
write_conclusion = Task(
|
| 257 |
-
description="
|
| 258 |
-
|
| 259 |
-
expected_output="Markdown-formatted Conclusion section with key insights and statistics.",
|
| 260 |
agent=conclusion_writer,
|
| 261 |
context=[analyze_data],
|
| 262 |
)
|
|
@@ -315,7 +224,7 @@ if st.session_state.df is not None:
|
|
| 315 |
visualizations.append(fig_employment)
|
| 316 |
|
| 317 |
# Step 5: Insert Visual Insights
|
| 318 |
-
st.markdown("
|
| 319 |
for fig in visualizations:
|
| 320 |
st.plotly_chart(fig, use_container_width=True)
|
| 321 |
|
|
@@ -323,28 +232,6 @@ if st.session_state.df is not None:
|
|
| 323 |
#st.markdown("#### 6. Conclusion")
|
| 324 |
st.markdown(conclusion_result if conclusion_result else "β οΈ No Conclusion Generated.")
|
| 325 |
|
| 326 |
-
# Step 7: PDF and TXT Download Buttons for Query Insights + Viz
|
| 327 |
-
if report_result and conclusion_result and visualizations:
|
| 328 |
-
# PDF Download
|
| 329 |
-
pdf_file = create_pdf_report_with_viz(report_result, conclusion_result, visualizations)
|
| 330 |
-
with open(pdf_file.name, "rb") as f:
|
| 331 |
-
st.download_button(
|
| 332 |
-
label="π₯ Download Full Report (PDF)",
|
| 333 |
-
data=f,
|
| 334 |
-
file_name="query_insights_report.pdf",
|
| 335 |
-
mime="application/pdf"
|
| 336 |
-
)
|
| 337 |
-
|
| 338 |
-
# TXT Download
|
| 339 |
-
text_file = create_text_report_with_viz(report_result, conclusion_result, visualizations)
|
| 340 |
-
st.download_button(
|
| 341 |
-
label="π₯ Download Full Report (TXT)",
|
| 342 |
-
data=text_file,
|
| 343 |
-
file_name="query_insights_report.txt",
|
| 344 |
-
mime="text/plain"
|
| 345 |
-
)
|
| 346 |
-
|
| 347 |
-
|
| 348 |
# Full Data Visualization Tab
|
| 349 |
with tab2:
|
| 350 |
st.subheader("π Comprehensive Data Visualizations")
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import sqlite3
|
| 4 |
import os
|
|
|
|
| 5 |
import json
|
| 6 |
from pathlib import Path
|
|
|
|
|
|
|
| 7 |
import plotly.express as px
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
from crewai import Agent, Crew, Process, Task
|
|
|
|
| 83 |
st.subheader("π Dataset Preview")
|
| 84 |
st.dataframe(st.session_state.df.head())
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# SQL-RAG Analysis
|
| 87 |
if st.session_state.df is not None:
|
| 88 |
temp_dir = tempfile.TemporaryDirectory()
|
|
|
|
| 129 |
|
| 130 |
report_writer = Agent(
|
| 131 |
role="Technical Report Writer",
|
| 132 |
+
goal="Write a structured report with Introduction, Key Insights, and Analysis. DO NOT include any Conclusion or Summary.",
|
| 133 |
backstory="Specializes in detailed analytical reports without conclusions.",
|
| 134 |
llm=llm,
|
| 135 |
)
|
|
|
|
| 156 |
)
|
| 157 |
|
| 158 |
write_report = Task(
|
| 159 |
+
description="Write the analysis report with Introduction, Key Insights, and Analysis. DO NOT include any Conclusion or Summary.",
|
| 160 |
expected_output="Markdown-formatted report excluding Conclusion.",
|
| 161 |
agent=report_writer,
|
| 162 |
context=[analyze_data],
|
| 163 |
)
|
| 164 |
|
| 165 |
write_conclusion = Task(
|
| 166 |
+
description="Write a brief and impactful 3-5 line Conclusion summarizing only the most important insights/findings. Include the max, min, and average salary"
|
| 167 |
+
"and highlight the most impactful insights.",
|
| 168 |
+
expected_output="Markdown-formatted Conclusion/Summary section with key insights and statistics.",
|
| 169 |
agent=conclusion_writer,
|
| 170 |
context=[analyze_data],
|
| 171 |
)
|
|
|
|
| 224 |
visualizations.append(fig_employment)
|
| 225 |
|
| 226 |
# Step 5: Insert Visual Insights
|
| 227 |
+
st.markdown("#### 5. Visual Insights")
|
| 228 |
for fig in visualizations:
|
| 229 |
st.plotly_chart(fig, use_container_width=True)
|
| 230 |
|
|
|
|
| 232 |
#st.markdown("#### 6. Conclusion")
|
| 233 |
st.markdown(conclusion_result if conclusion_result else "β οΈ No Conclusion Generated.")
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
# Full Data Visualization Tab
|
| 236 |
with tab2:
|
| 237 |
st.subheader("π Comprehensive Data Visualizations")
|