Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import base64
|
| 3 |
import openai
|
|
@@ -9,128 +148,60 @@ def encode_image(image_file):
|
|
| 9 |
# Streamlit page setup
|
| 10 |
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
|
| 11 |
|
| 12 |
-
#Add the image with a specified width
|
| 13 |
image_width = 300 # Set the desired width in pixels
|
| 14 |
st.image('MTSS.ai_Logo.png', width=image_width)
|
| 15 |
|
| 16 |
-
# st.title('MTSS:grey[.ai]')
|
| 17 |
st.header('VisionText™ | Accessibility')
|
| 18 |
st.subheader(':green[_Image Alt Text Generator_]')
|
| 19 |
|
| 20 |
# Retrieve the OpenAI API Key from secrets
|
| 21 |
openai.api_key = st.secrets["openai_api_key"]
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
if uploaded_file:
|
| 27 |
-
# Display the uploaded image with specified width
|
| 28 |
-
image_width = 100 # Set the desired width in pixels
|
| 29 |
-
with st.expander("Image", expanded=True):
|
| 30 |
-
st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
|
| 31 |
|
| 32 |
# Toggle for showing additional details input
|
| 33 |
-
show_details = st.toggle("Add details about the image.
|
| 34 |
|
|
|
|
|
|
|
| 35 |
if show_details:
|
| 36 |
-
# Text input for additional details about the image, shown only if toggle is True
|
| 37 |
additional_details = st.text_area(
|
| 38 |
-
"The details could include specific information that is important to include in the alt text or reflect why the image is being used:"
|
| 39 |
-
disabled=not show_details
|
| 40 |
)
|
| 41 |
|
| 42 |
# Toggle for modifying the prompt for complex images
|
| 43 |
-
complex_image = st.toggle("Is this a complex image?
|
| 44 |
-
|
| 45 |
-
if complex_image:
|
| 46 |
-
# Text input for additional details about the image, shown only if toggle is True
|
| 47 |
-
complex_image_details = st.caption(
|
| 48 |
-
"By clicking this toggle, it will inform MTSS.ai to create a description that exceeds the 125 character limit. "
|
| 49 |
-
"Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box. "
|
| 50 |
-
)
|
| 51 |
|
| 52 |
# Button to trigger the analysis
|
| 53 |
-
analyze_button = st.button("Analyze the Image"
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
"As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
|
| 58 |
-
"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. "
|
| 59 |
-
"Skip phrases like 'image of' or 'picture of.' "
|
| 60 |
-
"Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
base64_image = encode_image(uploaded_file)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
if complex_image:
|
| 72 |
-
prompt_text = complex_image_prompt_text
|
| 73 |
-
else:
|
| 74 |
-
prompt_text = (
|
| 75 |
-
"As an expert in image accessibility and alternative text, succinctly describe the image provided in less than 125 characters. "
|
| 76 |
-
"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. "
|
| 77 |
-
"Skip phrases like 'image of' or 'picture of.' "
|
| 78 |
-
"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."
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
if show_details and additional_details:
|
| 82 |
-
prompt_text += (
|
| 83 |
-
f"\n\nAdditional Context Provided by the User:\n{additional_details}"
|
| 84 |
-
)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
# Create the payload for the completion request
|
| 88 |
-
messages = [
|
| 89 |
-
{
|
| 90 |
-
"role": "user",
|
| 91 |
-
"content": [
|
| 92 |
-
{"type": "text", "text": prompt_text},
|
| 93 |
-
{
|
| 94 |
-
"type": "image_url",
|
| 95 |
-
"image_url": f"data:image/jpeg;base64,{base64_image}",
|
| 96 |
-
},
|
| 97 |
-
],
|
| 98 |
-
}
|
| 99 |
-
]
|
| 100 |
-
|
| 101 |
-
# Make the request to the OpenAI API
|
| 102 |
-
try:
|
| 103 |
-
# Without Stream
|
| 104 |
-
|
| 105 |
-
# response = openai.chat.completions.create(
|
| 106 |
-
# model="gpt-4-vision-preview", messages=messages, max_tokens=250, stream=False
|
| 107 |
-
# )
|
| 108 |
-
|
| 109 |
-
# Stream the response
|
| 110 |
-
full_response = ""
|
| 111 |
-
message_placeholder = st.empty()
|
| 112 |
-
for completion in openai.chat.completions.create(
|
| 113 |
-
model="gpt-4-vision-preview", messages=messages,
|
| 114 |
-
max_tokens=250, stream=True
|
| 115 |
-
):
|
| 116 |
-
# # Check if there is content to display
|
| 117 |
-
# if completion.choices[0].delta.content is not None:
|
| 118 |
-
# full_response += completion.choices[0].delta.content
|
| 119 |
-
# message_placeholder.markdown(full_response + "▌")
|
| 120 |
-
# # Final update to placeholder after the stream ends
|
| 121 |
-
# message_placeholder.markdown(full_response) # stream text
|
| 122 |
-
|
| 123 |
-
# Check if there is content to display
|
| 124 |
-
if completion.choices[0].delta.content is not None:
|
| 125 |
-
full_response += completion.choices[0].delta.content
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
except Exception as e:
|
| 132 |
-
st.error(f"An error occurred: {e}")
|
| 133 |
else:
|
| 134 |
-
# Warnings for user action required
|
| 135 |
if not uploaded_file and analyze_button:
|
| 136 |
-
st.warning("Please upload an image.")
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import base64
|
| 3 |
+
# import openai
|
| 4 |
+
|
| 5 |
+
# # Function to encode the image to base64
|
| 6 |
+
# def encode_image(image_file):
|
| 7 |
+
# return base64.b64encode(image_file.getvalue()).decode("utf-8")
|
| 8 |
+
|
| 9 |
+
# # Streamlit page setup
|
| 10 |
+
# st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
|
| 11 |
+
|
| 12 |
+
# #Add the image with a specified width
|
| 13 |
+
# image_width = 300 # Set the desired width in pixels
|
| 14 |
+
# st.image('MTSS.ai_Logo.png', width=image_width)
|
| 15 |
+
|
| 16 |
+
# # st.title('MTSS:grey[.ai]')
|
| 17 |
+
# st.header('VisionText™ | Accessibility')
|
| 18 |
+
# st.subheader(':green[_Image Alt Text Generator_]')
|
| 19 |
+
|
| 20 |
+
# # Retrieve the OpenAI API Key from secrets
|
| 21 |
+
# openai.api_key = st.secrets["openai_api_key"]
|
| 22 |
+
|
| 23 |
+
# # File uploader allows user to add their own image
|
| 24 |
+
# uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 25 |
+
|
| 26 |
+
# if uploaded_file:
|
| 27 |
+
# # Display the uploaded image with specified width
|
| 28 |
+
# image_width = 100 # Set the desired width in pixels
|
| 29 |
+
# with st.expander("Image", expanded=True):
|
| 30 |
+
# st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
|
| 31 |
+
|
| 32 |
+
# # Toggle for showing additional details input
|
| 33 |
+
# show_details = st.toggle("Add details about the image. ", value=False)
|
| 34 |
+
|
| 35 |
+
# if show_details:
|
| 36 |
+
# # Text input for additional details about the image, shown only if toggle is True
|
| 37 |
+
# additional_details = st.text_area(
|
| 38 |
+
# "The details could include specific information that is important to include in the alt text or reflect why the image is being used:",
|
| 39 |
+
# disabled=not show_details
|
| 40 |
+
# )
|
| 41 |
+
|
| 42 |
+
# # Toggle for modifying the prompt for complex images
|
| 43 |
+
# complex_image = st.toggle("Is this a complex image? ", value=False)
|
| 44 |
+
|
| 45 |
+
# if complex_image:
|
| 46 |
+
# # Text input for additional details about the image, shown only if toggle is True
|
| 47 |
+
# complex_image_details = st.caption(
|
| 48 |
+
# "By clicking this toggle, it will inform MTSS.ai to create a description that exceeds the 125 character limit. "
|
| 49 |
+
# "Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box. "
|
| 50 |
+
# )
|
| 51 |
+
|
| 52 |
+
# # Button to trigger the analysis
|
| 53 |
+
# analyze_button = st.button("Analyze the Image", type="secondary")
|
| 54 |
+
|
| 55 |
+
# # Optimized prompt for complex images
|
| 56 |
+
# complex_image_prompt_text = (
|
| 57 |
+
# "As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
|
| 58 |
+
# "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. "
|
| 59 |
+
# "Skip phrases like 'image of' or 'picture of.' "
|
| 60 |
+
# "Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
|
| 61 |
+
# )
|
| 62 |
+
|
| 63 |
+
# # Check if an image has been uploaded, if the API key is available, and if the button has been pressed
|
| 64 |
+
# if uploaded_file is not None and analyze_button:
|
| 65 |
+
|
| 66 |
+
# with st.spinner("Analyzing the image ..."):
|
| 67 |
+
# # Encode the image
|
| 68 |
+
# base64_image = encode_image(uploaded_file)
|
| 69 |
+
|
| 70 |
+
# # Determine which prompt to use based on the complexity of the image
|
| 71 |
+
# if complex_image:
|
| 72 |
+
# prompt_text = complex_image_prompt_text
|
| 73 |
+
# else:
|
| 74 |
+
# prompt_text = (
|
| 75 |
+
# "As an expert in image accessibility and alternative text, succinctly describe the image provided in less than 125 characters. "
|
| 76 |
+
# "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. "
|
| 77 |
+
# "Skip phrases like 'image of' or 'picture of.' "
|
| 78 |
+
# "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."
|
| 79 |
+
# )
|
| 80 |
+
|
| 81 |
+
# if show_details and additional_details:
|
| 82 |
+
# prompt_text += (
|
| 83 |
+
# f"\n\nAdditional Context Provided by the User:\n{additional_details}"
|
| 84 |
+
# )
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# # Create the payload for the completion request
|
| 88 |
+
# messages = [
|
| 89 |
+
# {
|
| 90 |
+
# "role": "user",
|
| 91 |
+
# "content": [
|
| 92 |
+
# {"type": "text", "text": prompt_text},
|
| 93 |
+
# {
|
| 94 |
+
# "type": "image_url",
|
| 95 |
+
# "image_url": f"data:image/jpeg;base64,{base64_image}",
|
| 96 |
+
# },
|
| 97 |
+
# ],
|
| 98 |
+
# }
|
| 99 |
+
# ]
|
| 100 |
+
|
| 101 |
+
# # Make the request to the OpenAI API
|
| 102 |
+
# try:
|
| 103 |
+
# # Without Stream
|
| 104 |
+
|
| 105 |
+
# # response = openai.chat.completions.create(
|
| 106 |
+
# # model="gpt-4-vision-preview", messages=messages, max_tokens=250, stream=False
|
| 107 |
+
# # )
|
| 108 |
+
|
| 109 |
+
# # Stream the response
|
| 110 |
+
# full_response = ""
|
| 111 |
+
# message_placeholder = st.empty()
|
| 112 |
+
# for completion in openai.chat.completions.create(
|
| 113 |
+
# model="gpt-4-vision-preview", messages=messages,
|
| 114 |
+
# max_tokens=250, stream=True
|
| 115 |
+
# ):
|
| 116 |
+
# # # Check if there is content to display
|
| 117 |
+
# # if completion.choices[0].delta.content is not None:
|
| 118 |
+
# # full_response += completion.choices[0].delta.content
|
| 119 |
+
# # message_placeholder.markdown(full_response + "▌")
|
| 120 |
+
# # # Final update to placeholder after the stream ends
|
| 121 |
+
# # message_placeholder.markdown(full_response) # stream text
|
| 122 |
+
|
| 123 |
+
# # Check if there is content to display
|
| 124 |
+
# if completion.choices[0].delta.content is not None:
|
| 125 |
+
# full_response += completion.choices[0].delta.content
|
| 126 |
+
|
| 127 |
+
# # Display the response in a text area
|
| 128 |
+
# st.text_area('Response:', value=full_response, height=250, key="response_text_area")
|
| 129 |
+
|
| 130 |
+
# st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
| 131 |
+
# except Exception as e:
|
| 132 |
+
# st.error(f"An error occurred: {e}")
|
| 133 |
+
# else:
|
| 134 |
+
# # Warnings for user action required
|
| 135 |
+
# if not uploaded_file and analyze_button:
|
| 136 |
+
# st.warning("Please upload an image.")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
import streamlit as st
|
| 141 |
import base64
|
| 142 |
import openai
|
|
|
|
| 148 |
# Streamlit page setup
|
| 149 |
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
|
| 150 |
|
| 151 |
+
# Add the logo 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.header('VisionText™ | Accessibility')
|
| 156 |
st.subheader(':green[_Image Alt Text Generator_]')
|
| 157 |
|
| 158 |
# Retrieve the OpenAI API Key from secrets
|
| 159 |
openai.api_key = st.secrets["openai_api_key"]
|
| 160 |
|
| 161 |
+
# Initialize an empty string for the full_response to ensure the text area is always displayed
|
| 162 |
+
full_response = "Awaiting analysis..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Toggle for showing additional details input
|
| 165 |
+
show_details = st.toggle("Add details about the image.", value=False)
|
| 166 |
|
| 167 |
+
# Text input for additional details about the image, shown based on the toggle state
|
| 168 |
+
additional_details = ""
|
| 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 |
# Toggle for modifying the prompt for complex images
|
| 175 |
+
complex_image = st.toggle("Is this a complex image?", value=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# Button to trigger the analysis
|
| 178 |
+
analyze_button = st.button("Analyze the Image")
|
| 179 |
|
| 180 |
+
# Display the response in a text area
|
| 181 |
+
response_text_area = st.text_area('Response:', value=full_response, height=250, key="response_text_area")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
# File uploader allows user to add their own image
|
| 184 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 185 |
+
|
| 186 |
+
if uploaded_file:
|
| 187 |
+
# Display the uploaded image with specified width in an expander
|
| 188 |
+
with st.expander("Image Preview", expanded=True):
|
| 189 |
+
st.image(uploaded_file, caption=uploaded_file.name, width=100, use_column_width=False)
|
| 190 |
|
| 191 |
+
# Ensure that analysis only proceeds when an image is uploaded and the analyze button is pressed
|
| 192 |
+
if uploaded_file is not None and analyze_button:
|
| 193 |
+
with st.spinner("Analyzing the image..."):
|
| 194 |
+
# Encode the image for analysis
|
| 195 |
base64_image = encode_image(uploaded_file)
|
| 196 |
|
| 197 |
+
# Logic to set prompt_text based on complex_image toggle state and append additional_details if provided
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
# Your OpenAI API call and handling logic here to update full_response based on the analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
# Update the response text area with the new full_response
|
| 202 |
+
response_text_area.text_area('Response:', value=full_response, height=250, key="response_text_area")
|
| 203 |
+
|
| 204 |
+
st.success('Analysis complete. Review the generated text for accuracy.')
|
|
|
|
|
|
|
| 205 |
else:
|
|
|
|
| 206 |
if not uploaded_file and analyze_button:
|
| 207 |
+
st.warning("Please upload an image to proceed with the analysis.")
|