Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| from datetime import datetime | |
| import folium | |
| from streamlit_folium import folium_static | |
| import groq | |
| # Load bus data | |
| data_path = r"C:\Users\Muthuraja\OneDrive\Attachments\Desktop\second\pdp.csv" | |
| df = pd.read_csv(data_path) | |
| # Dummy user credentials | |
| USER_CREDENTIALS = { | |
| "Muthuraja":"virat", | |
| "Praveen":"dhoni", | |
| "Pandi":"kabadi", | |
| "admin": "password123", | |
| "user": "buspass2025" | |
| } | |
| # Groq API Key | |
| GROQ_API_KEY = "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" | |
| groq.api_key = GROQ_API_KEY | |
| # User login | |
| def authenticate(username, password): | |
| if username in USER_CREDENTIALS and USER_CREDENTIALS[username] == password: | |
| return True | |
| return False | |
| # Generate bus prediction using Groq API | |
| def predict_bus_status(bus_number, city, area): | |
| prompt = f"Predict the status and arrival time for bus {bus_number} in {city}, {area}." | |
| try: | |
| client = groq.Client(api_key=GROQ_API_KEY) | |
| response = client.chat.completions.create( | |
| model="llama3-70b-8192", # Correct model name for Groq # Use Groq's model, adjust if needed | |
| messages=[{"role": "system", "content": prompt}] | |
| ) | |
| return response.choices[0].message.content.strip() | |
| except Exception as e: | |
| return f"Error: {e}" | |
| # Plot bus locations using Folium with real coordinates | |
| def plot_bus_map(area_df): | |
| if area_df.empty: | |
| st.warning("No bus data available for this area.") | |
| return | |
| if "latitude" not in area_df.columns or "longitude" not in area_df.columns: | |
| st.error("Latitude and longitude columns missing. Please update CSV.") | |
| return | |
| center_lat = area_df["latitude"].mean() | |
| center_lon = area_df["longitude"].mean() | |
| m = folium.Map(location=[center_lat, center_lon], zoom_start=12) | |
| for _, row in area_df.iterrows(): | |
| folium.Marker( | |
| location=[row["latitude"], row["longitude"]], | |
| popup=f"{row['bus_number']} - {row['bus_route']}\nArrival: {row['arrival_time']}\nStatus: {row['status']}", | |
| icon=folium.Icon(color="blue" if row["status"].lower() == "on time" else "red") | |
| ).add_to(m) | |
| folium_static(m) | |
| # Streamlit UI | |
| st.title("๐ Tamil Nadu Bus Tracking & Prediction System") | |
| # Login form | |
| if "authenticated" not in st.session_state: | |
| st.session_state.authenticated = False | |
| if not st.session_state.authenticated: | |
| username = st.text_input("Username") | |
| password = st.text_input("Password", type="password") | |
| if st.button("Login"): | |
| if authenticate(username, password): | |
| st.session_state.authenticated = True | |
| st.success("Login successful!") | |
| else: | |
| st.error("Invalid username or password") | |
| if st.session_state.authenticated: | |
| city = st.selectbox("Select City", df["city"].unique()) | |
| area = st.selectbox("Select Area", df[df["city"] == city]["area"].unique()) | |
| filtered_df = df[(df["city"] == city) & (df["area"] == area)] | |
| st.subheader(f"๐ Bus Details for {city}, {area}") | |
| st.dataframe(filtered_df) | |
| st.subheader("๐บ๏ธ Bus Map View") | |
| plot_bus_map(filtered_df) | |
| def predict_next_bus(area_df): | |
| now = datetime.now() | |
| upcoming_buses = area_df[area_df["arrival_time"] > now.strftime("%Y-%m-%d %H:%M:%S")] | |
| if not upcoming_buses.empty: | |
| next_bus = upcoming_buses.iloc[0] | |
| prediction = predict_bus_status(next_bus['bus_number'], city, area) | |
| return f"๐ Next bus: {next_bus['bus_number']} arriving at {next_bus['arrival_time']}\n๐ฎ Prediction: {prediction}" | |
| return "โ ๏ธ No upcoming buses available." | |
| prediction = predict_next_bus(filtered_df) | |
| st.success(prediction) | |
| if st.button("Logout"): | |
| st.session_state.authenticated = False | |
| st.experimental_rerun() | |