Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)))
|