Update app.py
Browse files
app.py
CHANGED
@@ -87,32 +87,6 @@ if st.session_state.df is not None and st.session_state.show_preview:
|
|
87 |
st.dataframe(st.session_state.df.head())
|
88 |
|
89 |
|
90 |
-
#def ask_gpt4o_for_visualization(query, df, llm):
|
91 |
-
# columns = ', '.join(df.columns)
|
92 |
-
# prompt = f"""
|
93 |
-
# Analyze the query and suggest one or more relevant visualizations.
|
94 |
-
# Query: "{query}"
|
95 |
-
# Available Columns: {columns}
|
96 |
-
# Respond in this JSON format (as a list if multiple suggestions):
|
97 |
-
# [
|
98 |
-
# {{
|
99 |
-
# "chart_type": "bar/box/line/scatter",
|
100 |
-
# "x_axis": "column_name",
|
101 |
-
# "y_axis": "column_name",
|
102 |
-
# "group_by": "optional_column_name"
|
103 |
-
# }}
|
104 |
-
# ]
|
105 |
-
# """
|
106 |
-
# response = llm.generate(prompt)
|
107 |
-
# try:
|
108 |
-
# return json.loads(response)
|
109 |
-
# except json.JSONDecodeError:
|
110 |
-
# st.error("⚠️ GPT-4o failed to generate a valid suggestion.")
|
111 |
-
# return None
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
# Helper Function for Validation
|
117 |
def is_valid_suggestion(suggestion):
|
118 |
chart_type = suggestion.get("chart_type", "").lower()
|
@@ -455,91 +429,6 @@ def handle_visualization_suggestions(suggestions, df):
|
|
455 |
st.plotly_chart(fig, use_container_width=True)
|
456 |
|
457 |
|
458 |
-
|
459 |
-
# Function to create TXT file
|
460 |
-
def create_text_report_with_viz_temp(report, conclusion, visualizations):
|
461 |
-
content = f"### Analysis Report\n\n{report}\n\n### Visualizations\n"
|
462 |
-
|
463 |
-
for i, fig in enumerate(visualizations, start=1):
|
464 |
-
fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
|
465 |
-
x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
|
466 |
-
y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
|
467 |
-
|
468 |
-
content += f"\n{i}. {fig_title}\n"
|
469 |
-
content += f" - X-axis: {x_axis}\n"
|
470 |
-
content += f" - Y-axis: {y_axis}\n"
|
471 |
-
|
472 |
-
if fig.data:
|
473 |
-
trace_types = set(trace.type for trace in fig.data)
|
474 |
-
content += f" - Chart Type(s): {', '.join(trace_types)}\n"
|
475 |
-
else:
|
476 |
-
content += " - No data available in this visualization.\n"
|
477 |
-
|
478 |
-
content += f"\n\n\n{conclusion}"
|
479 |
-
|
480 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w', encoding='utf-8') as temp_txt:
|
481 |
-
temp_txt.write(content)
|
482 |
-
return temp_txt.name
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
# Function to create PDF with report text and visualizations
|
487 |
-
def create_pdf_report_with_viz(report, conclusion, visualizations):
|
488 |
-
pdf = FPDF()
|
489 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
490 |
-
pdf.add_page()
|
491 |
-
pdf.set_font("Arial", size=12)
|
492 |
-
|
493 |
-
# Title
|
494 |
-
pdf.set_font("Arial", style="B", size=18)
|
495 |
-
pdf.cell(0, 10, "📊 Analysis Report", ln=True, align="C")
|
496 |
-
pdf.ln(10)
|
497 |
-
|
498 |
-
# Report Content
|
499 |
-
pdf.set_font("Arial", style="B", size=14)
|
500 |
-
pdf.cell(0, 10, "Analysis", ln=True)
|
501 |
-
pdf.set_font("Arial", size=12)
|
502 |
-
pdf.multi_cell(0, 10, report)
|
503 |
-
|
504 |
-
pdf.ln(10)
|
505 |
-
pdf.set_font("Arial", style="B", size=14)
|
506 |
-
pdf.cell(0, 10, "Conclusion", ln=True)
|
507 |
-
pdf.set_font("Arial", size=12)
|
508 |
-
pdf.multi_cell(0, 10, conclusion)
|
509 |
-
|
510 |
-
# Add Visualizations
|
511 |
-
pdf.add_page()
|
512 |
-
pdf.set_font("Arial", style="B", size=16)
|
513 |
-
pdf.cell(0, 10, "📈 Visualizations", ln=True)
|
514 |
-
pdf.ln(5)
|
515 |
-
|
516 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
517 |
-
for i, fig in enumerate(visualizations, start=1):
|
518 |
-
fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
|
519 |
-
x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
|
520 |
-
y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
|
521 |
-
|
522 |
-
# Save each visualization as a PNG image
|
523 |
-
img_path = os.path.join(temp_dir, f"viz_{i}.png")
|
524 |
-
fig.write_image(img_path)
|
525 |
-
|
526 |
-
# Insert Title and Description
|
527 |
-
pdf.set_font("Arial", style="B", size=14)
|
528 |
-
pdf.multi_cell(0, 10, f"{i}. {fig_title}")
|
529 |
-
pdf.set_font("Arial", size=12)
|
530 |
-
pdf.multi_cell(0, 10, f"X-axis: {x_axis} | Y-axis: {y_axis}")
|
531 |
-
pdf.ln(3)
|
532 |
-
|
533 |
-
# Embed Visualization
|
534 |
-
pdf.image(img_path, w=170)
|
535 |
-
pdf.ln(10)
|
536 |
-
|
537 |
-
# Save PDF
|
538 |
-
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
539 |
-
pdf.output(temp_pdf.name)
|
540 |
-
|
541 |
-
return temp_pdf
|
542 |
-
|
543 |
def escape_markdown(text):
|
544 |
# Ensure text is a string
|
545 |
text = str(text)
|
@@ -684,28 +573,6 @@ if st.session_state.df is not None:
|
|
684 |
safe_conclusion = escape_markdown(conclusion_result if conclusion_result else "⚠️ No Conclusion Generated.")
|
685 |
st.markdown(safe_conclusion)
|
686 |
|
687 |
-
# Full Data Visualization Tab
|
688 |
-
with tab2:
|
689 |
-
st.subheader("📊 Comprehensive Data Visualizations")
|
690 |
-
|
691 |
-
fig1 = px.histogram(st.session_state.df, x="job_title", title="Job Title Frequency")
|
692 |
-
st.plotly_chart(fig1)
|
693 |
-
|
694 |
-
fig2 = px.bar(
|
695 |
-
st.session_state.df.groupby("experience_level")["salary_in_usd"].mean().reset_index(),
|
696 |
-
x="experience_level", y="salary_in_usd",
|
697 |
-
title="Average Salary by Experience Level"
|
698 |
-
)
|
699 |
-
st.plotly_chart(fig2)
|
700 |
-
|
701 |
-
fig3 = px.box(st.session_state.df, x="employment_type", y="salary_in_usd",
|
702 |
-
title="Salary Distribution by Employment Type")
|
703 |
-
st.plotly_chart(fig3)
|
704 |
-
|
705 |
-
temp_dir.cleanup()
|
706 |
-
else:
|
707 |
-
st.info("Please load a dataset to proceed.")
|
708 |
-
|
709 |
|
710 |
# Sidebar Reference
|
711 |
with st.sidebar:
|
|
|
87 |
st.dataframe(st.session_state.df.head())
|
88 |
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
# Helper Function for Validation
|
91 |
def is_valid_suggestion(suggestion):
|
92 |
chart_type = suggestion.get("chart_type", "").lower()
|
|
|
429 |
st.plotly_chart(fig, use_container_width=True)
|
430 |
|
431 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
def escape_markdown(text):
|
433 |
# Ensure text is a string
|
434 |
text = str(text)
|
|
|
573 |
safe_conclusion = escape_markdown(conclusion_result if conclusion_result else "⚠️ No Conclusion Generated.")
|
574 |
st.markdown(safe_conclusion)
|
575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
576 |
|
577 |
# Sidebar Reference
|
578 |
with st.sidebar:
|