whackthejacker/DataHubHub
1
1import streamlit as st2import pandas as pd3import json4import io5from utils.dataset_utils import get_dataset_info, detect_dataset_format6 7def render_dataset_uploader():8 """9 Renders the dataset upload component that supports CSV and JSON formats.10 """11 st.markdown("""12 <div class="upload-container">13 <p>Upload your dataset in CSV or JSON format</p>14 </div>15 """, unsafe_allow_html=True)16 17 # File uploader18 uploaded_file = st.file_uploader(19 "Choose a file", 20 type=["csv", "json"], 21 help="Upload a CSV or JSON file containing your dataset"22 )23 24 # Sample dataset option25 st.markdown("Or use a sample dataset:")26 sample_dataset = st.selectbox(27 "Select a sample dataset",28 ["None", "Iris Dataset", "Titanic Dataset", "Boston Housing Dataset"]29 )30 31 # Process uploaded file32 if uploaded_file is not None:33 try:34 # Check file extension35 file_extension = uploaded_file.name.split(".")[-1].lower()36 37 if file_extension == "csv":38 df = pd.read_csv(uploaded_file)39 dataset_type = "csv"40 elif file_extension == "json":41 # Try different JSON formats42 try:43 # First try parsing as a regular JSON with records orientation44 df = pd.read_json(uploaded_file)45 dataset_type = "json"46 except:47 # If that fails, try to parse as JSON Lines48 try:49 df = pd.read_json(uploaded_file, lines=True)50 dataset_type = "jsonl"51 except:52 # If that also fails, load raw JSON and convert53 content = json.loads(uploaded_file.getvalue().decode("utf-8"))54 if isinstance(content, list):55 df = pd.DataFrame(content)56 elif isinstance(content, dict):57 # Handle nested dict structures58 if any(isinstance(v, list) for v in content.values()):59 # Find the list field and use it60 for key, value in content.items():61 if isinstance(value, list):62 df = pd.DataFrame(value)63 break64 else:65 # Flat dict or dict of dicts66 df = pd.DataFrame([content])67 dataset_type = "json"68 else:69 st.error(f"Unsupported file format: {file_extension}")70 return71 72 # Store dataset and its info in session state73 st.session_state.dataset = df74 st.session_state.dataset_name = uploaded_file.name75 st.session_state.dataset_type = dataset_type76 st.session_state.dataset_info = get_dataset_info(df)77 78 except Exception as e:79 st.error(f"Error loading dataset: {str(e)}")80 81 # Process sample dataset82 elif sample_dataset != "None":83 try:84 if sample_dataset == "Iris Dataset":85 # Load Iris dataset86 from sklearn.datasets import load_iris87 iris = load_iris()88 df = pd.DataFrame(data=iris.data, columns=iris.feature_names)89 df['target'] = iris.target90 dataset_type = "csv"91 92 elif sample_dataset == "Titanic Dataset":93 # URL for Titanic dataset94 url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"95 df = pd.read_csv(url)96 dataset_type = "csv"97 98 elif sample_dataset == "Boston Housing Dataset":99 # Load Boston Housing dataset100 from sklearn.datasets import fetch_california_housing101 housing = fetch_california_housing()102 df = pd.DataFrame(data=housing.data, columns=housing.feature_names)103 df['target'] = housing.target104 dataset_type = "csv"105 106 # Store dataset and its info in session state107 st.session_state.dataset = df108 st.session_state.dataset_name = sample_dataset109 st.session_state.dataset_type = dataset_type110 st.session_state.dataset_info = get_dataset_info(df)111 112 except Exception as e:113 st.error(f"Error loading sample dataset: {str(e)}")114 