Model Prediction Response Endpoint to Streamlit

Hello! :slightly_smiling_face:
Just having some issues with the prediction response from the Roboflow API. Not getting the predictions to come through which I want to deploy on Streamlit.

I attached my a screenshot and my code. Double checked my API key, project name, version but there must be something I am missing.

Thank you so much in advance.

Screenshot:

Code:

import streamlit as st
from roboflow import Roboflow
from PIL import Image
from io import BytesIO

Function to get predictions from Roboflow model

def get_prediction(image_bytes):
try:
# eventually use streamlit secrets for api
api_key = “XXXXXX”
project_name = “maize-catergories”
version = “1” # Replace with version number

      # Initialize Roboflow
      rf = Roboflow(api_key=api_key)
      project = rf.workspace().project(project_name)
      model = project.version(version).model

      # Save the uploaded image to a temporary file for prediction
      image = Image.open(BytesIO(image_bytes))
      image.save("temp_image.jpg")

      # Perform prediction and handle possible exceptions
      prediction = model.predict("temp_image.jpg").json()
      return prediction

  except Exception as e:
      return {"error": str(e)}

Streamlit app interface

st.title(“Image Classification with Roboflow”)

uploaded_file = st.file_uploader(“Choose an image…”, type=[“jpg”, “jpeg”, “png”])

if uploaded_file is not None:
try:
# Display the uploaded image
st.image(uploaded_file, caption=‘Uploaded Image’, use_column_width=True)

      # Get predictions
      with st.spinner('Analyzing the image...'):
      prediction = get_prediction(uploaded_file.getvalue())

      # Check for errors in the prediction
      if "error" in prediction:
          st.error(f"Error in prediction: {prediction['error']}")
      else:
          st.success('Analysis complete.')
          # Display predictions
          st.write("Prediction Time:", prediction.get("time", "N/A"))
          st.write("Predictions:")
          for pred in prediction.get("predictions", []):
          st.write(f"Class: {pred.get('class', 'N/A')}, Confidence: {pred.get('confidence', 'N/A'):.2f}")
          st.write("Top Prediction:", prediction.get("top", "N/A"), "with confidence", prediction.get("confidence", "N/A"))

except Exception as e:
st.error(f"Error: {str(e)}")

Hey Lenny worked through all this, still stuck on this issue at the moment.

Best wishes.

Hi @ia29

Could you explain in deeper detail what the problem you’re experiencing is? I’m not sure what you mean when you say you’re not getting the predictions to come through.

Also, I see that you included debug code, which is great. Could you share the console outputs? It could be very helpful in narrowing the issue.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.