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