Aka18 commited on
Commit
3727a4e
Β·
verified Β·
1 Parent(s): 0132390

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -134
app.py CHANGED
@@ -682,158 +682,63 @@ def upload_dataset():
682
  return False
683
 
684
  def run_analysis():
685
- """Run the AI analysis with beautiful progress indicators"""
 
686
  if st.session_state.dataset is None:
687
  st.warning("Please upload a dataset first.")
688
  return
689
 
690
- # Check for API key from environment or session state
 
 
691
  api_key = os.environ.get('GROQ_API_KEY') or st.session_state.get('groq_api_key')
692
  if not api_key:
693
- st.warning("Please set GROQ_API_KEY environment variable or enter it in the sidebar.")
694
  return
695
 
696
- # Create agent
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
- # Save dataset temporarily
706
- temp_file = "temp_dataset.csv"
707
- st.session_state.dataset.to_csv(temp_file, index=False)
708
-
709
- # Beautiful progress tracking
710
- progress_container = st.container()
 
 
711
 
712
- with progress_container:
713
- st.markdown("### πŸš€ AI Analysis in Progress")
714
-
715
- # Progress bar
716
- progress_bar = st.progress(0)
717
- status_text = st.empty()
718
-
719
- # Step indicators
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
- # Step 1
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"❌ Analysis failed: {results['error']}")
816
- return
817
-
818
- st.session_state.analysis_results = results
819
- st.session_state.analysis_complete = True
820
-
821
- # Success animation
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: {str(e)}")
 
 
 
 
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