ProfessorLeVesseur commited on
Commit
235a4b4
·
verified ·
1 Parent(s): a688783

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -110,8 +110,12 @@ analyze_button = st.button("Analyze the Image", type="secondary")
110
 
111
 
112
 
 
 
 
113
  # Check if an image has been uploaded, if the API key is available, and if the button has been pressed
114
  if uploaded_file is not None and analyze_button:
 
115
  with st.spinner("Analyzing the image ..."):
116
  # Encode the image
117
  base64_image = encode_image(uploaded_file)
@@ -122,11 +126,13 @@ if uploaded_file is not None and analyze_button:
122
  "Your task is to examine the following image in detail. "
123
  "Provide a comprehensive, factual, and accurate explanation of what the image depicts. "
124
  "Highlight key elements and their significance, and present your analysis in clear, well-structured paragraph format. "
125
- "Create a detailed image caption explaining in 150 words or less."
126
  )
127
 
128
  if show_details and additional_details:
129
- prompt_text += f"\n\nAdditional Context Provided by the User:\n{additional_details}"
 
 
130
 
131
  # Create the payload for the completion request
132
  messages = [
@@ -144,15 +150,23 @@ if uploaded_file is not None and analyze_button:
144
 
145
  # Make the request to the OpenAI API
146
  try:
 
 
 
 
 
 
 
147
  full_response = ""
148
- for completion in openai.ChatCompletion.create(
 
149
  model="gpt-4-vision-preview", messages=messages,
150
  max_tokens=150, stream=True
151
  ):
152
  # Check if there is content to display
153
  if completion.choices[0].delta.content is not None:
154
  full_response += completion.choices[0].delta.content
155
-
156
  # Display the response in a text area
157
  st.text_area('Response:', value=full_response, height=400, key="response_text_area")
158
 
@@ -163,4 +177,3 @@ else:
163
  # Warnings for user action required
164
  if not uploaded_file and analyze_button:
165
  st.warning("Please upload an image.")
166
-
 
110
 
111
 
112
 
113
+
114
+
115
+
116
  # Check if an image has been uploaded, if the API key is available, and if the button has been pressed
117
  if uploaded_file is not None and analyze_button:
118
+
119
  with st.spinner("Analyzing the image ..."):
120
  # Encode the image
121
  base64_image = encode_image(uploaded_file)
 
126
  "Your task is to examine the following image in detail. "
127
  "Provide a comprehensive, factual, and accurate explanation of what the image depicts. "
128
  "Highlight key elements and their significance, and present your analysis in clear, well-structured paragraph format. "
129
+ "Create a detailed image caption in explaining in 150 words or less."
130
  )
131
 
132
  if show_details and additional_details:
133
+ prompt_text += (
134
+ f"\n\nAdditional Context Provided by the User:\n{additional_details}"
135
+ )
136
 
137
  # Create the payload for the completion request
138
  messages = [
 
150
 
151
  # Make the request to the OpenAI API
152
  try:
153
+ # Without Stream
154
+
155
+ # response = openai.chat.completions.create(
156
+ # model="gpt-4-vision-preview", messages=messages, max_tokens=500, stream=False
157
+ # )
158
+
159
+ # Stream the response
160
  full_response = ""
161
+ message_placeholder = st.empty()
162
+ for completion in openai.chat.completions.create(
163
  model="gpt-4-vision-preview", messages=messages,
164
  max_tokens=150, stream=True
165
  ):
166
  # Check if there is content to display
167
  if completion.choices[0].delta.content is not None:
168
  full_response += completion.choices[0].delta.content
169
+
170
  # Display the response in a text area
171
  st.text_area('Response:', value=full_response, height=400, key="response_text_area")
172
 
 
177
  # Warnings for user action required
178
  if not uploaded_file and analyze_button:
179
  st.warning("Please upload an image.")