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,129 +148,62 @@ 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 |
-
|
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 |
-
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
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 |
-
|
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"
|
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 |
-
if uploaded_file
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
|
|
|
|
68 |
base64_image = encode_image(uploaded_file)
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
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 |
-
|
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 |
-
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 |
-
#
|
135 |
-
|
136 |
-
|
|
|
|
|
137 |
|
|
|
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 image with a specified width
|
152 |
+
st.image('MTSS.ai_Logo.png', width=300) # Adjusted for consistency
|
|
|
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 |
+
# Initialize placeholders for additional details and complex image details
|
164 |
+
additional_details = ""
|
165 |
+
complex_image_details = ""
|
|
|
|
|
|
|
|
|
|
|
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 |
+
complex_image = st.toggle("Is this a complex image?", value=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
# Button to trigger the analysis
|
177 |
+
analyze_button = st.button("Analyze the Image")
|
178 |
|
179 |
+
# Always visible response area initialized with a placeholder message
|
180 |
+
response_placeholder = st.empty()
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
+
# Display the uploaded image in an expander if an image is uploaded
|
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 |
+
# Ensure that analysis only proceeds when an image is uploaded and the analyze button is pressed
|
188 |
+
if uploaded_file and analyze_button:
|
189 |
+
with st.spinner("Analyzing the image..."):
|
190 |
+
# Encode the image for analysis
|
191 |
base64_image = encode_image(uploaded_file)
|
192 |
|
193 |
+
# Here you would include your logic to determine the prompt based on the complexity of the image
|
194 |
+
# and to generate the full_response using the OpenAI API
|
195 |
+
|
196 |
+
# For demonstration, let's simulate a response
|
197 |
+
full_response = "This is where the analyzed text will be displayed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
+
# Update the response text area with the new full_response
|
200 |
+
response_placeholder.text_area('Response:', value=full_response, height=250, key="response_text_area")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
+
st.success('Analysis complete. Review the generated text for accuracy.')
|
|
|
|
|
|
|
|
|
|
|
203 |
else:
|
204 |
+
# If no image is uploaded, show a waiting message or keep it empty
|
205 |
+
response_placeholder.text_area('Response:', value="Upload an image to analyze.", height=250, key="response_text_area")
|
206 |
+
if analyze_button:
|
207 |
+
st.warning("Please upload an image first.")
|
208 |
+
|
209 |
|