Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -136,7 +136,6 @@
|
|
| 136 |
# st.warning("Please upload an image.")
|
| 137 |
|
| 138 |
|
| 139 |
-
|
| 140 |
import streamlit as st
|
| 141 |
import base64
|
| 142 |
import openai
|
|
@@ -148,9 +147,8 @@ def encode_image(image_file):
|
|
| 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
|
| 152 |
-
|
| 153 |
-
st.image('MTSS.ai_Logo.png', width=image_width)
|
| 154 |
|
| 155 |
st.header('VisionText™ | Accessibility')
|
| 156 |
st.subheader(':green[_Image Alt Text Generator_]')
|
|
@@ -158,51 +156,62 @@ st.subheader(':green[_Image Alt Text Generator_]')
|
|
| 158 |
# Retrieve the OpenAI API Key from secrets
|
| 159 |
openai.api_key = st.secrets["openai_api_key"]
|
| 160 |
|
| 161 |
-
# Initialize
|
| 162 |
-
|
|
|
|
| 163 |
|
| 164 |
-
#
|
| 165 |
-
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
|
| 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 |
-
#
|
| 178 |
analyze_button = st.button("Analyze the Image")
|
| 179 |
|
| 180 |
-
#
|
| 181 |
-
|
| 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
|
| 188 |
with st.expander("Image Preview", expanded=True):
|
| 189 |
-
st.image(uploaded_file, caption=uploaded_file.name, width=100
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
else:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
| 136 |
# st.warning("Please upload an image.")
|
| 137 |
|
| 138 |
|
|
|
|
| 139 |
import streamlit as st
|
| 140 |
import base64
|
| 141 |
import openai
|
|
|
|
| 147 |
# Streamlit page setup
|
| 148 |
st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
|
| 149 |
|
| 150 |
+
# Add the image with a specified width
|
| 151 |
+
st.image('MTSS.ai_Logo.png', width=300) # Adjusted for consistency
|
|
|
|
| 152 |
|
| 153 |
st.header('VisionText™ | Accessibility')
|
| 154 |
st.subheader(':green[_Image Alt Text Generator_]')
|
|
|
|
| 156 |
# Retrieve the OpenAI API Key from secrets
|
| 157 |
openai.api_key = st.secrets["openai_api_key"]
|
| 158 |
|
| 159 |
+
# Initialize placeholders for user inputs
|
| 160 |
+
additional_details = ""
|
| 161 |
+
complex_image_details = ""
|
| 162 |
|
| 163 |
+
# Place the file uploader at the start
|
| 164 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 165 |
|
| 166 |
+
# Define toggles and input areas for additional details
|
| 167 |
+
show_details = st.toggle("Add details about the image.", value=False)
|
| 168 |
if show_details:
|
| 169 |
additional_details = st.text_area(
|
| 170 |
"The details could include specific information that is important to include in the alt text or reflect why the image is being used:"
|
| 171 |
)
|
| 172 |
|
|
|
|
| 173 |
complex_image = st.toggle("Is this a complex image?", value=False)
|
| 174 |
|
| 175 |
+
# Analyze button
|
| 176 |
analyze_button = st.button("Analyze the Image")
|
| 177 |
|
| 178 |
+
# Always visible response area initialized with a placeholder message
|
| 179 |
+
response_placeholder = st.empty()
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
# Display uploaded image and analyze only if an image is uploaded and the button is pressed
|
| 182 |
if uploaded_file:
|
| 183 |
+
# Display the uploaded image with specified width
|
| 184 |
with st.expander("Image Preview", expanded=True):
|
| 185 |
+
st.image(uploaded_file, caption=uploaded_file.name, width=100) # Adjusted width for consistency
|
| 186 |
+
|
| 187 |
+
if analyze_button:
|
| 188 |
+
with st.spinner("Analyzing the image..."):
|
| 189 |
+
# Encode the image
|
| 190 |
+
base64_image = encode_image(uploaded_file)
|
| 191 |
+
|
| 192 |
+
# Prepare the prompt text based on the complexity toggle and additional details
|
| 193 |
+
prompt_text = "Your prompt here based on the toggles' states."
|
| 194 |
+
|
| 195 |
+
# Example payload for the OpenAI API call
|
| 196 |
+
messages = [
|
| 197 |
+
{
|
| 198 |
+
"role": "user",
|
| 199 |
+
"content": prompt_text,
|
| 200 |
+
"image_url": f"data:image/jpeg;base64,{base64_image}",
|
| 201 |
+
}
|
| 202 |
+
]
|
| 203 |
+
|
| 204 |
+
try:
|
| 205 |
+
# Simulate an OpenAI API call and response
|
| 206 |
+
full_response = "Simulated response based on the analysis."
|
| 207 |
+
|
| 208 |
+
# Update the response text area
|
| 209 |
+
response_placeholder.text_area('Response:', value=full_response, height=250, key="response_text_area")
|
| 210 |
+
st.success('Analysis complete.')
|
| 211 |
+
except Exception as e:
|
| 212 |
+
st.error(f"An error occurred: {e}")
|
| 213 |
else:
|
| 214 |
+
# If no image is uploaded, show a waiting message or keep it empty
|
| 215 |
+
response_placeholder.text_area('Response:', value="Upload an image to analyze.", height=250, key="response_text_area")
|
| 216 |
+
if analyze_button:
|
| 217 |
+
st.warning("Please upload an image first.")
|