Update app.py
Browse files
app.py
CHANGED
|
@@ -682,158 +682,63 @@ def upload_dataset():
|
|
| 682 |
return False
|
| 683 |
|
| 684 |
def run_analysis():
|
| 685 |
-
"
|
|
|
|
| 686 |
if st.session_state.dataset is None:
|
| 687 |
st.warning("Please upload a dataset first.")
|
| 688 |
return
|
| 689 |
|
| 690 |
-
|
|
|
|
|
|
|
| 691 |
api_key = os.environ.get('GROQ_API_KEY') or st.session_state.get('groq_api_key')
|
| 692 |
if not api_key:
|
| 693 |
-
st.
|
| 694 |
return
|
| 695 |
|
| 696 |
-
|
| 697 |
-
with st.spinner("π€ Initializing AI agent..."):
|
| 698 |
-
agent = create_agent()
|
| 699 |
-
if agent is None:
|
| 700 |
-
st.error("Failed to initialize AI agent. Check your API key.")
|
| 701 |
-
return
|
| 702 |
-
|
| 703 |
-
st.session_state.agent = agent
|
| 704 |
|
| 705 |
-
#
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
|
|
|
|
|
|
| 711 |
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
steps = [
|
| 721 |
-
("π", "Analyzing dataset structure"),
|
| 722 |
-
("π", "Examining columns and data quality"),
|
| 723 |
-
("π§ ", "Generating AI insights"),
|
| 724 |
-
("π", "Planning visualizations"),
|
| 725 |
-
("π¨", "Creating charts"),
|
| 726 |
-
("π―", "Formulating recommendations")
|
| 727 |
-
]
|
| 728 |
-
|
| 729 |
-
step_cols = st.columns(len(steps))
|
| 730 |
-
step_indicators = []
|
| 731 |
-
|
| 732 |
-
for i, (icon, desc) in enumerate(steps):
|
| 733 |
-
with step_cols[i]:
|
| 734 |
-
step_indicators.append(st.empty())
|
| 735 |
-
step_indicators[i].markdown(f"""
|
| 736 |
-
<div style="text-align: center; padding: 1rem; opacity: 0.3;">
|
| 737 |
-
<div style="font-size: 2rem;">{icon}</div>
|
| 738 |
-
<div style="font-size: 0.8rem; margin-top: 0.5rem;">{desc}</div>
|
| 739 |
-
</div>
|
| 740 |
-
""", unsafe_allow_html=True)
|
| 741 |
|
|
|
|
| 742 |
try:
|
| 743 |
-
|
| 744 |
-
step_indicators[0].markdown(f"""
|
| 745 |
-
<div style="text-align: center; padding: 1rem; opacity: 1; background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border-radius: 12px;">
|
| 746 |
-
<div style="font-size: 2rem;">π</div>
|
| 747 |
-
<div style="font-size: 0.8rem; margin-top: 0.5rem; font-weight: 600;">Analyzing Structure</div>
|
| 748 |
-
</div>
|
| 749 |
-
""", unsafe_allow_html=True)
|
| 750 |
-
status_text.markdown("**π AI agent analyzing dataset structure...**")
|
| 751 |
-
progress_bar.progress(15)
|
| 752 |
-
time.sleep(1)
|
| 753 |
-
|
| 754 |
-
# Step 2
|
| 755 |
-
step_indicators[1].markdown(f"""
|
| 756 |
-
<div style="text-align: center; padding: 1rem; opacity: 1; background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border-radius: 12px;">
|
| 757 |
-
<div style="font-size: 2rem;">π</div>
|
| 758 |
-
<div style="font-size: 0.8rem; margin-top: 0.5rem; font-weight: 600;">Examining Data</div>
|
| 759 |
-
</div>
|
| 760 |
-
""", unsafe_allow_html=True)
|
| 761 |
-
status_text.markdown("**π Analyzing columns and data quality...**")
|
| 762 |
-
progress_bar.progress(30)
|
| 763 |
-
time.sleep(1)
|
| 764 |
-
|
| 765 |
-
# Step 3
|
| 766 |
-
step_indicators[2].markdown(f"""
|
| 767 |
-
<div style="text-align: center; padding: 1rem; opacity: 1; background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border-radius: 12px;">
|
| 768 |
-
<div style="font-size: 2rem;">π§ </div>
|
| 769 |
-
<div style="font-size: 0.8rem; margin-top: 0.5rem; font-weight: 600;">AI Thinking</div>
|
| 770 |
-
</div>
|
| 771 |
-
""", unsafe_allow_html=True)
|
| 772 |
-
status_text.markdown("**π§ Generating insights with AI...**")
|
| 773 |
-
progress_bar.progress(50)
|
| 774 |
-
time.sleep(1)
|
| 775 |
-
|
| 776 |
-
# Step 4
|
| 777 |
-
step_indicators[3].markdown(f"""
|
| 778 |
-
<div style="text-align: center; padding: 1rem; opacity: 1; background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border-radius: 12px;">
|
| 779 |
-
<div style="font-size: 2rem;">π</div>
|
| 780 |
-
<div style="font-size: 0.8rem; margin-top: 0.5rem; font-weight: 600;">Planning Charts</div>
|
| 781 |
-
</div>
|
| 782 |
-
""", unsafe_allow_html=True)
|
| 783 |
-
status_text.markdown("**π Planning optimal visualizations...**")
|
| 784 |
-
progress_bar.progress(70)
|
| 785 |
-
time.sleep(1)
|
| 786 |
-
|
| 787 |
-
# Step 5
|
| 788 |
-
step_indicators[4].markdown(f"""
|
| 789 |
-
<div style="text-align: center; padding: 1rem; opacity: 1; background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border-radius: 12px;">
|
| 790 |
-
<div style="font-size: 2rem;">π¨</div>
|
| 791 |
-
<div style="font-size: 0.8rem; margin-top: 0.5rem; font-weight: 600;">Creating Charts</div>
|
| 792 |
-
</div>
|
| 793 |
-
""", unsafe_allow_html=True)
|
| 794 |
-
status_text.markdown("**π¨ Creating beautiful visualizations...**")
|
| 795 |
-
progress_bar.progress(85)
|
| 796 |
-
|
| 797 |
-
# Run the actual analysis
|
| 798 |
results = agent.analyze_dataset(temp_file)
|
| 799 |
-
|
| 800 |
-
# Step 6
|
| 801 |
-
step_indicators[5].markdown(f"""
|
| 802 |
-
<div style="text-align: center; padding: 1rem; opacity: 1; background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border-radius: 12px;">
|
| 803 |
-
<div style="font-size: 2rem;">π―</div>
|
| 804 |
-
<div style="font-size: 0.8rem; margin-top: 0.5rem; font-weight: 600;">Final Recommendations</div>
|
| 805 |
-
</div>
|
| 806 |
-
""", unsafe_allow_html=True)
|
| 807 |
-
status_text.markdown("**π― Formulating actionable recommendations...**")
|
| 808 |
-
progress_bar.progress(100)
|
| 809 |
-
|
| 810 |
-
# Clean up temp file
|
| 811 |
-
if os.path.exists(temp_file):
|
| 812 |
-
os.remove(temp_file)
|
| 813 |
|
| 814 |
if "error" in results:
|
| 815 |
-
st.error(f"
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
status_text.markdown("**β
Analysis completed successfully!**")
|
| 823 |
-
|
| 824 |
-
# Show completion message
|
| 825 |
-
st.balloons()
|
| 826 |
-
time.sleep(1)
|
| 827 |
-
|
| 828 |
-
# Clear progress and show results
|
| 829 |
-
progress_container.empty()
|
| 830 |
-
st.rerun()
|
| 831 |
-
|
| 832 |
except Exception as e:
|
| 833 |
-
st.error(f"β Analysis failed: {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 834 |
if os.path.exists(temp_file):
|
| 835 |
os.remove(temp_file)
|
| 836 |
-
|
| 837 |
def display_results():
|
| 838 |
"""Display beautiful analysis results"""
|
| 839 |
results = st.session_state.analysis_results
|
|
|
|
| 682 |
return False
|
| 683 |
|
| 684 |
def run_analysis():
|
| 685 |
+
st.write("π DEBUG: Starting analysis...")
|
| 686 |
+
|
| 687 |
if st.session_state.dataset is None:
|
| 688 |
st.warning("Please upload a dataset first.")
|
| 689 |
return
|
| 690 |
|
| 691 |
+
st.write(f"π DEBUG: Dataset shape: {st.session_state.dataset.shape}")
|
| 692 |
+
|
| 693 |
+
# Test API key
|
| 694 |
api_key = os.environ.get('GROQ_API_KEY') or st.session_state.get('groq_api_key')
|
| 695 |
if not api_key:
|
| 696 |
+
st.error("No API key found")
|
| 697 |
return
|
| 698 |
|
| 699 |
+
st.write("π DEBUG: API key found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 700 |
|
| 701 |
+
# Test agent creation
|
| 702 |
+
try:
|
| 703 |
+
st.write("π DEBUG: Creating agent...")
|
| 704 |
+
agent = create_agent()
|
| 705 |
+
st.write(f"π DEBUG: Agent created: {agent is not None}")
|
| 706 |
+
except Exception as e:
|
| 707 |
+
st.error(f"β Agent creation failed: {e}")
|
| 708 |
+
return
|
| 709 |
|
| 710 |
+
# Test dataset save
|
| 711 |
+
try:
|
| 712 |
+
temp_file = "temp_dataset.csv"
|
| 713 |
+
st.session_state.dataset.to_csv(temp_file, index=False)
|
| 714 |
+
st.write("π DEBUG: Dataset saved successfully")
|
| 715 |
+
except Exception as e:
|
| 716 |
+
st.error(f"β Dataset save failed: {e}")
|
| 717 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
|
| 719 |
+
# Test analysis
|
| 720 |
try:
|
| 721 |
+
st.write("π DEBUG: Starting agent analysis...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 722 |
results = agent.analyze_dataset(temp_file)
|
| 723 |
+
st.write(f"π DEBUG: Analysis complete: {type(results)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
|
| 725 |
if "error" in results:
|
| 726 |
+
st.error(f"Analysis error: {results['error']}")
|
| 727 |
+
else:
|
| 728 |
+
st.success("β
Analysis successful!")
|
| 729 |
+
st.session_state.analysis_results = results
|
| 730 |
+
st.session_state.analysis_complete = True
|
| 731 |
+
st.rerun()
|
| 732 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
except Exception as e:
|
| 734 |
+
st.error(f"β Analysis failed: {e}")
|
| 735 |
+
import traceback
|
| 736 |
+
st.code(traceback.format_exc())
|
| 737 |
+
|
| 738 |
+
finally:
|
| 739 |
if os.path.exists(temp_file):
|
| 740 |
os.remove(temp_file)
|
| 741 |
+
|
| 742 |
def display_results():
|
| 743 |
"""Display beautiful analysis results"""
|
| 744 |
results = st.session_state.analysis_results
|