Spaces:
Running
Running
File size: 3,475 Bytes
8d37eb3 b2766e7 45ccb5b b2766e7 45ccb5b b2766e7 8d37eb3 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
from openai import OpenAI
import streamlit as st
from utils import im_2_b64
RANDOM_SEED = 42
st.title("ChatGPT with Vision")
client = OpenAI(api_key=st.secrets["OPENAI_KEY"])
if "messages" not in st.session_state:
st.session_state.messages = []
if "uploader_key" not in st.session_state:
st.session_state["uploader_key"] = 0
def clear_uploader():
st.session_state["uploader_key"] += 1
st.rerun()
with st.sidebar:
if st.button("Clear chat"):
st.session_state.messages = []
clear_uploader()
with st.expander("Advanced Configuration"):
st.subheader("Temperature")
temperature = st.slider(label="x", min_value=0.1, max_value=1.0, value=0.5, step=0.1, label_visibility='hidden')
st.subheader("Max Tokens")
max_tokens = st.slider(label="x", min_value=32, max_value=1024, value=256, step=32, label_visibility='hidden')
with st.expander("Image Input", expanded=True):
images = st.file_uploader(
"Image Upload",
accept_multiple_files=True,
type=["png", "jpg", "jpeg"],
key=st.session_state["uploader_key"],
label_visibility="collapsed",
)
for message in st.session_state.messages:
with st.chat_message(message["role"]):
contents = message["content"]
for content in contents:
if content["type"] == "text":
st.markdown(content["text"])
number_of_images = sum(1 for c in contents if c["type"] == "image_url")
if number_of_images > 0:
cols = st.columns(number_of_images)
i = 0
for content in contents:
if content["type"] == "image_url":
with cols[i]:
st.image(content["image_url"]["url"])
i += 1
def push_message(role, content, images=None):
contents = []
contents.append({"type": "text", "text": content})
if images:
for image in images:
image_b64 = im_2_b64(image)
image_url = f"data:image/jpeg;base64,{image_b64.decode('utf-8')}"
obj = {
"type": "image_url",
"image_url": {
"url": image_url,
},
}
contents.append(obj)
message = {"role": role, "content": contents}
st.session_state.messages.append(message)
return message
chat_input_disabled = False
if prompt := st.chat_input("Type a message", key="chat_input", disabled=chat_input_disabled):
push_message("user", prompt, images)
with st.chat_message("user"):
st.markdown(prompt)
if images:
cols = st.columns(len(images))
for i, image in enumerate(images):
with cols[i]:
st.image(image)
with st.chat_message("assistant"):
messages = [
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
]
# print("api call", messages)
chat_input_disabled = True
stream = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=messages,
stream=True,
seed=RANDOM_SEED,
temperature=temperature,
max_tokens=max_tokens,
)
response = st.write_stream(stream)
push_message("assistant", response)
chat_input_disabled = False
clear_uploader()
|