ProfessorLeVesseur commited on
Commit
ca698d1
·
verified ·
1 Parent(s): 94fcb7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -37
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 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_]')
@@ -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 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.")
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.")