github-actions[bot] commited on
Commit
a2da3c2
·
1 Parent(s): f8edce3

Sync with https://github.com/mozilla-ai/osm-ai-helper

Browse files
Files changed (2) hide show
  1. Dockerfile +26 -0
  2. app.py +119 -0
Dockerfile ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM nvidia/cuda:12.2.2-cudnn8-devel-ubuntu22.04
2
+
3
+ RUN apt-get update && apt-get install --no-install-recommends -y \
4
+ espeak-ng \
5
+ build-essential \
6
+ python3.10 \
7
+ python3.10-dev \
8
+ python3-pip \
9
+ git \
10
+ && apt-get clean && rm -rf /var/lib/apt/lists/*
11
+
12
+ RUN useradd -m -u 1000 user
13
+
14
+ USER user
15
+
16
+ ENV HOME=/home/user \
17
+ PATH=/home/user/.local/bin:$PATH
18
+
19
+ WORKDIR $HOME/app
20
+
21
+ RUN pip3 install git+https://github.com/mozilla-ai/osm-ai-helper.git
22
+
23
+ COPY --chown=user . $HOME/app
24
+
25
+ EXPOSE 8501
26
+ ENTRYPOINT ["streamlit", "run", "app.py", "--server.enableXsrfProtection", "false"]
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ from shutil import move
4
+
5
+ import folium
6
+ import streamlit as st
7
+ from loguru import logger
8
+ from huggingface_hub import hf_hub_download
9
+ from PIL import Image
10
+ from streamlit_folium import st_folium
11
+
12
+ from osm_ai_helper.run_inference import run_inference
13
+ from osm_ai_helper.upload_osm import upload_osm
14
+
15
+
16
+ class StreamlitHandler:
17
+ def __init__(self):
18
+ self.text_widget = st.empty()
19
+ self.log_messages = ""
20
+
21
+ def write(self, message):
22
+ self.log_messages += message
23
+ self.text_widget.code(self.log_messages)
24
+ return
25
+
26
+
27
+ @st.fragment
28
+ def inference(lat_lon, margin):
29
+ hf_hub_download(
30
+ "daavoo/yolo-osm-pool-detector",
31
+ filename="model.pt",
32
+ repo_type="model",
33
+ local_dir="models",
34
+ )
35
+ output_path, *_ = run_inference(
36
+ model_file="models/model.pt",
37
+ output_dir="results",
38
+ lat_lon=lat_lon,
39
+ margin=margin,
40
+ )
41
+ return output_path
42
+
43
+
44
+ @st.fragment
45
+ def handle_polygon(polygon):
46
+ raw_image = Image.open(polygon.with_suffix(".png"))
47
+ painted_image = Image.open(f"{polygon.parent}/{polygon.stem}_painted.png")
48
+
49
+ st.subheader(f"Reviewing: {polygon.name}")
50
+
51
+ col1, col2 = st.columns(2)
52
+
53
+ with col1:
54
+ st.image(raw_image, caption="Raw Image", use_column_width=True)
55
+ with col2:
56
+ st.image(painted_image, caption="Painted Image", use_column_width=True)
57
+
58
+ if st.button("Keep Polygon", key=f"keep_{polygon}"):
59
+ keep_folder = polygon.parent / "keep"
60
+ keep_folder.mkdir(parents=True, exist_ok=True)
61
+ move(polygon, keep_folder / polygon.name)
62
+ st.success(f"Polygon moved to {keep_folder}")
63
+ elif st.button("Discard Polygon", key=f"discard_{polygon.stem}"):
64
+ discard_folder = polygon.parent / "discard"
65
+ discard_folder.mkdir(parents=True, exist_ok=True)
66
+ move(polygon, discard_folder / polygon.name)
67
+ st.success(f"Polygon moved to {discard_folder}")
68
+
69
+
70
+ @st.fragment
71
+ def upload_results(output_path):
72
+ st.divider()
73
+ st.header("Upload all polygons in `keep`")
74
+
75
+ st.markdown(
76
+ "The results will be uploaded using our OpenStreetMap account."
77
+ "You can check the [Colab Notebook](ttps://colab.research.google.com/github/mozilla-ai/osm-ai-helper/blob/main/demo/run_inference.ipynb)"
78
+ " and the [Authorization Guide](https://mozilla-ai.github.io/osm-ai-helper/authorization)"
79
+ " to contribute with your own OpenStreetMap account."
80
+ )
81
+ contributor = st.text_input("(Optional) Indicate your name for attribution")
82
+ if st.button("Upload all polygons in `keep`"):
83
+ if contributor:
84
+ comment = f"Add Swimming Pools. Contributed by {contributor}"
85
+ else:
86
+ comment = "Add Swimming Pools"
87
+ upload_osm(
88
+ results_dir=output_path / "keep",
89
+ client_id=os.environ["OSM_CLIENT_ID"],
90
+ client_secret=os.environ["OSM_CLIENT_SECRET"],
91
+ comment=comment,
92
+ )
93
+
94
+
95
+ st.title("Open Street Map AI Helper")
96
+
97
+ st.divider()
98
+
99
+ st.subheader("Click on the map to select a latitude and longitude")
100
+
101
+ m = folium.Map(location=[42.2489, -8.5117], zoom_start=11, tiles="OpenStreetMap")
102
+
103
+ st_data = st_folium(m, width=725)
104
+
105
+ if st_data.get("last_clicked"):
106
+ lat = st_data["last_clicked"]["lat"]
107
+ lon = st_data["last_clicked"]["lng"]
108
+ st.write(f"Last Clicked: {lat}, {lon}")
109
+
110
+ if st.button("Run Inference"):
111
+ streamlit_handler = StreamlitHandler()
112
+ logger.add(streamlit_handler, format="<level>{message}</level>")
113
+
114
+ output_path = inference(lat_lon=(lat, lon), margin=1)
115
+
116
+ for new in Path(output_path).glob("*.json"):
117
+ handle_polygon(new)
118
+
119
+ upload_results(output_path)