Spaces:
Runtime error
Runtime error
| import base64 | |
| import requests | |
| import streamlit as st | |
| import tempfile, os | |
| # OpenAI API Key | |
| GPT4V_KEY = os.environ.get("GPT4V_KEY") | |
| GPT4V_ENDPOINT = "https://sean-westus.openai.azure.com/openai/deployments/gpt-4-vision/chat/completions?api-version=2023-07-01-preview" | |
| def send(base64_image, question): | |
| headers = { | |
| "Content-Type": "application/json", | |
| "Authorization": f"Bearer {GPT4V_KEY}" | |
| } | |
| # Payload for the request | |
| payload = { | |
| "model": "gpt-4-vision-preview", | |
| "messages": [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "image_url", | |
| "image_url": { | |
| "url": f"data:image/jpeg;base64,{base64_image}" | |
| } | |
| }, | |
| { | |
| "type": "text", | |
| "text": f""" {question}.""" + """\n""" + """{ | |
| "items_present": { | |
| "bottle": true | |
| } | |
| }""" | |
| } | |
| ] | |
| },], | |
| "temperature": 0.7, | |
| "max_tokens": 400, | |
| "top_p": 0.95, | |
| } | |
| try: | |
| response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) | |
| print(response.json()) | |
| response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code | |
| except requests.RequestException as e: | |
| raise SystemExit(f"Failed to make the request. Error: {e}") | |
| print(response.json()) | |
| # Handle the response as needed (e.g., print or process) | |
| return(response.json()) | |
| st.title("GPT-4 Vision Chat Demo") | |
| photo_taker = st.camera_input('s') | |
| image_upload = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) | |
| if image_upload is not None: | |
| st.image(image_upload) | |
| question = st.text_input("Ask a question") | |
| button = st.button("Ask") | |
| if button: | |
| # get the image as a base64 string | |
| # image = base64.b64encode(image_upload.read()).decode('utf-8') | |
| image = base64.b64encode(photo_taker.getvalue()).decode('utf-8') | |
| with st.spinner("Thinking..."): | |
| st.write(send(image, question)["choices"][0]["message"]["content"]) | |