Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ui.py 1.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  1. import streamlit as st
  2. import requests
  3. import numpy as np
  4. from PIL import Image
  5. import io
  6. API_ENDPOINT = "https://pet-pawpularity.herokuapp.com/predict"
  7. # Create the header page content
  8. st.title("Pet Pawpularity Prediction App")
  9. st.markdown("### Predict the popularity of your cat or dog with machine learning",
  10. unsafe_allow_html=True)
  11. with open("data/app_image.jpg", "rb") as f:
  12. st.image(f.read(), use_column_width=True)
  13. st.text("Grab a picture of your pet or upload an image to get a Pawpularity score.")
  14. def predict(img):
  15. """
  16. A function that sends a prediction request to the API and return a cuteness score.
  17. """
  18. # Convert the bytes image to a NumPy array
  19. bytes_image = img.getvalue()
  20. numpy_image_array = np.array(Image.open(io.BytesIO(bytes_image)))
  21. # Send the image to the API
  22. response = requests.post(API_ENDPOINT, headers={"content-type": "text/plain"},
  23. data=str(numpy_image_array))
  24. if response.status_code == 200:
  25. return response.text
  26. else:
  27. raise Exception("Status: {}".format(response.status_code))
  28. def main():
  29. img_file = st.file_uploader("Upload an image", type=["jpg", "png"])
  30. if img_file is not None:
  31. with st.spinner("Predicting..."):
  32. prediction = float(predict(img_file).strip("[").strip("]"))
  33. st.success(f"Your pet's cuteness score is {prediction:.3f}")
  34. camera_input = st.camera_input("Or take a picture")
  35. if camera_input is not None:
  36. with st.spinner("Predicting..."):
  37. prediction = float(predict(camera_input).strip("[").strip("]"))
  38. st.success(f"Your pet's cuteness score is {prediction:.3f}")
  39. if __name__ == "__main__":
  40. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...