Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -146,64 +146,134 @@ def encode_image(image_file):
|
|
146 |
return base64.b64encode(image_file.getvalue()).decode("utf-8")
|
147 |
|
148 |
# Streamlit page setup
|
149 |
-
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="
|
150 |
|
151 |
-
#
|
152 |
-
|
|
|
153 |
|
|
|
154 |
st.header('VisionText™ | Accessibility')
|
155 |
st.subheader(':green[_Image Alt Text Generator_]')
|
156 |
|
157 |
# Retrieve the OpenAI API Key from secrets
|
158 |
openai.api_key = st.secrets["openai_api_key"]
|
159 |
|
|
|
|
|
|
|
160 |
# Move the file uploader to the sidebar
|
161 |
uploaded_file = st.sidebar.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
-
# Define toggles and input areas for additional details in the main area, not the sidebar
|
168 |
-
show_details = st.toggle("Add details about the image.", value=False)
|
169 |
if show_details:
|
|
|
170 |
additional_details = st.text_area(
|
171 |
-
"The details could include specific information that is important to include in the alt text or reflect why the image is being used:"
|
|
|
172 |
)
|
173 |
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
# Button to trigger the analysis
|
177 |
-
analyze_button = st.button("Analyze the Image")
|
178 |
|
179 |
-
#
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
-
#
|
183 |
-
if uploaded_file:
|
184 |
-
with st.expander("Uploaded Image Preview"):
|
185 |
-
st.image(uploaded_file, caption="Uploaded Image", width=300) # Display uploaded image with adjusted width
|
186 |
|
187 |
-
|
188 |
-
|
189 |
-
with st.spinner("Analyzing the image..."):
|
190 |
-
# Encode the image for analysis
|
191 |
base64_image = encode_image(uploaded_file)
|
192 |
|
193 |
-
#
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
else:
|
204 |
-
#
|
205 |
-
|
206 |
-
|
207 |
-
|
|
|
|
|
208 |
|
209 |
|
|
|
146 |
return base64.b64encode(image_file.getvalue()).decode("utf-8")
|
147 |
|
148 |
# Streamlit page setup
|
149 |
+
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="expanded")
|
150 |
|
151 |
+
#Add the image with a specified width
|
152 |
+
image_width = 300 # Set the desired width in pixels
|
153 |
+
st.image('MTSS.ai_Logo.png', width=image_width)
|
154 |
|
155 |
+
# st.title('MTSS:grey[.ai]')
|
156 |
st.header('VisionText™ | Accessibility')
|
157 |
st.subheader(':green[_Image Alt Text Generator_]')
|
158 |
|
159 |
# Retrieve the OpenAI API Key from secrets
|
160 |
openai.api_key = st.secrets["openai_api_key"]
|
161 |
|
162 |
+
# File uploader allows user to add their own image
|
163 |
+
# uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
164 |
+
|
165 |
# Move the file uploader to the sidebar
|
166 |
uploaded_file = st.sidebar.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
167 |
|
168 |
+
if uploaded_file:
|
169 |
+
# Display the uploaded image with specified width
|
170 |
+
image_width = 100 # Set the desired width in pixels
|
171 |
+
with st.expander("Image", expanded=True):
|
172 |
+
st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
|
173 |
+
|
174 |
+
# Toggle for showing additional details input
|
175 |
+
show_details = st.toggle("Add details about the image. ", value=False)
|
176 |
|
|
|
|
|
177 |
if show_details:
|
178 |
+
# Text input for additional details about the image, shown only if toggle is True
|
179 |
additional_details = st.text_area(
|
180 |
+
"The details could include specific information that is important to include in the alt text or reflect why the image is being used:",
|
181 |
+
disabled=not show_details
|
182 |
)
|
183 |
|
184 |
+
# Toggle for modifying the prompt for complex images
|
185 |
+
complex_image = st.toggle("Is this a complex image? ", value=False)
|
186 |
+
|
187 |
+
if complex_image:
|
188 |
+
# Text input for additional details about the image, shown only if toggle is True
|
189 |
+
complex_image_details = st.caption(
|
190 |
+
"By clicking this toggle, it will inform MTSS.ai to create a description that exceeds the 125 character limit. "
|
191 |
+
"Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box. "
|
192 |
+
)
|
193 |
|
194 |
# Button to trigger the analysis
|
195 |
+
analyze_button = st.button("Analyze the Image", type="secondary")
|
196 |
|
197 |
+
# Optimized prompt for complex images
|
198 |
+
complex_image_prompt_text = (
|
199 |
+
"As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
|
200 |
+
"Provide a brief description using not more than 500 characters that convey the essential information conveyed by the image in eight or fewer clear and concise sentences. "
|
201 |
+
"Skip phrases like 'image of' or 'picture of.' "
|
202 |
+
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
|
203 |
+
)
|
204 |
|
205 |
+
# Check if an image has been uploaded, if the API key is available, and if the button has been pressed
|
206 |
+
if uploaded_file is not None and analyze_button:
|
|
|
|
|
207 |
|
208 |
+
with st.spinner("Analyzing the image ..."):
|
209 |
+
# Encode the image
|
|
|
|
|
210 |
base64_image = encode_image(uploaded_file)
|
211 |
|
212 |
+
# Determine which prompt to use based on the complexity of the image
|
213 |
+
if complex_image:
|
214 |
+
prompt_text = complex_image_prompt_text
|
215 |
+
else:
|
216 |
+
prompt_text = (
|
217 |
+
"As an expert in image accessibility and alternative text, succinctly describe the image provided in less than 125 characters. "
|
218 |
+
"Provide a brief description using not more than 125 characters that convey the essential information conveyed by the image in three or fewer clear and concise sentences for use as alt text. "
|
219 |
+
"Skip phrases like 'image of' or 'picture of.' "
|
220 |
+
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points and newlines, focusing on creating a seamless narrative that serves as effective alternative text for accessibility purposes."
|
221 |
+
)
|
222 |
+
|
223 |
+
if show_details and additional_details:
|
224 |
+
prompt_text += (
|
225 |
+
f"\n\nAdditional Context Provided by the User:\n{additional_details}"
|
226 |
+
)
|
227 |
|
228 |
+
|
229 |
+
# Create the payload for the completion request
|
230 |
+
messages = [
|
231 |
+
{
|
232 |
+
"role": "user",
|
233 |
+
"content": [
|
234 |
+
{"type": "text", "text": prompt_text},
|
235 |
+
{
|
236 |
+
"type": "image_url",
|
237 |
+
"image_url": f"data:image/jpeg;base64,{base64_image}",
|
238 |
+
},
|
239 |
+
],
|
240 |
+
}
|
241 |
+
]
|
242 |
+
|
243 |
+
# Make the request to the OpenAI API
|
244 |
+
try:
|
245 |
+
# Without Stream
|
246 |
+
|
247 |
+
# response = openai.chat.completions.create(
|
248 |
+
# model="gpt-4-vision-preview", messages=messages, max_tokens=250, stream=False
|
249 |
+
# )
|
250 |
+
|
251 |
+
# Stream the response
|
252 |
+
full_response = ""
|
253 |
+
message_placeholder = st.empty()
|
254 |
+
for completion in client.chat.completions.create(
|
255 |
+
model="gpt-4-vision-preview", messages=messages,
|
256 |
+
max_tokens=1200, stream=True
|
257 |
+
):
|
258 |
+
# Check if there is content to display
|
259 |
+
if completion.choices[0].delta.content is not None:
|
260 |
+
full_response += completion.choices[0].delta.content
|
261 |
+
message_placeholder.markdown(full_response + "▌")
|
262 |
+
# Final update to placeholder after the stream ends
|
263 |
+
message_placeholder.markdown(full_response)
|
264 |
+
|
265 |
+
# # Display the response in a text area
|
266 |
+
# st.text_area('Response:', value=full_response, height=250, key="response_text_area")
|
267 |
+
|
268 |
+
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
269 |
+
except Exception as e:
|
270 |
+
st.error(f"An error occurred: {e}")
|
271 |
else:
|
272 |
+
# Warnings for user action required
|
273 |
+
if not uploaded_file and analyze_button:
|
274 |
+
st.warning("Please upload an image.")
|
275 |
+
|
276 |
+
|
277 |
+
|
278 |
|
279 |
|