Update app.py
Browse files
app.py
CHANGED
|
@@ -3,81 +3,56 @@ import pandas as pd
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
lanes = [
|
| 9 |
-
{
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
{
|
| 18 |
-
|
| 19 |
-
"dst": "Chicago, IL",
|
| 20 |
-
"type": "Flatbed",
|
| 21 |
-
"rate": "$2.40/mi",
|
| 22 |
-
"src_coords": (29.7604, -95.3698),
|
| 23 |
-
"dst_coords": (41.8781, -87.6298),
|
| 24 |
-
},
|
| 25 |
-
{
|
| 26 |
-
"src": "Savannah, GA",
|
| 27 |
-
"dst": "Charlotte, NC",
|
| 28 |
-
"type": "Reefer",
|
| 29 |
-
"rate": "$2.75/mi",
|
| 30 |
-
"src_coords": (32.0809, -81.0912),
|
| 31 |
-
"dst_coords": (35.2271, -80.8431),
|
| 32 |
-
},
|
| 33 |
-
{
|
| 34 |
-
"src": "Memphis, TN",
|
| 35 |
-
"dst": "Birmingham, AL",
|
| 36 |
-
"type": "Auto Parts",
|
| 37 |
-
"rate": "$2.10/mi",
|
| 38 |
-
"src_coords": (35.1495, -90.0490),
|
| 39 |
-
"dst_coords": (33.5186, -86.8104),
|
| 40 |
-
},
|
| 41 |
-
{
|
| 42 |
-
"src": "Mobile, AL",
|
| 43 |
-
"dst": "New Orleans, LA",
|
| 44 |
-
"type": "Tanker",
|
| 45 |
-
"rate": "$3.15/mi",
|
| 46 |
-
"src_coords": (30.6954, -88.0399),
|
| 47 |
-
"dst_coords": (29.9511, -90.0715),
|
| 48 |
-
},
|
| 49 |
]
|
| 50 |
|
| 51 |
-
# Initialize session state
|
| 52 |
-
if
|
| 53 |
st.session_state.messages = []
|
| 54 |
-
if
|
| 55 |
-
st.session_state.lane_counts = {f"{
|
| 56 |
-
if
|
| 57 |
-
st.session_state.markers = []
|
| 58 |
-
if
|
| 59 |
st.session_state.running = False
|
| 60 |
|
| 61 |
-
#
|
| 62 |
def generate_loads():
|
| 63 |
while st.session_state.running:
|
| 64 |
lane = random.choice(lanes)
|
| 65 |
-
msg = f"FreightMinded AI: Booked load {lane['src']} → {lane['dst']} ({lane['type']}, {lane['rate']})"
|
| 66 |
-
st.session_state.messages.append(msg)
|
| 67 |
key = f"{lane['src']} → {lane['dst']}"
|
|
|
|
|
|
|
|
|
|
| 68 |
st.session_state.lane_counts[key] += 1
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
time.sleep(5)
|
| 73 |
-
st.experimental_rerun()
|
| 74 |
|
| 75 |
-
|
|
|
|
| 76 |
|
|
|
|
|
|
|
| 77 |
st.title("🚚 Freight Minded Dispatch Automation")
|
| 78 |
st.caption("Let your truck pay like it weigh")
|
| 79 |
|
| 80 |
-
#
|
| 81 |
col1, col2 = st.columns(2)
|
| 82 |
with col1:
|
| 83 |
if st.session_state.running:
|
|
@@ -86,40 +61,41 @@ with col1:
|
|
| 86 |
else:
|
| 87 |
if st.button("Start Simulation"):
|
| 88 |
st.session_state.running = True
|
| 89 |
-
|
| 90 |
-
thread.start()
|
| 91 |
with col2:
|
| 92 |
if st.button("Reset"):
|
| 93 |
-
st.session_state.messages = []
|
| 94 |
-
st.session_state.lane_counts = {f"{lane['src']} → {lane['dst']}": 0 for lane in lanes}
|
| 95 |
-
st.session_state.markers = []
|
| 96 |
st.session_state.running = False
|
| 97 |
-
st.
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
#
|
| 100 |
left, right = st.columns([2, 1])
|
| 101 |
-
|
| 102 |
-
# Left column: message feed
|
| 103 |
with left:
|
| 104 |
st.subheader("Live Feed")
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
-
# Right column: stats, chart, map, report download
|
| 110 |
with right:
|
| 111 |
-
st.subheader("
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
st.subheader("Loads by Lane")
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
})
|
| 121 |
-
|
| 122 |
-
st.bar_chart(chart_df)
|
| 123 |
|
| 124 |
st.subheader("Route Map")
|
| 125 |
if st.session_state.markers:
|
|
@@ -128,6 +104,7 @@ with right:
|
|
| 128 |
else:
|
| 129 |
st.info("No routes booked yet.")
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import random
|
| 6 |
+
from streamlit_autorefresh import st_autorefresh
|
| 7 |
|
| 8 |
+
# Lane definitions (same as before)
|
| 9 |
lanes = [
|
| 10 |
+
{"src": "Dallas, TX", "dst": "Atlanta, GA", "type": "Dry Van", "rate": "$2.15/mi",
|
| 11 |
+
"src_coords": (32.7767, -96.7970), "dst_coords": (33.7490, -84.3880)},
|
| 12 |
+
{"src": "Houston, TX", "dst": "Chicago, IL", "type": "Flatbed", "rate": "$2.40/mi",
|
| 13 |
+
"src_coords": (29.7604, -95.3698), "dst_coords": (41.8781, -87.6298)},
|
| 14 |
+
{"src": "Savannah, GA", "dst": "Charlotte, NC", "type": "Reefer", "rate": "$2.75/mi",
|
| 15 |
+
"src_coords": (32.0809, -81.0912), "dst_coords": (35.2271, -80.8431)},
|
| 16 |
+
{"src": "Memphis, TN", "dst": "Birmingham, AL", "type": "Auto Parts", "rate": "$2.10/mi",
|
| 17 |
+
"src_coords": (35.1495, -90.0490), "dst_coords": (33.5186, -86.8104)},
|
| 18 |
+
{"src": "Mobile, AL", "dst": "New Orleans, LA", "type": "Tanker", "rate": "$3.15/mi",
|
| 19 |
+
"src_coords": (30.6954, -88.0399), "dst_coords": (29.9511, -90.0715)},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
+
# Initialize session state
|
| 23 |
+
if "messages" not in st.session_state:
|
| 24 |
st.session_state.messages = []
|
| 25 |
+
if "lane_counts" not in st.session_state:
|
| 26 |
+
st.session_state.lane_counts = {f"{l['src']} → {l['dst']}": 0 for l in lanes}
|
| 27 |
+
if "markers" not in st.session_state:
|
| 28 |
+
st.session_state.markers = []
|
| 29 |
+
if "running" not in st.session_state:
|
| 30 |
st.session_state.running = False
|
| 31 |
|
| 32 |
+
# Safe background loader
|
| 33 |
def generate_loads():
|
| 34 |
while st.session_state.running:
|
| 35 |
lane = random.choice(lanes)
|
|
|
|
|
|
|
| 36 |
key = f"{lane['src']} → {lane['dst']}"
|
| 37 |
+
# build and append message
|
| 38 |
+
msg = f"FreightMinded AI: Booked load {key} ({lane['type']}, {lane['rate']})"
|
| 39 |
+
st.session_state.messages.append(msg)
|
| 40 |
st.session_state.lane_counts[key] += 1
|
| 41 |
+
st.session_state.markers.extend([
|
| 42 |
+
{"lat": lane['src_coords'][0], "lon": lane['src_coords'][1]},
|
| 43 |
+
{"lat": lane['dst_coords'][0], "lon": lane['dst_coords'][1]},
|
| 44 |
+
])
|
| 45 |
time.sleep(5)
|
|
|
|
| 46 |
|
| 47 |
+
# Auto-refresh mechanism
|
| 48 |
+
stad = st_autorefresh(interval=2000, limit=None, key="auto")
|
| 49 |
|
| 50 |
+
# Page layout
|
| 51 |
+
st.set_page_config(page_title="Freight Minded Dispatch Automation", layout="wide")
|
| 52 |
st.title("🚚 Freight Minded Dispatch Automation")
|
| 53 |
st.caption("Let your truck pay like it weigh")
|
| 54 |
|
| 55 |
+
# Controls
|
| 56 |
col1, col2 = st.columns(2)
|
| 57 |
with col1:
|
| 58 |
if st.session_state.running:
|
|
|
|
| 61 |
else:
|
| 62 |
if st.button("Start Simulation"):
|
| 63 |
st.session_state.running = True
|
| 64 |
+
threading.Thread(target=generate_loads, daemon=True).start()
|
|
|
|
| 65 |
with col2:
|
| 66 |
if st.button("Reset"):
|
|
|
|
|
|
|
|
|
|
| 67 |
st.session_state.running = False
|
| 68 |
+
st.session_state.messages.clear()
|
| 69 |
+
st.session_state.lane_counts = {f"{l['src']} → {l['dst']}": 0 for l in lanes}
|
| 70 |
+
st.session_state.markers.clear()
|
| 71 |
|
| 72 |
+
# Main layout
|
| 73 |
left, right = st.columns([2, 1])
|
|
|
|
|
|
|
| 74 |
with left:
|
| 75 |
st.subheader("Live Feed")
|
| 76 |
+
feed = st.empty()
|
| 77 |
+
recent = st.session_state.messages[-20:][::-1]
|
| 78 |
+
feed.markdown("\n".join(f"- {m}" for m in recent))
|
| 79 |
|
|
|
|
| 80 |
with right:
|
| 81 |
+
st.subheader("📊 Key Metrics")
|
| 82 |
+
total = len(st.session_state.messages)
|
| 83 |
+
avg_rate = None
|
| 84 |
+
# compute average rate if any
|
| 85 |
+
if total > 0:
|
| 86 |
+
# naive parse: sum numeric from string
|
| 87 |
+
rates = [float(m.split("$")[1].split("/")[0]) for m in st.session_state.messages]
|
| 88 |
+
avg_rate = sum(rates) / len(rates)
|
| 89 |
+
st.metric("Total Loads", total)
|
| 90 |
+
if avg_rate:
|
| 91 |
+
st.metric("Avg Rate ($/mi)", f"{avg_rate:.2f}")
|
| 92 |
|
| 93 |
st.subheader("Loads by Lane")
|
| 94 |
+
df = pd.DataFrame({
|
| 95 |
+
"Lane": list(st.session_state.lane_counts.keys()),
|
| 96 |
+
"Count": list(st.session_state.lane_counts.values())
|
| 97 |
+
}).set_index("Lane")
|
| 98 |
+
st.bar_chart(df)
|
|
|
|
| 99 |
|
| 100 |
st.subheader("Route Map")
|
| 101 |
if st.session_state.markers:
|
|
|
|
| 104 |
else:
|
| 105 |
st.info("No routes booked yet.")
|
| 106 |
|
| 107 |
+
st.download_button("Download Report",
|
| 108 |
+
data="\n".join(st.session_state.messages),
|
| 109 |
+
file_name="daily_report.txt",
|
| 110 |
+
mime="text/plain")
|