Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,10 @@ import base64
|
|
6 |
|
7 |
st.set_page_config(page_title='GPT-4 Vision', page_icon='👁️')
|
8 |
|
9 |
-
if '
|
10 |
st.session_state['history'] = [{'role': 'system', 'content': ''}]
|
11 |
st.session_state['cost'] = 0.0
|
12 |
-
st.session_state['
|
13 |
|
14 |
st.markdown('# GPT-4 Vision Client')
|
15 |
api_key = st.text_input('OpenAI API Key', '', type='password')
|
@@ -49,8 +49,8 @@ with chatTab:
|
|
49 |
st.markdown('Assistant: ' + msg_content)
|
50 |
|
51 |
# get user inputs
|
52 |
-
text_input = st.text_input('Prompt', '')
|
53 |
-
img_input = st.file_uploader('Images', accept_multiple_files=True)
|
54 |
|
55 |
# set up button layout
|
56 |
st.markdown(
|
@@ -74,45 +74,49 @@ with chatTab:
|
|
74 |
# send api request
|
75 |
with cols[0]:
|
76 |
if st.button('Send'):
|
77 |
-
if api_key:
|
78 |
-
if text_input or img_input:
|
79 |
-
msg = {'role': 'user', 'content': []}
|
80 |
-
if text_input:
|
81 |
-
msg['content'].append({'type': 'text', 'text': text_input})
|
82 |
-
for img in img_input:
|
83 |
-
encoded_img = base64.b64encode(img.read()).decode('utf-8')
|
84 |
-
msg['content'].append(
|
85 |
-
{
|
86 |
-
'type': 'image_url',
|
87 |
-
'image_url': {
|
88 |
-
'url': f'data:image/jpeg;base64,{encoded_img}',
|
89 |
-
'detail': image_detail
|
90 |
-
}
|
91 |
-
}
|
92 |
-
)
|
93 |
-
st.session_state['history'].append(msg)
|
94 |
-
history = (
|
95 |
-
st.session_state['history']
|
96 |
-
if st.session_state['history'][0]['content']
|
97 |
-
else st.session_state['history'][1:]
|
98 |
-
)
|
99 |
-
client = OpenAI(api_key=api_key)
|
100 |
-
response = client.chat.completions.create(
|
101 |
-
model='gpt-4-vision-preview',
|
102 |
-
temperature=temperature,
|
103 |
-
max_tokens=max_tokens,
|
104 |
-
messages=history
|
105 |
-
)
|
106 |
-
st.session_state['history'].append(
|
107 |
-
{'role': 'assistant', 'content': response.choices[0].message.content}
|
108 |
-
)
|
109 |
-
st.session_state['cost'] += response.usage.prompt_tokens * 0.01 / 1000
|
110 |
-
st.session_state['cost'] += response.usage.completion_tokens * 0.03 / 1000
|
111 |
-
st.rerun()
|
112 |
-
else:
|
113 |
-
st.warning('You can\'t just send nothing!')
|
114 |
-
else:
|
115 |
st.warning('API Key required')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
# clear chat history
|
118 |
with cols[1]:
|
|
|
6 |
|
7 |
st.set_page_config(page_title='GPT-4 Vision', page_icon='👁️')
|
8 |
|
9 |
+
if 'history' not in st.session_state:
|
10 |
st.session_state['history'] = [{'role': 'system', 'content': ''}]
|
11 |
st.session_state['cost'] = 0.0
|
12 |
+
st.session_state['counters'] = [0, 1]
|
13 |
|
14 |
st.markdown('# GPT-4 Vision Client')
|
15 |
api_key = st.text_input('OpenAI API Key', '', type='password')
|
|
|
49 |
st.markdown('Assistant: ' + msg_content)
|
50 |
|
51 |
# get user inputs
|
52 |
+
text_input = st.text_input('Prompt', '', key=st.session_state['counters'][0])
|
53 |
+
img_input = st.file_uploader('Images', accept_multiple_files=True, key=st.session_state['counters'][1])
|
54 |
|
55 |
# set up button layout
|
56 |
st.markdown(
|
|
|
74 |
# send api request
|
75 |
with cols[0]:
|
76 |
if st.button('Send'):
|
77 |
+
if not api_key:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
st.warning('API Key required')
|
79 |
+
st.stop()
|
80 |
+
if not (text_input or img_input):
|
81 |
+
st.warning('You can\'t just send nothing!')
|
82 |
+
st.stop()
|
83 |
+
msg = {'role': 'user', 'content': []}
|
84 |
+
if text_input:
|
85 |
+
msg['content'].append({'type': 'text', 'text': text_input})
|
86 |
+
for img in img_input:
|
87 |
+
if img.name.split('.')[-1].lower() not in ['png', 'jpg', 'jpeg', 'gif', 'webp']:
|
88 |
+
st.warning('Only .jpg, .png, .gif, or .webp are supported')
|
89 |
+
st.stop()
|
90 |
+
encoded_img = base64.b64encode(img.read()).decode('utf-8')
|
91 |
+
msg['content'].append(
|
92 |
+
{
|
93 |
+
'type': 'image_url',
|
94 |
+
'image_url': {
|
95 |
+
'url': f'data:image/jpeg;base64,{encoded_img}',
|
96 |
+
'detail': image_detail
|
97 |
+
}
|
98 |
+
}
|
99 |
+
)
|
100 |
+
st.session_state['history'].append(msg)
|
101 |
+
history = (
|
102 |
+
st.session_state['history']
|
103 |
+
if st.session_state['history'][0]['content']
|
104 |
+
else st.session_state['history'][1:]
|
105 |
+
)
|
106 |
+
client = OpenAI(api_key=api_key)
|
107 |
+
response = client.chat.completions.create(
|
108 |
+
model='gpt-4-vision-preview',
|
109 |
+
temperature=temperature,
|
110 |
+
max_tokens=max_tokens,
|
111 |
+
messages=history
|
112 |
+
)
|
113 |
+
st.session_state['history'].append(
|
114 |
+
{'role': 'assistant', 'content': response.choices[0].message.content}
|
115 |
+
)
|
116 |
+
st.session_state['cost'] += response.usage.prompt_tokens * 0.01 / 1000
|
117 |
+
st.session_state['cost'] += response.usage.completion_tokens * 0.03 / 1000
|
118 |
+
st.session_state['counters'] = [i+2 for i in st.session_state['counters']]
|
119 |
+
st.rerun()
|
120 |
|
121 |
# clear chat history
|
122 |
with cols[1]:
|