ProfessorLeVesseur commited on
Commit
79caa78
·
verified ·
1 Parent(s): 0747aac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -90
app.py CHANGED
@@ -1,6 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
@@ -9,128 +152,58 @@ 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('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 = 150 # 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 checking this box, it will inform MTSS.ai to create a robust description that exceeds the 125 character limit. "
49
- "You will add the description in a placeholder behind the image and add the text, '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=400, 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.")
 
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 = 150 # 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 checking this box, it will inform MTSS.ai to create a robust description that exceeds the 125 character limit. "
49
+ # "You will add the description in a placeholder behind the image and add the text, '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=400, 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
+
141
+
142
+
143
+
144
  import streamlit as st
145
  import base64
146
+ import openai
147
 
148
  # Function to encode the image to base64
149
  def encode_image(image_file):
 
152
  # Streamlit page setup
153
  st.set_page_config(page_title="MTSS Image Accessibility Alt Text Generator", layout="centered", initial_sidebar_state="collapsed")
154
 
155
+ # Add the image with a specified width
156
  image_width = 300 # Set the desired width in pixels
157
  st.image('MTSS.ai_Logo.png', width=image_width)
158
 
 
159
  st.header('VisionText™ | Accessibility')
160
  st.subheader(':green[_Image Alt Text Generator_]')
161
 
162
  # Retrieve the OpenAI API Key from secrets
163
  openai.api_key = st.secrets["openai_api_key"]
164
 
 
 
 
 
 
 
 
 
 
165
  # Toggle for showing additional details input
166
  show_details = st.toggle("Add details about the image. ", value=False)
167
 
168
+ # Text input for additional details about the image, shown only if toggle is True
169
+ additional_details = ""
170
  if show_details:
 
171
  additional_details = st.text_area(
172
+ "The details could include specific information that is important to include in the alt text or reflect why the image is being used:"
 
173
  )
174
 
175
  # Toggle for modifying the prompt for complex images
176
  complex_image = st.toggle("Is this a complex image? ", value=False)
177
 
 
 
 
 
 
 
 
178
  # Button to trigger the analysis
179
+ analyze_button = st.button("Analyze the Image")
180
 
181
+ # Prepare to display the response in a text area later
182
+ full_response = ""
 
 
 
 
 
183
 
184
+ # Now that we've defined our toggles and button, let's handle the file upload and analysis
185
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
186
 
187
+ if uploaded_file and analyze_button:
188
  with st.spinner("Analyzing the image ..."):
189
+ # Display the uploaded image with specified width
190
+ with st.expander("Image", expanded=True):
191
+ st.image(uploaded_file, caption=uploaded_file.name, width=150, use_column_width=False)
192
+
193
  # Encode the image
194
  base64_image = encode_image(uploaded_file)
195
 
196
+ # Your existing logic to set `prompt_text` based on `complex_image` and to append `additional_details` if available
197
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  # Make the request to the OpenAI API
199
  try:
200
+ # Your existing OpenAI API request logic
201
+ # Display the response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  st.text_area('Response:', value=full_response, height=400, key="response_text_area")
 
203
  st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
204
  except Exception as e:
205
  st.error(f"An error occurred: {e}")
206
  else:
207
  # Warnings for user action required
208
  if not uploaded_file and analyze_button:
209
+ st.warning("Please upload an image.")