Spaces:
Sleeping
Sleeping
Update app.py (#3)
Browse files- Update app.py (715e443b9f3159bc32fae0192aa1475aef14f0a3)
app.py
CHANGED
|
@@ -1,110 +1,111 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import folium
|
| 5 |
-
from
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
longitude
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if "
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
st.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
st.
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
if
|
| 110 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import folium
|
| 5 |
+
from streamlit_folium import folium_static
|
| 6 |
+
import groq
|
| 7 |
+
|
| 8 |
+
# Load bus data
|
| 9 |
+
data_path = r"C:\Users\Muthuraja\OneDrive\Attachments\Desktop\second\pdp.csv"
|
| 10 |
+
df = pd.read_csv(data_path)
|
| 11 |
+
|
| 12 |
+
# Dummy user credentials
|
| 13 |
+
USER_CREDENTIALS = {
|
| 14 |
+
"Muthuraja":"virat",
|
| 15 |
+
"Praveen":"dhoni",
|
| 16 |
+
"Pandi":"kabadi",
|
| 17 |
+
"admin": "password123",
|
| 18 |
+
"user": "buspass2025"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Groq API Key
|
| 22 |
+
GROQ_API_KEY = "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"
|
| 23 |
+
groq.api_key = GROQ_API_KEY
|
| 24 |
+
|
| 25 |
+
# User login
|
| 26 |
+
def authenticate(username, password):
|
| 27 |
+
if username in USER_CREDENTIALS and USER_CREDENTIALS[username] == password:
|
| 28 |
+
return True
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
+
# Generate bus prediction using Groq API
|
| 32 |
+
def predict_bus_status(bus_number, city, area):
|
| 33 |
+
prompt = f"Predict the status and arrival time for bus {bus_number} in {city}, {area}."
|
| 34 |
+
try:
|
| 35 |
+
client = groq.Client(api_key=GROQ_API_KEY)
|
| 36 |
+
response = client.chat.completions.create(
|
| 37 |
+
model="llama3-70b-8192", # Correct model name for Groq # Use Groq's model, adjust if needed
|
| 38 |
+
messages=[{"role": "system", "content": prompt}]
|
| 39 |
+
)
|
| 40 |
+
return response.choices[0].message.content.strip()
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error: {e}"
|
| 43 |
+
|
| 44 |
+
# Plot bus locations using Folium with real coordinates
|
| 45 |
+
def plot_bus_map(area_df):
|
| 46 |
+
if area_df.empty:
|
| 47 |
+
st.warning("No bus data available for this area.")
|
| 48 |
+
return
|
| 49 |
+
|
| 50 |
+
if "latitude" not in area_df.columns or "longitude" not in area_df.columns:
|
| 51 |
+
st.error("Latitude and longitude columns missing. Please update CSV.")
|
| 52 |
+
return
|
| 53 |
+
|
| 54 |
+
center_lat = area_df["latitude"].mean()
|
| 55 |
+
center_lon = area_df["longitude"].mean()
|
| 56 |
+
|
| 57 |
+
m = folium.Map(location=[center_lat, center_lon], zoom_start=12)
|
| 58 |
+
|
| 59 |
+
for _, row in area_df.iterrows():
|
| 60 |
+
folium.Marker(
|
| 61 |
+
location=[row["latitude"], row["longitude"]],
|
| 62 |
+
popup=f"{row['bus_number']} - {row['bus_route']}\nArrival: {row['arrival_time']}\nStatus: {row['status']}",
|
| 63 |
+
icon=folium.Icon(color="blue" if row["status"].lower() == "on time" else "red")
|
| 64 |
+
).add_to(m)
|
| 65 |
+
|
| 66 |
+
folium_static(m)
|
| 67 |
+
|
| 68 |
+
# Streamlit UI
|
| 69 |
+
st.title("🚍 Tamil Nadu Bus Tracking & Prediction System")
|
| 70 |
+
|
| 71 |
+
# Login form
|
| 72 |
+
if "authenticated" not in st.session_state:
|
| 73 |
+
st.session_state.authenticated = False
|
| 74 |
+
|
| 75 |
+
if not st.session_state.authenticated:
|
| 76 |
+
username = st.text_input("Username")
|
| 77 |
+
password = st.text_input("Password", type="password")
|
| 78 |
+
if st.button("Login"):
|
| 79 |
+
if authenticate(username, password):
|
| 80 |
+
st.session_state.authenticated = True
|
| 81 |
+
st.success("Login successful!")
|
| 82 |
+
else:
|
| 83 |
+
st.error("Invalid username or password")
|
| 84 |
+
|
| 85 |
+
if st.session_state.authenticated:
|
| 86 |
+
city = st.selectbox("Select City", df["city"].unique())
|
| 87 |
+
area = st.selectbox("Select Area", df[df["city"] == city]["area"].unique())
|
| 88 |
+
|
| 89 |
+
filtered_df = df[(df["city"] == city) & (df["area"] == area)]
|
| 90 |
+
|
| 91 |
+
st.subheader(f"🚌 Bus Details for {city}, {area}")
|
| 92 |
+
st.dataframe(filtered_df)
|
| 93 |
+
|
| 94 |
+
st.subheader("🗺️ Bus Map View")
|
| 95 |
+
plot_bus_map(filtered_df)
|
| 96 |
+
|
| 97 |
+
def predict_next_bus(area_df):
|
| 98 |
+
now = datetime.now()
|
| 99 |
+
upcoming_buses = area_df[area_df["arrival_time"] > now.strftime("%Y-%m-%d %H:%M:%S")]
|
| 100 |
+
if not upcoming_buses.empty:
|
| 101 |
+
next_bus = upcoming_buses.iloc[0]
|
| 102 |
+
prediction = predict_bus_status(next_bus['bus_number'], city, area)
|
| 103 |
+
return f"🚏 Next bus: {next_bus['bus_number']} arriving at {next_bus['arrival_time']}\n🔮 Prediction: {prediction}"
|
| 104 |
+
return "⚠️ No upcoming buses available."
|
| 105 |
+
|
| 106 |
+
prediction = predict_next_bus(filtered_df)
|
| 107 |
+
st.success(prediction)
|
| 108 |
+
|
| 109 |
+
if st.button("Logout"):
|
| 110 |
+
st.session_state.authenticated = False
|
| 111 |
+
st.experimental_rerun()
|