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v1 as components
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
import utils # personal library utils.py file
|
| 8 |
+
import gmplot
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
|
| 13 |
+
# Load the .env file
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Get the Hugging Face token from the environment variables
|
| 17 |
+
google_map_token = os.getenv('GOOGLE_MAP_API_KEY')
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
url_dict = {"Incidents": "http://datamall2.mytransport.sg/ltaodataservice/TrafficIncidents",
|
| 21 |
+
"SpeedBands": "http://datamall2.mytransport.sg/ltaodataservice/v3/TrafficSpeedBands"}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# st.balloons()
|
| 25 |
+
st.title("LTA Singapore Traffic Watch")
|
| 26 |
+
|
| 27 |
+
st.write("We are so glad to see you here. ✨ "
|
| 28 |
+
"This app is going to have a quick walkthrough with you on "
|
| 29 |
+
"how to make an interactive data annotation app in streamlit in 5 min!")
|
| 30 |
+
|
| 31 |
+
st.write("Imagine you are evaluating different models for a Q&A bot "
|
| 32 |
+
"and you want to evaluate a set of model generated responses. "
|
| 33 |
+
"You have collected some user data. "
|
| 34 |
+
"Here is a sample question and response set.")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
incidents_df, speedbands_df = utils.get_lta_data(url_dict)
|
| 38 |
+
|
| 39 |
+
# To distinguish accidents from roadworks
|
| 40 |
+
incidents_df['Color_Type'] = incidents_df['Type'].apply(lambda x: 1 if x == "Accident" else 0)
|
| 41 |
+
|
| 42 |
+
# Pack the coordinates for polygon
|
| 43 |
+
speedbands_df['polygon'] = speedbands_df.apply(utils.zip_columns, axis=1)
|
| 44 |
+
|
| 45 |
+
incidents_df["Issue"] = [True] * incidents_df.shape[0]
|
| 46 |
+
incidents_df['Category'] = ["Accuracy"] * incidents_df.shape[0]
|
| 47 |
+
|
| 48 |
+
new_df = st.data_editor(
|
| 49 |
+
incidents_df,
|
| 50 |
+
column_config = {
|
| 51 |
+
"Questions":st.column_config.TextColumn(
|
| 52 |
+
width = "medium",
|
| 53 |
+
disabled=True
|
| 54 |
+
),
|
| 55 |
+
"Answers":st.column_config.TextColumn(
|
| 56 |
+
width = "medium",
|
| 57 |
+
disabled=True
|
| 58 |
+
),
|
| 59 |
+
"Issue":st.column_config.CheckboxColumn(
|
| 60 |
+
"Mark as annotated?",
|
| 61 |
+
default = False
|
| 62 |
+
),
|
| 63 |
+
"Category":st.column_config.SelectboxColumn
|
| 64 |
+
(
|
| 65 |
+
"Issue Category",
|
| 66 |
+
help = "select the category",
|
| 67 |
+
options = ['Accuracy', 'Relevance', 'Coherence', 'Bias', 'Completeness'],
|
| 68 |
+
required = False
|
| 69 |
+
)
|
| 70 |
+
}
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Create the map plotter:
|
| 74 |
+
center_lat = incidents_df['Latitude'].iloc[incidents_df.shape[0] // 2]
|
| 75 |
+
center_lng = incidents_df['Longitude'].iloc[incidents_df.shape[0] // 2]
|
| 76 |
+
zoom = 5
|
| 77 |
+
|
| 78 |
+
gmap = gmplot.GoogleMapPlotter(center_lat, center_lng, zoom, apikey=google_map_token, map_type='roadmap')
|
| 79 |
+
|
| 80 |
+
x = incidents_df['Longitude'].to_numpy()
|
| 81 |
+
y = incidents_df['Latitude'].to_numpy()
|
| 82 |
+
z = incidents_df['Color_Type'].to_numpy()
|
| 83 |
+
|
| 84 |
+
colors = list(map(utils.get_color_from_value, z))
|
| 85 |
+
|
| 86 |
+
gmap.scatter(y, x, color=colors, size=2, marker=True)
|
| 87 |
+
|
| 88 |
+
# Draw the map:
|
| 89 |
+
# gmap.draw('map.html') # without speedbands
|
| 90 |
+
|
| 91 |
+
polygons = speedbands_df['polygon']
|
| 92 |
+
speeds = speedbands_df['SpeedBand']
|
| 93 |
+
utils.plot_polygon_with_speed(polygons, speeds, gmap, 'map2.html')
|
| 94 |
+
|
| 95 |
+
st.divider()
|
| 96 |
+
|
| 97 |
+
st.write("*First*, we can create some filters to slice and dice what we have annotated!")
|
| 98 |
+
|
| 99 |
+
col1, col2 = st.columns([1,1])
|
| 100 |
+
with col1:
|
| 101 |
+
issue_filter = st.selectbox("Issues or Non-issues", options = new_df.Issue.unique())
|
| 102 |
+
with col2:
|
| 103 |
+
category_filter = st.selectbox("Choose a category", options = new_df[new_df["Issue"]==issue_filter].Category.unique())
|
| 104 |
+
|
| 105 |
+
st.dataframe(new_df[(new_df['Issue'] == issue_filter) & (new_df['Category'] == category_filter)])
|
| 106 |
+
|
| 107 |
+
st.markdown("")
|
| 108 |
+
st.write("*Next*, we can visualize our data quickly using `st.metrics` and `st.bar_plot`")
|
| 109 |
+
|
| 110 |
+
issue_cnt = len(new_df[new_df['Issue']==True])
|
| 111 |
+
total_cnt = len(new_df)
|
| 112 |
+
issue_perc = f"{issue_cnt/total_cnt*100:.0f}%"
|
| 113 |
+
|
| 114 |
+
col1, col2 = st.columns([1,1])
|
| 115 |
+
with col1:
|
| 116 |
+
st.metric("Number of responses",issue_cnt)
|
| 117 |
+
with col2:
|
| 118 |
+
st.metric("Annotation Progress", issue_perc)
|
| 119 |
+
|
| 120 |
+
df_plot = new_df[new_df['Category']!=''].Category.value_counts().reset_index()
|
| 121 |
+
|
| 122 |
+
st.bar_chart(df_plot, x = 'Category', y = 'count')
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
st.markdown("Real-Time Accident or Roadworks Monitoring using Streamlit Map")
|
| 126 |
+
|
| 127 |
+
#plotting a map with the above defined points
|
| 128 |
+
incidents_df = incidents_df.rename(columns={"Latitude": "latitude", "Longitude": "longitude"})
|
| 129 |
+
|
| 130 |
+
st.map(incidents_df[['latitude', 'longitude']])
|
| 131 |
+
|
| 132 |
+
st.divider()
|
| 133 |
+
|
| 134 |
+
st.markdown("Embedded Google Maps HTML from web-hosting site")
|
| 135 |
+
|
| 136 |
+
# Read the HTML file
|
| 137 |
+
map_url = "https://lta-sg-archived-traffic-monitoring.tiiny.site" # Adjust the path if necessary
|
| 138 |
+
|
| 139 |
+
# HTML content with iframe to embed the Google Map
|
| 140 |
+
iframe_html = f"""
|
| 141 |
+
<!DOCTYPE html>
|
| 142 |
+
<html>
|
| 143 |
+
<head>
|
| 144 |
+
<title>Embedded Google Map</title>
|
| 145 |
+
</head>
|
| 146 |
+
<body>
|
| 147 |
+
<iframe src="{map_url}" width="100%" height="800" style="border:none;"></iframe>
|
| 148 |
+
</body>
|
| 149 |
+
</html>
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
# Embed the iframe in the Streamlit app
|
| 153 |
+
components.html(iframe_html, height=800)
|
| 154 |
+
|
| 155 |
+
st.write("Here we are at the end of getting started with streamlit! Happy Streamlit-ing! :balloon:")
|