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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -191
app.py CHANGED
@@ -1,141 +1,3 @@
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
  import streamlit as st
140
  import base64
141
  import openai
@@ -147,71 +9,130 @@ def encode_image(image_file):
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_]')
 
 
155
 
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.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import base64
3
  import openai
 
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.header('VisionText™ | Image Alt Text Generator')
19
+ # st.subheader(':green[_Image Alt Text Generator_]')
20
 
21
  # Retrieve the OpenAI API Key from secrets
22
  openai.api_key = st.secrets["openai_api_key"]
23
 
24
+ # File uploader allows user to add their own image
 
 
 
 
25
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
26
 
27
+ if uploaded_file:
28
+ # Display the uploaded image with specified width
29
+ image_width = 100 # Set the desired width in pixels
30
+ with st.expander("Image", expanded=True):
31
+ st.image(uploaded_file, caption=uploaded_file.name, width=image_width, use_column_width=False)
32
+
33
+ # Toggle for showing additional details input
34
+ show_details = st.toggle("Add details about the image. ", value=False)
35
+
36
  if show_details:
37
+ # Text input for additional details about the image, shown only if toggle is True
38
  additional_details = st.text_area(
39
+ "The details could include specific information that is important to include in the alt text or reflect why the image is being used:",
40
+ disabled=not show_details
41
  )
42
 
43
+ # Toggle for modifying the prompt for complex images
44
+ complex_image = st.toggle("Is this a complex image? ", value=False)
45
 
46
+ if complex_image:
47
+ # Text input for additional details about the image, shown only if toggle is True
48
+ complex_image_details = st.caption(
49
+ "By clicking this toggle, it will inform MTSS.ai to create a description that exceeds the 125 character limit. "
50
+ "Add the description in a placeholder behind the image and 'Description in the content placeholder' in the alt text box. "
51
+ )
52
 
53
+ # Button to trigger the analysis
54
+ analyze_button = st.button("Analyze the Image", type="secondary")
55
+
56
+ # Optimized prompt for complex images
57
+ complex_image_prompt_text = (
58
+ "As an expert in image accessibility and alternative text, thoroughly describe the image provided. "
59
+ "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. "
60
+ "Skip phrases like 'image of' or 'picture of.' "
61
+ "Your description should form a clear, well-structured, and factual paragraph that avoids bullet points, focusing on creating a seamless narrative."
62
+ )
63
+
64
+ # Check if an image has been uploaded, if the API key is available, and if the button has been pressed
65
+ if uploaded_file is not None and analyze_button:
66
+
67
+ with st.spinner("Analyzing the image ..."):
68
+ # Encode the image
69
+ base64_image = encode_image(uploaded_file)
70
+
71
+ # Determine which prompt to use based on the complexity of the image
72
+ if complex_image:
73
+ prompt_text = complex_image_prompt_text
74
+ else:
75
+ prompt_text = (
76
+ "As an expert in image accessibility and alternative text, succinctly describe the image provided in less than 125 characters. "
77
+ "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. "
78
+ "Skip phrases like 'image of' or 'picture of.' "
79
+ "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."
80
+ )
81
+
82
+ if show_details and additional_details:
83
+ prompt_text += (
84
+ f"\n\nAdditional Context Provided by the User:\n{additional_details}"
85
+ )
86
 
87
+
88
+ # Create the payload for the completion request
89
+ messages = [
90
+ {
91
+ "role": "user",
92
+ "content": [
93
+ {"type": "text", "text": prompt_text},
94
+ {
95
+ "type": "image_url",
96
+ "image_url": f"data:image/jpeg;base64,{base64_image}",
97
+ },
98
+ ],
99
+ }
100
+ ]
101
+
102
+ # Make the request to the OpenAI API
103
+ try:
104
+ # Without Stream
105
+
106
+ # response = openai.chat.completions.create(
107
+ # model="gpt-4-vision-preview", messages=messages, max_tokens=250, stream=False
108
+ # )
109
+
110
+ # Stream the response
111
+ full_response = ""
112
+ message_placeholder = st.empty()
113
+ for completion in openai.chat.completions.create(
114
+ model="gpt-4-vision-preview", messages=messages,
115
+ max_tokens=250, stream=True
116
+ ):
117
+ # # Check if there is content to display
118
+ # if completion.choices[0].delta.content is not None:
119
+ # full_response += completion.choices[0].delta.content
120
+ # message_placeholder.markdown(full_response + "▌")
121
+ # # Final update to placeholder after the stream ends
122
+ # message_placeholder.markdown(full_response) # stream text
123
+
124
+ # Check if there is content to display
125
+ if completion.choices[0].delta.content is not None:
126
+ full_response += completion.choices[0].delta.content
127
+
128
+ # Display the response in a text area
129
+ st.text_area('Response:', value=full_response, height=250, key="response_text_area")
130
+
131
+ st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
132
+ except Exception as e:
133
+ st.error(f"An error occurred: {e}")
134
  else:
135
+ # Warnings for user action required
136
+ if not uploaded_file and analyze_button:
137
+ st.warning("Please upload an image.")
138
+