# Model Prediction Response Endpoint to Streamlit

**URL:** <https://discuss.roboflow.com/t/model-prediction-response-endpoint-to-streamlit/4236>\
**Category:** 🤝  Community Help\
**Created:** [January 18, 2024, 1:43pm UTC](https://discuss.roboflow.com/t/model-prediction-response-endpoint-to-streamlit/4236 "2024-01-18T13:43:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ia29](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@ia29](https://discuss.roboflow.com/u/ia29)\
**Post date:** [January 18, 2024, 1:43pm UTC](https://discuss.roboflow.com/t/model-prediction-response-endpoint-to-streamlit/4236/1 "2024-01-18T13:43:00Z")

</div>

Hello! 🙂  
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:**

 ![streamlit_app_screenshot_2](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/0/0b73f0fbf18156592973e70cbea46067c904d648.jpeg)

**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)}")

---

<div class="post-metadata">

**Author:** ![ia29](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@ia29](https://discuss.roboflow.com/u/ia29)\
**Post date:** [January 19, 2024, 3:46pm UTC](https://discuss.roboflow.com/t/model-prediction-response-endpoint-to-streamlit/4236/3 "2024-01-19T15:46:03Z")

</div>

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

Best wishes.

---

<div class="post-metadata">

**Author:** ![leo](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/leo/32/2228_2.png) [@leo](https://discuss.roboflow.com/u/leo)\
**Post date:** [January 20, 2024, 12:12am UTC](https://discuss.roboflow.com/t/model-prediction-response-endpoint-to-streamlit/4236/4 "2024-01-20T00:12:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/bb6edbfa56d0cf2ee603f061e9e09103502cc8cc.png) [@system](https://discuss.roboflow.com/u/system)\
**Post date:** [February 10, 2024, 12:12am UTC](https://discuss.roboflow.com/t/model-prediction-response-endpoint-to-streamlit/4236/5 "2024-02-10T00:12:52Z")

</div>

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