simrol commited on
Commit
dba2162
·
1 Parent(s): ab82633

New UKCEH logo, add user licence, warmup function speed up API

Browse files
Files changed (4) hide show
  1. .gitignore +3 -1
  2. app.py +36 -13
  3. static/img/UKCEH.png +0 -0
  4. static/licence/licence.md +105 -0
.gitignore CHANGED
@@ -1,3 +1,5 @@
1
  .env
2
  .streamlit/secrets.toml
3
- venv
 
 
 
1
  .env
2
  .streamlit/secrets.toml
3
+ venv
4
+
5
+ rsconnect-python/
app.py CHANGED
@@ -4,6 +4,7 @@ import json
4
  import os
5
  from dotenv import load_dotenv
6
  from streamlit_js_eval import get_geolocation
 
7
 
8
  # Load environment variables from .env file
9
  load_dotenv()
@@ -12,31 +13,37 @@ load_dotenv()
12
  api_key = os.getenv("API_KEY",None)
13
  base_url = os.getenv("API_URL",None)
14
  url = f"{base_url}/predict" # Adjust if upload endpoint is different
 
15
 
16
  headers = {
17
  "Authorization": f"Key {api_key}",
18
  "accept": "application/json"
19
  }
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  st.set_page_config(page_title="AI-Hab Habitat Classifier", page_icon="static/img/ai-hab-logo-transparent.png", layout="centered")
22
- st.title("AI-Hab Habitat Classifier")
23
 
24
- #tab1, tab2, tab3 = st.tabs(["Location", "Image", "Prediction"])
25
 
26
- #with tab1:
27
- # st.write("## Location")
28
- location = get_geolocation()
29
- # if location is not None:
30
- # st.map([{"lat": location['coords']['latitude'], "lon": location['coords']['longitude']}])
31
- # else:
32
- # st.warning("Location is not enabled.")
33
 
34
  # Take photo or upload
35
- #with tab2:
36
  st.write("## Capture or Upload Image")
37
  img = st.camera_input("Take a photo of the habitat") or st.file_uploader("Or upload a photo", type=["png", "jpg", "jpeg"])
 
38
 
39
- #with tab3:
40
  if img:
41
  # Send to API
42
  with st.spinner("Analyzing habitat..."):
@@ -94,5 +101,21 @@ with col1:
94
  with col2:
95
  st.image("static/img/University-of-Lincoln.png")
96
 
97
- st.write("AI-Hab is a habitat classification model developed by the UK Centre for Ecology & Hydrology and the University of Lincoln. It is based on the UKHab Habitat Classification system and uses computer vision to classify habitats from images.")
98
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import os
5
  from dotenv import load_dotenv
6
  from streamlit_js_eval import get_geolocation
7
+ import threading
8
 
9
  # Load environment variables from .env file
10
  load_dotenv()
 
13
  api_key = os.getenv("API_KEY",None)
14
  base_url = os.getenv("API_URL",None)
15
  url = f"{base_url}/predict" # Adjust if upload endpoint is different
16
+ warm_up_url = f"{base_url}/warmup"
17
 
18
  headers = {
19
  "Authorization": f"Key {api_key}",
20
  "accept": "application/json"
21
  }
22
 
23
+ # Warmup function this runs in a separate thread to avoid blocking the main app and to ensure the API is ready (model loaded from cache) when the user makes a request
24
+ def warmup():
25
+ print("Warming up the API...")
26
+ resp = requests.get(warm_up_url, headers=headers)
27
+ print("Warmup response:", resp.status_code)
28
+ @st.cache_resource
29
+ def start_warmup_thread():
30
+ thread = threading.Thread(target=warmup, daemon=True)
31
+ thread.start()
32
+ return "warmup_started"
33
+ start_warmup_thread()
34
+
35
+
36
  st.set_page_config(page_title="AI-Hab Habitat Classifier", page_icon="static/img/ai-hab-logo-transparent.png", layout="centered")
 
37
 
38
+ st.title("AI-Hab Habitat Classifier")
39
 
40
+ st.markdown("AI-Hab is a habitat classification model developed by the [Laboratory of Vision Engineering](https://www.visioneng.org.uk/) at the [University of Lincoln](https://www.lincoln.ac.uk/) and the [UK Centre for Ecology & Hydrology](https://www.ceh.ac.uk/). It is based on the [UKHab](https://www.ukhab.org/) Habitat Classification system and uses computer vision to classify habitats from images. The model is trained on images from the [UKCEH Contryside Survey](https://www.ceh.ac.uk/our-science/projects/countryside-survey).")
 
 
 
 
 
 
41
 
42
  # Take photo or upload
 
43
  st.write("## Capture or Upload Image")
44
  img = st.camera_input("Take a photo of the habitat") or st.file_uploader("Or upload a photo", type=["png", "jpg", "jpeg"])
45
+ location = get_geolocation()
46
 
 
47
  if img:
48
  # Send to API
49
  with st.spinner("Analyzing habitat..."):
 
101
  with col2:
102
  st.image("static/img/University-of-Lincoln.png")
103
 
104
+ st.markdown("Read the preprint: [Habitat Classification from Ground-Level Imagery Using Deep Neural Networks](https://arxiv.org/abs/2507.04017).")
105
+ st.markdown("View the code to this demonstrator app on [GitHub](https://github.com/NERC-CEH/aihab-streamlit-demo)")
106
+
107
+ # Load licence markdown (cached)
108
+ @st.cache_data
109
+ def load_licence():
110
+ with open("static/licence/licence.md", "r", encoding="utf-8") as f:
111
+ return f.read()
112
+
113
+ # Define a dialog using the new decorator
114
+ @st.dialog("Terms and Conditions for AI-Hab API access")
115
+ def licence_dialog():
116
+ st.markdown(load_licence())
117
+ st.button("Close", on_click=lambda: st.session_state.update(show_dialog=False))
118
+
119
+ # Create a button to open the dialog
120
+ if st.button("View Terms and Conditions"):
121
+ licence_dialog()
static/img/UKCEH.png CHANGED
static/licence/licence.md ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Licence for API Access to AI-HAB API Available from the UK Centre for Ecology & Hydrology
2
+
3
+ Your use of information provided by UKCEH is at your own risk. Please read any warnings given about the limitations of the information.
4
+
5
+ ## 1 DEFINITIONS
6
+
7
+ “the API” means the application programming interface providing access to the Data and available here: ..
8
+
9
+ "Data" means information on habitat classification returned to you.
10
+
11
+ “Evaluation Use” means to access and use the API and Data for the purposes of internal assessment purposes only, without the right to commercialise, redistribute, or publicly disclose the Data or any derivative works created from it.
12
+
13
+ "Non-Commercial" means use of the API or Data that does not involve making a financial gain.
14
+
15
+ "Licensor" means Centre for Ecology & Hydrology (CEH), a component part of the Natural Environment Research Council (NERC).
16
+
17
+ "You" or "Licensee" means either (a) the individual accepting the terms of this licence on their own behalf, or (b) the corporate entity or partnership on whose behalf those terms are accepted.
18
+
19
+ ## 2 GRANT OF LICENCE
20
+
21
+ The Licensor grants to The Licensee a non-exclusive, non-transferable, royalty free licence to use the API and Data providing the use falls within the permitted use set out below and for no other purpose. Your licence does not permit You to sub-license the API or the Data for any purpose.
22
+
23
+ ## 3 PERMITTED USE
24
+
25
+ 3.1 You may use the API and Data for Evaluation Use which is Non-commercial in nature.
26
+
27
+ 4 RESTRICTIONS ON USE
28
+
29
+ 4.1 You may not rent, lease, sell, sublicense or otherwise distribute the Data or the API to a third party.
30
+
31
+ 4.2 You may not assign or transfer this Licence Agreement or any rights under it.
32
+
33
+ 4.3 You may not use the API in any way that damages the performance or security of the API and You understand and agree that the Licensor may if it deems your use of the API to damage the performance or security of the API disable your access to the API and terminate this licence without notice.
34
+
35
+
36
+
37
+ ## 5 UNDERTAKINGS
38
+
39
+ 5.1 You undertake to store the Data in such a way that they can be accessed and used only in accordance with the terms of the Licence Agreement.
40
+
41
+ 5.2 You undertake to ensure that all persons to whom You make the Data accessible are aware of the limitations placed upon the use of the Data by this Licence Agreement, and ensure that such persons comply with all the restrictions placed upon the use of and access to the Data as set out in this Licence Agreement.
42
+
43
+ 5.3 You agree to take full responsibility for identifying whether the API is fit for any use that You make of it and You understand and agree that the Licensor will not be responsible for providing support in relation to your use of the API or in relation to any Derived Services.
44
+
45
+ ## 6 OWNERSHIP
46
+
47
+ 6.1 The Licensor or its third party licensors retain ownership of the Data and related documentation, which are also protected under applicable copyright and database laws.
48
+
49
+ ## 7 WARRANTY
50
+
51
+ 7.1 Data may have been transcribed from analogue to digital format, or may have been acquired by means of automated techniques. Although such processes are subjected to quality control to ensure reliability where possible, some data may have been processed without human intervention and may as a consequence contain undetected errors.
52
+
53
+ 7.2 The Licensor gives no warranty as to the accuracy or completeness of the Data, including in the form in which they are cached or downloaded to your computer, as they may be affected by on-line conditions over which the Licensor has no control.
54
+
55
+ 7.3 The data, information and related records supplied by the Licensor should not be taken as a substitute for specialist interpretations, and /or professional advice. You must seek professional advice before making technical interpretations on the basis of the materials provided.
56
+
57
+ 7.4 The Licensor does not warrant that the API or the Data will meet your requirements or that the API’s operation or the operation of any Derived Services will be uninterrupted or error free. This licence agreement does not affect your statutory rights.
58
+
59
+ 7.5 The Licensor does not warrant that the API or Data will remain unchanged in terms of content or structure.
60
+
61
+ ## 8 LIABILITY
62
+
63
+ 8.1 All guarantees, representations and warranties of any kind, whether express or implied, including, without limitation, the implied warranties of satisfactory quality, merchantability and fitness for a particular purpose or ability to achieve a particular result are hereby excluded, so far as such exclusion or disclaimer is permitted under the applicable law. You assume the entire risk as to the quality and performance of the API and Data. Should the API or Data prove defective, You (and not the Licensor) assume the entire cost of all necessary servicing or correction.
64
+
65
+ 8.2 In no event shall the Licensor be liable for any damages, including loss of business, loss of opportunity, loss of data, loss of profits or for any other indirect or consequential loss or damage whatsoever arising out of the use of or inability to use the API or Data even if the Licensor has been made aware of the possibility of such damages.
66
+
67
+ 8.3 The Licensor accepts no liability for any loss or damage which may be caused by the API or Data and the Licensee is expected to operate suitable anti-virus software before loading it into its computer system. The Licensee also being responsible for ensuring that the form of the information received is compatible with its computer system and any other data with which the information is to be used.
68
+
69
+ 8.4 Nothing in this Licence Agreement shall exclude or limit the liability of the Licensor or the Licensee for fraudulent misrepresentation or for death or personal injury resulting from the negligence of the Licensor or the Licensee.
70
+
71
+ ## 9 DATA PROTECTION
72
+
73
+ 9.1 The Licensor will treat any personal information provided or obtained, in accordance with the provisions of the Data Protection Act of 2018.
74
+
75
+ 9.2 Any personal information provided or obtained shall be used only for the purposes of providing the Licensee with access to the API and Data, for communication relating to updates and amendments, relating to CEH products or services, and for system administration of the web server, unless otherwise agreed by the Licensee.
76
+
77
+ 9.3 The Licensor will on request, delete or remove any personal information held from a Licensee.
78
+
79
+ ## 10 TERMINATION
80
+
81
+ 10.1 The Licensor may terminate this Licence Agreement and disable the Licensee’s access to the API without notice.
82
+
83
+ 10.2 The Licence Agreement will terminate automatically if any of its terms are breached by the Licensee.
84
+
85
+ 10.3 Upon termination all rights You have to use the API and Data will cease and You must destroy or delete the Data (including cashed, partial or modified versions and datasets derived from the Data) and all copies from all storage media in your control.
86
+
87
+ Clauses 1, 7, 8, 10.3 and 13 will survive termination of this agreement.
88
+
89
+ ## 11 SEVERABILITY
90
+
91
+ If any provision of this licence agreement is held invalid, illegal or unenforceable for any reason by any court of competent jurisdiction such provision shall be severed and the remainder of the provisions hereof shall continue in full force and effect as if this licence agreement had been agreed with the invalid illegal or unenforceable provision eliminated.
92
+
93
+ ## 12 ENTIRE AGREEMENT
94
+
95
+ This licence agreement contains the entire agreement between parties relating to the subject matter and supersedes all proposals, representations, understandings and prior agreements, whether oral or written, and all other communications between parties relating to that subject matter.
96
+
97
+ ## 13 GOVERNING LAW AND JURISDICTION
98
+
99
+ This agreement shall be governed by and construed in accordance with English law and the parties submit to the exclusive jurisdiction of the English courts.
100
+
101
+ ## 14 CONTRACTS (RIGHTS OF THIRD PARTIES) ACT 1999
102
+
103
+ A person who is not party to this Agreement has no right under the Contracts (Rights of Third Parties) Act 1999 to enforce any terms of this Agreement, but this does not affect any right or remedy of a third party which exists or is available apart from that Act.
104
+
105
+