simrol commited on
Commit
0a7bf98
Β·
0 Parent(s):

first commit

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .env
2
+ .streamlit/secrets.toml
3
+ venv
.streamlit/config.toml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ [theme]
2
+ base="light"
3
+ primaryColor="slateBlue"
4
+ baseRadius="full"
5
+
6
+
7
+ [server]
8
+ maxUploadSize = 20
README.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AI-Hab API usage Demonstrator
2
+
3
+ This is a minimal demonstrator streamlit web application that classifies habitats into **UKHab Level 3 categories** using the AI-Hab computer computer vision model. This app serves as an example of integrating the API into an app.
4
+
5
+ The API codebase is available here: https://github.com/NERC-CEH/aihab-api
6
+
7
+ The API is in development and is currently hosted on the UKCEH Posit Connect server, it is only accessible via authentication.
8
+
9
+ ## Features
10
+
11
+ * **Camera capture or file upload**: Take a photo directly in the app or upload an existing image.
12
+ * **API-powered predictions**: Sends the image to a Posit Connect API for habitat classification.
13
+ * **Expandable API response**: Inspect full JSON responses directly in the app.
14
+
15
+ ## Requirements
16
+
17
+ * Python 3.8+
18
+ * [Streamlit](https://streamlit.io)
19
+ * `requests`
20
+ * `python-dotenv`
21
+
22
+ ## Installation
23
+
24
+ 1. Clone this repository:
25
+
26
+ ```bash
27
+ git clone https://github.com/<your-username>/<your-repo>.git
28
+ cd <your-repo>
29
+ ```
30
+
31
+ 2. Install dependencies:
32
+
33
+ ```bash
34
+ pip install -r requirements.txt
35
+ ```
36
+
37
+ 3. Create a `.env` file with your API details:
38
+
39
+ ```bash
40
+ API_KEY=<your-api-key>
41
+ API_URL=<your-api-url>
42
+ ```
43
+
44
+ ## Running the App
45
+
46
+ Run the Streamlit app locally:
47
+
48
+ ```bash
49
+ streamlit run app.py
50
+ ```
51
+
52
+ The app will open in your browser at `http://localhost:8501`.
53
+
54
+ ## Environment Variables
55
+
56
+ * `API_KEY`: Your API key for authenticating with the API.
57
+ * `API_URL`: Base URL of the API.
58
+
59
+ ## Project Structure
60
+
61
+ ```
62
+ β”œβ”€β”€ app.py # Main Streamlit app
63
+ β”œβ”€β”€ static/
64
+ β”‚ β”œβ”€β”€ img/
65
+ β”‚ β”‚ β”œβ”€β”€ logos etc.
66
+ β”œβ”€β”€ .env # Environment variables (not committed)
67
+ └── requirements.txt # Python dependencies
68
+ ```
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+ import os
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
+
10
+ # Load the API key from environment variables
11
+ api_key = os.getenv("API_KEY",None)
12
+ base_url = os.getenv("API_URL",None)
13
+ url = f"{base_url}/predict" # Adjust if upload endpoint is different
14
+
15
+ headers = {
16
+ "Authorization": f"Key {api_key}",
17
+ "accept": "application/json"
18
+ }
19
+
20
+ st.set_page_config(page_title="AI-Hab Habitat Classifier", page_icon="static/img/ai-hab-logo-transparent.png", layout="centered")
21
+ st.title("AI-Hab Habitat Classifier")
22
+
23
+ # Take photo or upload
24
+ img = st.camera_input("Take a photo of the habitat you wish to identify to UKHab Level 3") or st.file_uploader("Or upload a photo", type=["png", "jpg", "jpeg"])
25
+
26
+ if img:
27
+
28
+ # Send to API
29
+ with st.spinner("Analyzing habitat..."):
30
+ files = {"file": ("habitat.jpg", img.getvalue(), "image/jpeg")}
31
+ resp = requests.post(url, headers=headers, files=files)
32
+ data = resp.json()
33
+
34
+ # Display results
35
+ predictions = data["results"]["ukhab"]
36
+
37
+ st.image(img, caption="Uploaded image")
38
+
39
+ st.warning("**Note:** The model is still in development and may not be accurate.")
40
+
41
+ # Show secondary predictions
42
+ st.write("## Predictions")
43
+ for pred in predictions[0:]:
44
+ with st.container(border=True):
45
+ st.subheader(f"{pred['code']} - {pred['name']}")
46
+ if(pred['confidence']> 0.5):
47
+ st.badge(f"**Confidence:** {pred['confidence']:.2%}",color="green")
48
+ else:
49
+ st.badge(f"**Confidence:** {pred['confidence']:.2%}",color="orange")
50
+ st.write("" + " > ".join([h['name'] for h in pred['primary_habitat_hierarchy']]))
51
+
52
+ st.write("## API Response")
53
+ with st.expander("Show response data"):
54
+ st.code(json.dumps(data, indent=2), language="json")
55
+
56
+
57
+
58
+ st.divider()
59
+
60
+ col1, col2 = st.columns(2)
61
+ with col1:
62
+ st.write(" ")
63
+ st.image("static/img/UKCEH.png")
64
+ with col2:
65
+ st.image("static/img/University-of-Lincoln.png")
66
+
67
+ 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.")
68
+
static/img/UKCEH.png ADDED
static/img/University-of-Lincoln.png ADDED
static/img/ai-hab-logo-transparent.png ADDED
static/img/ai-hab-logo.png ADDED