import streamlit as st import pandas as pd from utils import ( validate_csv, analyze_data, get_numeric_columns, get_categorical_columns, calculate_basic_stats ) from dotenv import load_dotenv import os from pathlib import Path import plotly.express as px import plotly.graph_objects as go import base64 def load_image_as_base64(image_path): with open(image_path, "rb") as img_file: return base64.b64encode(img_file.read()).decode() st.set_page_config( page_title="SQL AI - SpiderX", page_icon="assets/images/small-icon-black.png", layout="wide" ) st.sidebar.image("assets/images/logo.png", width=300) # Create data directory if it doesn't exist data_dir = Path("data") data_dir.mkdir(exist_ok=True) # Load all CSV files automatically at startup #st.header("1. Data Files") # Get list of all CSV files in data directory csv_files = list(data_dir.glob("*.csv")) if not csv_files: st.warning("No CSV files found in the 'data' directory. Please add CSV files to the 'data' folder.") st.stop() # Automatically process all CSV files st.session_state.selected_files = {} for file_path in csv_files: with file_path.open('rb') as f: is_valid, result = validate_csv(f) if is_valid: st.session_state.selected_files[file_path.name] = result else: st.error(f"Error in {file_path.name}: {result}") #st.success("f"✅ {len(st.session_state.selected_files)} files loaded successfully!"") # Display loaded files #for filename in st.session_state.selected_files.keys(): #st.write(f"- {filename}") def plot_histogram(df, column): """Create a histogram for numeric data from files""" fig = px.histogram(df, x=column, title=f'Distribution of {column}') return fig def plot_box(df, column): """Create a box plot for numeric data""" fig = px.box(df, y=column, title=f'Box Plot of {column}') return fig def plot_scatter(df, x_col, y_col): """Create a scatter plot for two numeric columns""" fig = px.scatter(df, x=x_col, y=y_col, title=f'{x_col} vs {y_col}') return fig def main(): st.title("SQL AI - SpiderX") load_dotenv() # Load environment variables from .env api_key = os.getenv("OPENAI_API_KEY") if not api_key: st.error("⚠️ OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.") return # Data preview section if st.session_state.selected_files: tab1, tab2 = st.tabs(["SQL AI", "Data Preview"]) with tab2: st.header("Data Preview") total_files = len(st.session_state.selected_files) st.info(f""" 🔍 Analysis Details: - Total files loaded: {total_files} - All files will be included in the analysis """) for filename, df in st.session_state.selected_files.items(): with st.expander(f"Preview: {filename}"): st.dataframe(df.head(5), use_container_width=True) st.text(f"Shape: {df.shape[0]} rows × {df.shape[1]} columns") with tab1: st.header("1. Ask Questions About Your Data") question = st.text_area( "Enter your question", placeholder="Example: What is the average value in column X? Compare data across all files.", help="Ask any question about your data - the AI will analyze all selected files" ) if st.button("Analyze", type="primary"): if question: with st.spinner("Analyzing your data..."): success, response, prompt = analyze_data(question, st.session_state.selected_files) if success: st.header("Analysis Results") st.write(response) else: st.error(response) else: st.warning("Please enter a question to analyze.") else: st.info("👆 CSV files are preloaded automatically. Please check the 'data' folder.") # Footer st.markdown("---") st.markdown("""

Built with SpiderX AI

""", unsafe_allow_html=True) if __name__ == "__main__": main()