Spaces:
Sleeping
Sleeping
testing
Browse files- app.py +84 -174
- chatmap.py +41 -0
app.py
CHANGED
@@ -11,207 +11,117 @@
|
|
11 |
# See the License for the specific language governing permissions and
|
12 |
# limitations under the License.
|
13 |
|
|
|
14 |
import leafmap.foliumap as leafmap
|
15 |
import streamlit as st
|
16 |
st.set_page_config(layout="wide", page_title="Protected Areas Explorer", page_icon=":globe:")
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
mixed = "https://minio.carlboettiger.info/shared-data/pad/mixed.pmtiles"
|
23 |
red = "#850101"
|
24 |
purple = "#BF40BF"
|
25 |
green = "#005a00"
|
26 |
blue = "#3388ff"
|
27 |
|
28 |
-
public_style = {
|
29 |
-
"version": 8,
|
30 |
-
"sources": {
|
31 |
-
"public": {
|
32 |
-
"type": "vector",
|
33 |
-
"url": "pmtiles://" + public,
|
34 |
-
"attribution": "US PAD v3"}},
|
35 |
-
"layers": [{
|
36 |
-
"id": "public",
|
37 |
-
"source": "public",
|
38 |
-
"source-layer": "public_protected",
|
39 |
-
"type": "fill",
|
40 |
-
"paint": {"fill-color": blue, "fill-opacity": 0.5}}]}
|
41 |
-
private_style = {
|
42 |
-
"version": 8,
|
43 |
-
"sources": {
|
44 |
-
"private": {
|
45 |
-
"type": "vector",
|
46 |
-
"url": "pmtiles://" + private,
|
47 |
-
"attribution": "US PAD v3",}},
|
48 |
-
"layers": [
|
49 |
-
{
|
50 |
-
"id": "private",
|
51 |
-
"source": "private",
|
52 |
-
"source-layer": "private_protected",
|
53 |
-
"type": "fill",
|
54 |
-
"paint": {"fill-color": red, "fill-opacity": 0.5}}]}
|
55 |
-
tribal_style = {
|
56 |
-
"version": 8,
|
57 |
-
"sources": {
|
58 |
-
"tribal": {
|
59 |
-
"type": "vector",
|
60 |
-
"url": "pmtiles://" + tribal,
|
61 |
-
"attribution": "US PAD v3"}},
|
62 |
-
"layers": [{
|
63 |
-
"id": "tribal",
|
64 |
-
"source": "tribal",
|
65 |
-
"source-layer": "tribal_protected",
|
66 |
-
"type": "fill",
|
67 |
-
"paint": {"fill-color": purple, "fill-opacity": 0.6}}]}
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
# -
|
84 |
|
85 |
-
## Tooltip layer -- hack
|
86 |
-
combined_style = {
|
87 |
-
"version": 8,
|
88 |
-
"sources": {
|
89 |
-
"public": {
|
90 |
-
"type": "vector",
|
91 |
-
"url": "pmtiles://" + public,
|
92 |
-
"attribution": "PMTiles"},
|
93 |
-
"tribal": {
|
94 |
-
"type": "vector",
|
95 |
-
"url": "pmtiles://" + tribal,
|
96 |
-
"attribution": "PMTiles"},
|
97 |
-
"private": {
|
98 |
-
"type": "vector",
|
99 |
-
"url": "pmtiles://" + private,
|
100 |
-
"attribution": "PMTiles"},
|
101 |
-
"mixed": {
|
102 |
-
"type": "vector",
|
103 |
-
"url": "pmtiles://" + mixed,
|
104 |
-
"attribution": "PMTiles"}},
|
105 |
-
"layers": [{
|
106 |
-
"id": "mixed",
|
107 |
-
"source": "mixed",
|
108 |
-
"source-layer": "mixed_protected",
|
109 |
-
"type": "fill",
|
110 |
-
"paint": {"fill-color": green, "fill-opacity": 0.}},
|
111 |
-
{
|
112 |
-
"id": "tribal",
|
113 |
-
"source": "tribal",
|
114 |
-
"source-layer": "tribal_protected",
|
115 |
-
"type": "fill",
|
116 |
-
"paint": {"fill-color": purple, "fill-opacity": 0.}
|
117 |
-
},
|
118 |
-
{
|
119 |
-
"id": "public",
|
120 |
-
"source": "public",
|
121 |
-
"source-layer": "public_protected",
|
122 |
-
"type": "fill",
|
123 |
-
"paint": {"fill-color": blue, "fill-opacity": 0.}
|
124 |
-
},
|
125 |
-
{
|
126 |
-
"id": "private",
|
127 |
-
"source": "private",
|
128 |
-
"source-layer": "private_protected",
|
129 |
-
"type": "fill",
|
130 |
-
"paint": {"fill-color": red, "fill-opacity": 0.}}]}
|
131 |
|
132 |
# +
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
# -
|
150 |
|
151 |
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
153 |
style = {
|
154 |
"version": 8,
|
155 |
"sources": {
|
156 |
-
"
|
157 |
-
"type": "vector",
|
158 |
-
"url": "pmtiles://" + public,
|
159 |
-
"attribution": "PMTiles"},
|
160 |
-
"tribal": {
|
161 |
-
"type": "vector",
|
162 |
-
"url": "pmtiles://" + tribal,
|
163 |
-
"attribution": "PMTiles"},
|
164 |
-
"private": {
|
165 |
"type": "vector",
|
166 |
-
"url": "pmtiles://" +
|
167 |
-
"attribution": "
|
168 |
-
"mixed": {
|
169 |
-
"type": "vector",
|
170 |
-
"url": "pmtiles://" + mixed,
|
171 |
-
"attribution": "PMTiles"}},
|
172 |
"layers": [{
|
173 |
-
"id": "mixed",
|
174 |
-
"source": "mixed",
|
175 |
-
"source-layer": "mixed_protected",
|
176 |
-
"type": "fill",
|
177 |
-
"paint": {"fill-color": green, "fill-opacity": 0.5}},
|
178 |
-
{
|
179 |
-
"id": "tribal",
|
180 |
-
"source": "tribal",
|
181 |
-
"source-layer": "tribal_protected",
|
182 |
-
"type": "fill",
|
183 |
-
"paint": {"fill-color": purple, "fill-opacity": 0.5}
|
184 |
-
},
|
185 |
-
{
|
186 |
"id": "public",
|
187 |
-
"source": "
|
188 |
-
"source-layer": "
|
189 |
"type": "fill",
|
190 |
-
"paint":
|
191 |
-
}
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
"paint": {"fill-color": red, "fill-opacity": 0.5}}]}
|
198 |
|
199 |
# +
|
200 |
-
|
|
|
201 |
|
|
|
202 |
|
203 |
-
#
|
204 |
|
205 |
-
|
206 |
-
m.add_basemap("Esri.WorldGrayCanvas")
|
207 |
-
m.add_tile_layer(
|
208 |
-
url="https://minio.carlboettiger.info/shared-data/mobi-tiles/red/{z}/{x}/{y}.png",
|
209 |
-
name="MOBI",
|
210 |
-
attribution="NatureServe",
|
211 |
-
opacity=0.9
|
212 |
-
)
|
213 |
-
m.add_pmtiles(
|
214 |
-
tribal, name="PMTiles", style=style, overlay=True, show=True, zoom_to_layer=False
|
215 |
-
)
|
216 |
|
217 |
-
|
|
|
11 |
# See the License for the specific language governing permissions and
|
12 |
# limitations under the License.
|
13 |
|
14 |
+
# +
|
15 |
import leafmap.foliumap as leafmap
|
16 |
import streamlit as st
|
17 |
st.set_page_config(layout="wide", page_title="Protected Areas Explorer", page_icon=":globe:")
|
18 |
|
19 |
+
low = st.slider("Red when richness is less than", 0, 30, 2)
|
20 |
+
high = st.slider("Green for richness larger than", 0, 30, 3)
|
21 |
+
"missing data in grey. in-between values in blue"
|
22 |
+
|
|
|
23 |
red = "#850101"
|
24 |
purple = "#BF40BF"
|
25 |
green = "#005a00"
|
26 |
blue = "#3388ff"
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# +
|
30 |
+
|
31 |
+
richness = {
|
32 |
+
"fill-color": [
|
33 |
+
"interpolate",
|
34 |
+
["linear"],
|
35 |
+
["get", "richness"],
|
36 |
+
0,
|
37 |
+
# ["min", ["number", ["get", "richness"]]],
|
38 |
+
"rgb(252, 226, 220)",
|
39 |
+
#["max", ["get", "richness"]],
|
40 |
+
8,
|
41 |
+
"rgb(102, 16, 66)"
|
42 |
+
],
|
43 |
+
"fill-opacity": 0.9
|
44 |
+
}
|
45 |
+
|
46 |
+
r = {
|
47 |
+
"fill-color": [
|
48 |
+
"rgb",
|
49 |
+
# red is higher when richness is higher
|
50 |
+
["get", "richness"],
|
51 |
+
# green is always zero
|
52 |
+
0,
|
53 |
+
# blue is higher when richness is lower
|
54 |
+
["-", 100, ["get", "richness"]]],
|
55 |
+
"fill-opacity": 0.8
|
56 |
+
}
|
57 |
|
|
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# +
|
61 |
+
|
62 |
+
thresholds = {
|
63 |
+
"fill-color":
|
64 |
+
['case',
|
65 |
+
['<', ['get', 'richness'], low],
|
66 |
+
red,
|
67 |
+
['>=', ['get', 'richness'], high],
|
68 |
+
green,
|
69 |
+
blue # default
|
70 |
+
],
|
71 |
+
"fill-opacity": 0.5
|
72 |
+
}
|
73 |
+
|
74 |
+
bucket = {
|
75 |
+
"fill-color":
|
76 |
+
['case',
|
77 |
+
['==', ['get', 'bucket'], 'public'],
|
78 |
+
blue,
|
79 |
+
['==', ['get', 'bucket'], 'private'],
|
80 |
+
red,
|
81 |
+
['==', ['get', 'bucket'], 'tribal'],
|
82 |
+
purple,
|
83 |
+
green # default (bucket == mixed)
|
84 |
+
],
|
85 |
+
"fill-opacity": 0.5
|
86 |
+
}
|
87 |
+
|
88 |
# -
|
89 |
|
90 |
|
91 |
+
|
92 |
+
# +
|
93 |
+
import leafmap.foliumap as leafmap
|
94 |
+
|
95 |
+
url = "https://data.source.coop/cboettig/pad-us-3/pad-mobi.pmtiles"
|
96 |
+
|
97 |
style = {
|
98 |
"version": 8,
|
99 |
"sources": {
|
100 |
+
"pad": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
"type": "vector",
|
102 |
+
"url": "pmtiles://" + url,
|
103 |
+
"attribution": "US PAD v3"}},
|
|
|
|
|
|
|
|
|
104 |
"layers": [{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
"id": "public",
|
106 |
+
"source": "pad",
|
107 |
+
"source-layer": "pad-mobi",
|
108 |
"type": "fill",
|
109 |
+
"paint": r
|
110 |
+
}]}
|
111 |
+
m = leafmap.Map(center=[35, -100], zoom=3)
|
112 |
+
#m.add_basemap("CartoDB.DarkMatter")
|
113 |
+
m.add_pmtiles(url, name="Public", style=style, overlay=True, show=True, zoom_to_layer=False)
|
114 |
+
#m.to_streamlit(height=900)
|
115 |
+
m
|
|
|
116 |
|
117 |
# +
|
118 |
+
import ibis
|
119 |
+
from ibis import _
|
120 |
|
121 |
+
t = ibis.read_parquet("https://data.source.coop/cboettig/pad-us-3/pad-mobi.parquet")
|
122 |
|
123 |
+
# -
|
124 |
|
125 |
+
t.group_by(_.bucket).aggregate(mean = (_.richness * _.area).mean() / _.area.sum()).to_pandas()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
+
t.group_by(_.bucket).aggregate(mean = (_.rsr * _.area).mean() / _.area.sum()).to_pandas()
|
chatmap.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from openai import OpenAI
|
3 |
+
|
4 |
+
st.title("ChatGPT-like clone")
|
5 |
+
|
6 |
+
# Set OpenAI API key from Streamlit secrets
|
7 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
8 |
+
|
9 |
+
# Set a default model
|
10 |
+
if "openai_model" not in st.session_state:
|
11 |
+
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
12 |
+
|
13 |
+
# Initialize chat history
|
14 |
+
if "messages" not in st.session_state:
|
15 |
+
st.session_state.messages = []
|
16 |
+
|
17 |
+
# Display chat messages from history on app rerun
|
18 |
+
for message in st.session_state.messages:
|
19 |
+
with st.chat_message(message["role"]):
|
20 |
+
st.markdown(message["content"])
|
21 |
+
|
22 |
+
# Accept user input
|
23 |
+
if prompt := st.chat_input("What is up?"):
|
24 |
+
# Add user message to chat history
|
25 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
26 |
+
# Display user message in chat message container
|
27 |
+
with st.chat_message("user"):
|
28 |
+
st.markdown(prompt)
|
29 |
+
|
30 |
+
# Display assistant response in chat message container
|
31 |
+
with st.chat_message("assistant"):
|
32 |
+
stream = client.chat.completions.create(
|
33 |
+
model=st.session_state["openai_model"],
|
34 |
+
messages=[
|
35 |
+
{"role": m["role"], "content": m["content"]}
|
36 |
+
for m in st.session_state.messages
|
37 |
+
],
|
38 |
+
stream=True,
|
39 |
+
)
|
40 |
+
response = st.write_stream(stream)
|
41 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|