Spaces:
Sleeping
Sleeping
Commit ·
dd97b30
1
Parent(s): 0a78961
Add Streamlit app and requirements
Browse files- app.py +39 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# FastAPI backend URL for prediction (replace this with your actual FastAPI URL)
|
| 7 |
+
FASTAPI_BACKEND_URL = "https://your-fastapi-model-url.a.run.app/predict"
|
| 8 |
+
|
| 9 |
+
# Function to send the image to FastAPI and get the prediction
|
| 10 |
+
def get_prediction(image):
|
| 11 |
+
# Convert image to bytes
|
| 12 |
+
img_byte_array = io.BytesIO()
|
| 13 |
+
image.save(img_byte_array, format="JPEG")
|
| 14 |
+
img_byte_array = img_byte_array.getvalue()
|
| 15 |
+
|
| 16 |
+
# Send the image to FastAPI using a POST request
|
| 17 |
+
response = requests.post(FASTAPI_BACKEND_URL, files={"file": img_byte_array})
|
| 18 |
+
|
| 19 |
+
if response.status_code == 200:
|
| 20 |
+
prediction = response.json()
|
| 21 |
+
return prediction
|
| 22 |
+
else:
|
| 23 |
+
return {"error": "Prediction failed"}
|
| 24 |
+
|
| 25 |
+
# Upload image through Streamlit
|
| 26 |
+
uploaded_image = st.file_uploader("Upload an image of a tree", type=["jpg", "png"])
|
| 27 |
+
|
| 28 |
+
if uploaded_image:
|
| 29 |
+
# Display the image
|
| 30 |
+
image = Image.open(uploaded_image)
|
| 31 |
+
st.image(image, caption="Uploaded Tree Image")
|
| 32 |
+
|
| 33 |
+
# Make prediction on the image
|
| 34 |
+
if st.button("Predict Decoration"):
|
| 35 |
+
prediction = get_prediction(image)
|
| 36 |
+
if 'error' not in prediction:
|
| 37 |
+
st.write(f"Prediction: {prediction['prediction']}")
|
| 38 |
+
else:
|
| 39 |
+
st.write("Error occurred during prediction.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
requests
|
| 3 |
+
pillow
|