willco-afk commited on
Commit
d8a9872
1 Parent(s): 9a3fd03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -73
app.py CHANGED
@@ -1,74 +1,53 @@
1
- Hugging Face's logo
2
- Hugging Face
3
-
4
- Models
5
- Datasets
6
- Spaces
7
- Docs
8
- Enterprise
9
- Pricing
10
-
11
-
12
-
13
-
14
- Spaces:
15
-
16
-
17
- willco-afk
18
- /
19
- tree-tests
20
-
21
- like
22
- 0
23
-
24
- App
25
-
26
- Files
27
- Community
28
- Settings
29
- tree-tests
30
- /
31
- app.py
32
-
33
- willco-afk's picture
34
- willco-afk
35
- Update app.py
36
- 8ef1729
37
- VERIFIED
38
- 4 minutes ago
39
- raw
40
-
41
- Copy download link
42
- history
43
- blame
44
- edit
45
- delete
46
-
47
- 1 kB
48
- import streamlit as st
49
- import numpy as np
50
- import tensorflow as tf
51
- from PIL import Image
52
  import os
53
-
54
- # Load the model directly from the path (no need to unzip)
55
- model_path = 'your_trained_model.keras' # Path to your model
56
- model = tf.keras.models.load_model(model_path) # Load the model
57
-
58
- # Streamlit UI for uploading an image
59
- st.title("Tree Decoration Prediction")
60
- uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png"])
61
-
62
- if uploaded_image:
63
- # Process the image for prediction
64
- img = Image.open(uploaded_image)
65
- img = img.resize((224, 224)) # Resize to match the input size of your model
66
- img_array = np.array(img) / 255.0 # Normalize the image
67
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
68
-
69
- # Make prediction
70
- prediction = model.predict(img_array)
71
-
72
- # Display the result
73
- st.image(uploaded_image, caption="Uploaded Image.", use_column_width=True)
74
- st.write(f"Prediction: {'Decorated' if prediction[0] > 0.5 else 'Undecorated'}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ from fastapi import FastAPI
3
+ from google.cloud import storage
4
+ from keras.models import load_model
5
+ import tempfile
6
+ import numpy as np
7
+ from pydantic import BaseModel
8
+
9
+ app = FastAPI()
10
+
11
+ # Function to load the model from Google Cloud Storage
12
+ def load_model_from_gcs(model_path):
13
+ client = storage.Client()
14
+ bucket = client.get_bucket('tree-decorator-model') # Your bucket name
15
+ blob = bucket.blob(model_path) # Path to your model in the bucket
16
+
17
+ # Save the model file locally in a temporary file
18
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
19
+ blob.download_to_filename(temp_file.name) # Download model to temporary file
20
+ model = load_model(temp_file.name) # Load model from the temporary file
21
+
22
+ return model
23
+
24
+ # Load the model from Google Cloud Storage (provide the path to your model in the bucket)
25
+ model = load_model_from_gcs('models/your_trained_model.keras') # Path in GCS
26
+
27
+ # Pydantic model for the incoming prediction request (adjust as needed)
28
+ class ImageData(BaseModel):
29
+ image: str # Base64-encoded image or URL of the image (you can adjust this)
30
+
31
+ @app.get("/")
32
+ def read_root():
33
+ return {"message": "Welcome to the Tree Decorator API!"}
34
+
35
+ @app.post("/predict/")
36
+ async def predict(data: ImageData):
37
+ # Example: Decode the image, preprocess it, and use the model for prediction
38
+ # Decode and preprocess the image data as required (e.g., using Pillow, OpenCV, etc.)
39
+
40
+ # For simplicity, we'll assume 'data.image' is already preprocessed or passed in an acceptable format
41
+
42
+ # Example prediction (replace with actual image processing and prediction logic)
43
+ # prediction = model.predict(processed_image)
44
+
45
+ # Dummy response for demonstration
46
+ prediction = {"prediction": "decorated" if np.random.random() > 0.5 else "not decorated"}
47
+
48
+ return prediction
49
+
50
+ # Run the FastAPI app
51
+ if __name__ == "__main__":
52
+ import uvicorn
53
+ uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8080)))