Spaces:
Sleeping
Sleeping
Update interim.py
Browse files- interim.py +107 -8
interim.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 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
|
|
@@ -83,6 +86,96 @@ if st.session_state.df is not None and st.session_state.show_preview:
|
|
| 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,7 +222,7 @@ if st.session_state.df is not None:
|
|
| 129 |
|
| 130 |
report_writer = Agent(
|
| 131 |
role="Technical Report Writer",
|
| 132 |
-
goal="Write a structured report with Key Insights
|
| 133 |
backstory="Specializes in detailed analytical reports without conclusions.",
|
| 134 |
llm=llm,
|
| 135 |
)
|
|
@@ -156,19 +249,22 @@ if st.session_state.df is not None:
|
|
| 156 |
)
|
| 157 |
|
| 158 |
write_report = Task(
|
| 159 |
-
description="Write the analysis report with Key Insights. DO NOT include
|
| 160 |
expected_output="Markdown-formatted report excluding Conclusion.",
|
| 161 |
agent=report_writer,
|
| 162 |
context=[analyze_data],
|
| 163 |
)
|
| 164 |
|
| 165 |
write_conclusion = Task(
|
| 166 |
-
description="
|
| 167 |
-
|
|
|
|
| 168 |
agent=conclusion_writer,
|
| 169 |
context=[analyze_data],
|
| 170 |
)
|
| 171 |
|
|
|
|
|
|
|
| 172 |
# Separate Crews for report and conclusion
|
| 173 |
crew_report = Crew(
|
| 174 |
agents=[sql_dev, data_analyst, report_writer],
|
|
@@ -201,7 +297,7 @@ if st.session_state.df is not None:
|
|
| 201 |
conclusion_result = crew_conclusion.kickoff(inputs=conclusion_inputs)
|
| 202 |
|
| 203 |
# Step 3: Display the report
|
| 204 |
-
st.markdown("### Analysis Report:")
|
| 205 |
st.markdown(report_result if report_result else "β οΈ No Report Generated.")
|
| 206 |
|
| 207 |
# Step 4: Generate Visualizations
|
|
@@ -223,13 +319,15 @@ if st.session_state.df is not None:
|
|
| 223 |
visualizations.append(fig_employment)
|
| 224 |
|
| 225 |
# Step 5: Insert Visual Insights
|
| 226 |
-
st.markdown("
|
| 227 |
for fig in visualizations:
|
| 228 |
st.plotly_chart(fig, use_container_width=True)
|
| 229 |
|
| 230 |
# Step 6: Display Concise Conclusion
|
| 231 |
-
st.markdown("
|
| 232 |
-
|
|
|
|
|
|
|
| 233 |
|
| 234 |
# Full Data Visualization Tab
|
| 235 |
with tab2:
|
|
@@ -258,3 +356,4 @@ else:
|
|
| 258 |
with st.sidebar:
|
| 259 |
st.header("π Reference:")
|
| 260 |
st.markdown("[SQL Agents w CrewAI & Llama 3 - Plaban Nayak](https://github.com/plaban1981/Agents/blob/main/SQL_Agents_with_CrewAI_and_Llama_3.ipynb)")
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import sqlite3
|
| 4 |
+
import tempfile
|
| 5 |
+
from fpdf import FPDF
|
| 6 |
import os
|
| 7 |
+
import re
|
| 8 |
import json
|
| 9 |
from pathlib import Path
|
| 10 |
import plotly.express as px
|
|
|
|
| 86 |
st.subheader("π Dataset Preview")
|
| 87 |
st.dataframe(st.session_state.df.head())
|
| 88 |
|
| 89 |
+
# Function to create TXT file
|
| 90 |
+
def create_text_report_with_viz_temp(report, conclusion, visualizations):
|
| 91 |
+
content = f"### Analysis Report\n\n{report}\n\n### Visualizations\n"
|
| 92 |
+
|
| 93 |
+
for i, fig in enumerate(visualizations, start=1):
|
| 94 |
+
fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
|
| 95 |
+
x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
|
| 96 |
+
y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
|
| 97 |
+
|
| 98 |
+
content += f"\n{i}. {fig_title}\n"
|
| 99 |
+
content += f" - X-axis: {x_axis}\n"
|
| 100 |
+
content += f" - Y-axis: {y_axis}\n"
|
| 101 |
+
|
| 102 |
+
if fig.data:
|
| 103 |
+
trace_types = set(trace.type for trace in fig.data)
|
| 104 |
+
content += f" - Chart Type(s): {', '.join(trace_types)}\n"
|
| 105 |
+
else:
|
| 106 |
+
content += " - No data available in this visualization.\n"
|
| 107 |
+
|
| 108 |
+
content += f"\n\n\n{conclusion}"
|
| 109 |
+
|
| 110 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w', encoding='utf-8') as temp_txt:
|
| 111 |
+
temp_txt.write(content)
|
| 112 |
+
return temp_txt.name
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Function to create PDF with report text and visualizations
|
| 116 |
+
def create_pdf_report_with_viz(report, conclusion, visualizations):
|
| 117 |
+
pdf = FPDF()
|
| 118 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 119 |
+
pdf.add_page()
|
| 120 |
+
pdf.set_font("Arial", size=12)
|
| 121 |
+
|
| 122 |
+
# Title
|
| 123 |
+
pdf.set_font("Arial", style="B", size=18)
|
| 124 |
+
pdf.cell(0, 10, "π Analysis Report", ln=True, align="C")
|
| 125 |
+
pdf.ln(10)
|
| 126 |
+
|
| 127 |
+
# Report Content
|
| 128 |
+
pdf.set_font("Arial", style="B", size=14)
|
| 129 |
+
pdf.cell(0, 10, "Analysis", ln=True)
|
| 130 |
+
pdf.set_font("Arial", size=12)
|
| 131 |
+
pdf.multi_cell(0, 10, report)
|
| 132 |
+
|
| 133 |
+
pdf.ln(10)
|
| 134 |
+
pdf.set_font("Arial", style="B", size=14)
|
| 135 |
+
pdf.cell(0, 10, "Conclusion", ln=True)
|
| 136 |
+
pdf.set_font("Arial", size=12)
|
| 137 |
+
pdf.multi_cell(0, 10, conclusion)
|
| 138 |
+
|
| 139 |
+
# Add Visualizations
|
| 140 |
+
pdf.add_page()
|
| 141 |
+
pdf.set_font("Arial", style="B", size=16)
|
| 142 |
+
pdf.cell(0, 10, "π Visualizations", ln=True)
|
| 143 |
+
pdf.ln(5)
|
| 144 |
+
|
| 145 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 146 |
+
for i, fig in enumerate(visualizations, start=1):
|
| 147 |
+
fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
|
| 148 |
+
x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
|
| 149 |
+
y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
|
| 150 |
+
|
| 151 |
+
# Save each visualization as a PNG image
|
| 152 |
+
img_path = os.path.join(temp_dir, f"viz_{i}.png")
|
| 153 |
+
fig.write_image(img_path)
|
| 154 |
+
|
| 155 |
+
# Insert Title and Description
|
| 156 |
+
pdf.set_font("Arial", style="B", size=14)
|
| 157 |
+
pdf.multi_cell(0, 10, f"{i}. {fig_title}")
|
| 158 |
+
pdf.set_font("Arial", size=12)
|
| 159 |
+
pdf.multi_cell(0, 10, f"X-axis: {x_axis} | Y-axis: {y_axis}")
|
| 160 |
+
pdf.ln(3)
|
| 161 |
+
|
| 162 |
+
# Embed Visualization
|
| 163 |
+
pdf.image(img_path, w=170)
|
| 164 |
+
pdf.ln(10)
|
| 165 |
+
|
| 166 |
+
# Save PDF
|
| 167 |
+
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 168 |
+
pdf.output(temp_pdf.name)
|
| 169 |
+
|
| 170 |
+
return temp_pdf
|
| 171 |
+
|
| 172 |
+
def escape_markdown(text):
|
| 173 |
+
# Ensure text is a string
|
| 174 |
+
text = str(text)
|
| 175 |
+
# Escape Markdown characters: *, _, `, ~
|
| 176 |
+
escape_chars = r"(\*|_|`|~)"
|
| 177 |
+
return re.sub(escape_chars, r"\\\1", text)
|
| 178 |
+
|
| 179 |
# SQL-RAG Analysis
|
| 180 |
if st.session_state.df is not None:
|
| 181 |
temp_dir = tempfile.TemporaryDirectory()
|
|
|
|
| 222 |
|
| 223 |
report_writer = Agent(
|
| 224 |
role="Technical Report Writer",
|
| 225 |
+
goal="Write a structured report with Introduction and Key Insights. DO NOT include any Conclusion or Summary.",
|
| 226 |
backstory="Specializes in detailed analytical reports without conclusions.",
|
| 227 |
llm=llm,
|
| 228 |
)
|
|
|
|
| 249 |
)
|
| 250 |
|
| 251 |
write_report = Task(
|
| 252 |
+
description="Write the analysis report with Introduction and Key Insights. DO NOT include any Conclusion or Summary.",
|
| 253 |
expected_output="Markdown-formatted report excluding Conclusion.",
|
| 254 |
agent=report_writer,
|
| 255 |
context=[analyze_data],
|
| 256 |
)
|
| 257 |
|
| 258 |
write_conclusion = Task(
|
| 259 |
+
description="Summarize the key findings in 3-5 impactful lines, highlighting the maximum, minimum, and average salaries."
|
| 260 |
+
"Emphasize significant insights on salary distribution and influential compensation trends for strategic decision-making.",
|
| 261 |
+
expected_output="Markdown-formatted Conclusion section with key insights and statistics.",
|
| 262 |
agent=conclusion_writer,
|
| 263 |
context=[analyze_data],
|
| 264 |
)
|
| 265 |
|
| 266 |
+
|
| 267 |
+
|
| 268 |
# Separate Crews for report and conclusion
|
| 269 |
crew_report = Crew(
|
| 270 |
agents=[sql_dev, data_analyst, report_writer],
|
|
|
|
| 297 |
conclusion_result = crew_conclusion.kickoff(inputs=conclusion_inputs)
|
| 298 |
|
| 299 |
# Step 3: Display the report
|
| 300 |
+
#st.markdown("### Analysis Report:")
|
| 301 |
st.markdown(report_result if report_result else "β οΈ No Report Generated.")
|
| 302 |
|
| 303 |
# Step 4: Generate Visualizations
|
|
|
|
| 319 |
visualizations.append(fig_employment)
|
| 320 |
|
| 321 |
# Step 5: Insert Visual Insights
|
| 322 |
+
st.markdown("### Visual Insights")
|
| 323 |
for fig in visualizations:
|
| 324 |
st.plotly_chart(fig, use_container_width=True)
|
| 325 |
|
| 326 |
# Step 6: Display Concise Conclusion
|
| 327 |
+
#st.markdown("#### Conclusion")
|
| 328 |
+
|
| 329 |
+
safe_conclusion = escape_markdown(conclusion_result if conclusion_result else "β οΈ No Conclusion Generated.")
|
| 330 |
+
st.markdown(safe_conclusion)
|
| 331 |
|
| 332 |
# Full Data Visualization Tab
|
| 333 |
with tab2:
|
|
|
|
| 356 |
with st.sidebar:
|
| 357 |
st.header("π Reference:")
|
| 358 |
st.markdown("[SQL Agents w CrewAI & Llama 3 - Plaban Nayak](https://github.com/plaban1981/Agents/blob/main/SQL_Agents_with_CrewAI_and_Llama_3.ipynb)")
|
| 359 |
+
|