import pandas as pd import streamlit as st import matplotlib.pyplot as plt # Function to load and process the CSV data def load_data(file): try: df = pd.read_csv(file) # Automatically detect the datetime column or ask the user to specify it timestamp_col = None for col in df.columns: if 'date' in col.lower() or 'time' in col.lower(): timestamp_col = col break if timestamp_col is None: timestamp_col = st.selectbox("Select the column containing the timestamp:", df.columns.tolist()) if timestamp_col: df['timestamp'] = pd.to_datetime(df[timestamp_col], errors='coerce') # Convert to datetime if df['timestamp'].isnull().any(): st.error("There are invalid or missing date/time values in the selected column.") return None, None # Show available columns for traffic data selection st.write(f"Available columns: {df.columns.tolist()}") traffic_column = st.selectbox("Select the column containing traffic flow data:", df.columns.tolist()) return df, traffic_column else: st.error("No valid timestamp column found in the dataset.") return None, None except Exception as e: st.error(f"Error loading file: {e}") return None, None # Function to generate peak traffic hour def peak_traffic_hour(df, traffic_column): df['hour'] = df['timestamp'].dt.hour # Extract hour from timestamp traffic_by_hour = df.groupby('hour')[traffic_column].sum() # Sum the traffic flow per hour peak_hour = traffic_by_hour.idxmax() # Find the hour with the maximum traffic peak_traffic = traffic_by_hour.max() # Find the traffic flow for that peak hour return peak_hour, peak_traffic # Function to generate hourly traffic summary def hourly_traffic_summary(df, traffic_column): df['hour'] = df['timestamp'].dt.hour df[traffic_column] = pd.to_numeric(df[traffic_column], errors='coerce') # Ensure numeric data hourly_summary = df.groupby('hour')[traffic_column].sum().reset_index() # Aggregate by hour return hourly_summary # Initialize Streamlit app def main(): st.title("Traffic Flow Analyzer") # Upload CSV file uploaded_file = st.file_uploader("Upload Traffic Data (CSV)", type=["csv"]) if uploaded_file is not None: df, traffic_column = load_data(uploaded_file) if df is not None and traffic_column is not None: st.write("Data loaded successfully! Now, you can ask your questions.") st.write(df.head()) # Display the first few rows of the dataset for user reference # Ask the user what they want to know user_question = st.text_input("Ask a question about the traffic data (e.g., 'What is the peak traffic hour?')") if user_question: user_question = user_question.lower() if "peak traffic hour" in user_question: peak_hour, peak_traffic = peak_traffic_hour(df, traffic_column) st.write(f"The peak traffic hour is {peak_hour}:00 with a total traffic flow of {peak_traffic} vehicles.") elif "hourly traffic summary" in user_question: hourly_summary = hourly_traffic_summary(df, traffic_column) st.write("Total traffic flow per hour:") st.write(hourly_summary) else: st.write("Sorry, I couldn't understand your question. Please try asking something else.") if __name__ == "__main__": main()