I am trying to write a streamlit app that takes a prompt, takes a photo, and then run a GPT-4 Vision analysis on the photo. The app does not run beyond line ~13 (the screen just stops after the photo is taken, with no traceback). Would anyone know what the issue might be?
import streamlit as st
import base64
import requests
# OpenAI API Key
api_key = "xxx"
title = st.text_input("Your prompt:","What is in the picture?")
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer is not None:
# To read image file buffer as bytes:
bytes_data = img_file_buffer .getvalue()
# Convert bytes data to base64
base64_encoded = base64.b64encode(bytes_data).decode()
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-turbo",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": title
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_encoded}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
print(response.json())
st.text(response.json())