File size: 5,530 Bytes
8d37eb3
 
3643ac5
3d98680
 
8d37eb3
38a9e38
 
8d37eb3
 
d039555
 
 
 
 
 
 
 
 
 
 
8d37eb3
 
 
 
38a9e38
 
8d37eb3
ce223ca
8394675
 
 
38a9e38
 
 
3d98680
 
 
38a9e38
 
 
 
 
3d98680
8394675
 
3d98680
8d37eb3
d039555
a742296
 
 
7c85284
8394675
3d98680
8394675
 
7c85284
3643ac5
7c85284
 
 
 
8d37eb3
b2766e7
 
8394675
b2766e7
8394675
 
 
b2766e7
 
 
45ccb5b
b2766e7
 
 
45ccb5b
b2766e7
8d37eb3
38a9e38
 
 
 
 
 
 
 
 
 
8469de9
 
8d37eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69e0201
a742296
 
 
8d37eb3
 
 
 
 
 
 
 
 
8469de9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from openai import OpenAI
import streamlit as st
from utils import im_2_b64, calculate_cost, clear_uploader, undo, restart
import pickle
from upload import upload_file, get_file

share_keys = ["messages", "cost"]

client = OpenAI(api_key=st.secrets["OPENAI_KEY"])

st.set_page_config(
    page_title="ChatGPT with Vision",
    page_icon="🤖",
    menu_items={
        "About": """
            *ChatGPT with Vision* is a chat interface that uses OpenAI's API to generate responses to your prompts.  
            *---- Developed by **[Shubhashis Roy Dipta](https://roydipta.com)** ----*
        """
    }
)

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
if "cost" not in st.session_state:
    st.session_state.cost = []

if len(st.session_state.messages) == 0 and "id" in st.query_params:
    with st.spinner("Loading chat..."):
        id = st.query_params["id"]
        data = get_file(id, 'chatgpt-vision-007')
        obj = pickle.loads(data)
        for k, v in obj.items():
            st.session_state[k] = v


def share():
    obj = {}
    for k in share_keys:
        if k in st.session_state:
            obj[k] = st.session_state[k]
    data = pickle.dumps(obj)
    id = upload_file(data, 'chatgpt-vision-007')
    url = f"https://umbc-nlp-chatgpt-vision.hf.space/?id={id}"
    st.success(f"Share URL: {url}")

with st.sidebar:
    st.title(":blue[ChatGPT with Vision]")

    password = st.text_input("Password", type="password")

    if st.button("Share", use_container_width=True):
        share()

    cols = st.columns(2)
    with cols[0]:
        if st.button("Restart", type="primary", use_container_width=True):
            restart()
    
    with cols[1]:
        if st.button("Undo", use_container_width=True):
            undo()

    with st.expander("Advanced Configuration"):
        st.subheader("Temperature")
        temperature = st.slider(label="x", min_value=0.1, max_value=1.0, value=0.2, step=0.1, label_visibility='collapsed')
        st.subheader("Max Tokens")
        max_tokens = st.slider(label="x", min_value=32, max_value=1024, value=256, step=32, label_visibility='collapsed')
        st.subheader("Random Seed")
        random_seed = st.number_input("Seed", min_value=0, max_value=1000000, value=42, step=1, label_visibility='collapsed')

    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",
        )

    with st.expander(f"Total Cost: ${sum(st.session_state.cost):.10f}"):
        if len(st.session_state.cost) > 0:
            st.subheader("Cost Breakdown")
            for i, c in enumerate(st.session_state.cost):
                st.write(f"Message {i+1}: ${c:.10f}")
            st.markdown("---")
            st.write(f"Total: ${sum(st.session_state.cost):.10f}")
        else:
            st.write("No cost incurred yet")

    append = st.checkbox("Append to previous message", value=False)


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

if prompt := st.chat_input("Type a message", key="chat_input"):
    if password != st.secrets["PASSWORD"]:
        st.error("Invalid password. Demo is read-only.")
        st.stop()
    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)

    if not append:
        with st.chat_message("assistant"):
            messages = [
                {"role": m["role"], "content": m["content"]}
                for m in st.session_state.messages
            ]
            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)
        calculate_cost()
    clear_uploader()