awacke1 commited on
Commit
14c8dd6
1 Parent(s): b9df75d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -2
app.py CHANGED
@@ -202,6 +202,66 @@ def extract_title(text):
202
  return title[-200:]
203
 
204
  def process_image(image_input, user_prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  if image_input:
206
  st.markdown('Processing image: ' + image_input.name )
207
  if image_input:
@@ -1052,7 +1112,7 @@ def display_glossary(glossary, area):
1052
 
1053
 
1054
  #@st.cache_resource
1055
- def display_videos_and_links(num_columns):
1056
  video_files = [f for f in os.listdir('.') if f.endswith('.mp4')]
1057
  if not video_files:
1058
  st.write("No MP4 videos found in the current directory.")
@@ -1072,7 +1132,6 @@ def display_videos_and_links(num_columns):
1072
  display_glossary_entity(k)
1073
  col_index += 1 # Increment column index to place the next video in the next column
1074
 
1075
- #@st.cache_resource
1076
  def display_images_and_wikipedia_summaries(num_columns=4):
1077
  image_files = [f for f in os.listdir('.') if f.endswith('.png')]
1078
  if not image_files:
@@ -1084,6 +1143,55 @@ def display_images_and_wikipedia_summaries(num_columns=4):
1084
  cols = st.columns(num_columns) # Use specified num_columns for layout
1085
  col_index = 0 # Initialize column index for cycling through columns
1086
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1087
  for image_file in image_files_sorted:
1088
  with cols[col_index % num_columns]: # Cycle through columns based on num_columns
1089
  image = Image.open(image_file)
 
202
  return title[-200:]
203
 
204
  def process_image(image_input, user_prompt):
205
+ SaveNewFile=True
206
+ image_file_name=''
207
+ if isinstance(image_input, str):
208
+ image_file_name = image_input
209
+ with open(image_input, "rb") as image_file:
210
+ image_input = image_file.read()
211
+ SaveNewFile=False # file is there and this is just prompt inference
212
+ else:
213
+ image_file_name = image_input.name
214
+ image_input = image_input.read()
215
+ SaveNewFile=True
216
+
217
+ st.markdown('Processing image: ' + image_file_name)
218
+ base64_image = base64.b64encode(image_input).decode("utf-8")
219
+ response = client.chat.completions.create(
220
+ model=MODEL,
221
+ messages=[
222
+ {"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
223
+ {"role": "user", "content": [
224
+ {"type": "text", "text": user_prompt},
225
+ {"type": "image_url", "image_url": {
226
+ "url": f"data:image/png;base64,{base64_image}"}
227
+ }
228
+ ]}
229
+ ],
230
+ temperature=0.0,
231
+ )
232
+ image_response = response.choices[0].message.content
233
+ st.markdown(image_response)
234
+
235
+ # Save markdown on image AI output from gpt4o
236
+ filename_md = generate_filename(image_file_name + '- ' + image_response, "md")
237
+ # Save markdown on image AI output from gpt4o
238
+ filename_png = filename_md.replace('.md', '.' + image_file_name.split('.')[-1])
239
+
240
+ create_file(filename_md, image_response, '', True)
241
+
242
+ with open(filename_md, "w", encoding="utf-8") as f:
243
+ f.write(image_response)
244
+
245
+ # Extract boldface terms from image_response then autoname save file
246
+ boldface_terms = extract_title(image_response).replace(':','')
247
+ filename_stem, extension = os.path.splitext(image_file_name)
248
+ filename_img = f"{filename_stem} {''.join(boldface_terms)}{extension}"
249
+ if SaveNewFile:
250
+ newfilename = save_image(image_input, filename_img)
251
+ filename_md = newfilename.replace('.png', '.md')
252
+ create_file(filename_md, '', image_response, True)
253
+ else:
254
+
255
+ filename = generate_filename(filename_md, "md")
256
+ create_file(filename, image_file_name, image_response, should_save)
257
+
258
+ #filename_md = image_file_name.replace('.png', '.md')
259
+ #create_file(filename_md, '', image_response, True)
260
+
261
+
262
+ return image_response
263
+
264
+ def process_image_old(image_input, user_prompt):
265
  if image_input:
266
  st.markdown('Processing image: ' + image_input.name )
267
  if image_input:
 
1112
 
1113
 
1114
  #@st.cache_resource
1115
+ def display_videos_and_links_old(num_columns):
1116
  video_files = [f for f in os.listdir('.') if f.endswith('.mp4')]
1117
  if not video_files:
1118
  st.write("No MP4 videos found in the current directory.")
 
1132
  display_glossary_entity(k)
1133
  col_index += 1 # Increment column index to place the next video in the next column
1134
 
 
1135
  def display_images_and_wikipedia_summaries(num_columns=4):
1136
  image_files = [f for f in os.listdir('.') if f.endswith('.png')]
1137
  if not image_files:
 
1143
  cols = st.columns(num_columns) # Use specified num_columns for layout
1144
  col_index = 0 # Initialize column index for cycling through columns
1145
 
1146
+ for image_file in image_files_sorted:
1147
+ with cols[col_index % num_columns]: # Cycle through columns based on num_columns
1148
+ image = Image.open(image_file)
1149
+ st.image(image, caption=image_file, use_column_width=True)
1150
+ k = image_file.split('.')[0] # Assumes keyword is the file name without extension
1151
+ display_glossary_entity(k)
1152
+
1153
+ # Add text input for image file
1154
+ image_text_input = st.text_input(f"Image Prompt for {image_file}", key=f"image_prompt_{image_file}")
1155
+ if image_text_input:
1156
+ process_image(image_file, image_text_input)
1157
+
1158
+ col_index += 1 # Increment to move to the next column in the next iteration
1159
+
1160
+ def display_videos_and_links(num_columns):
1161
+ video_files = [f for f in os.listdir('.') if f.endswith('.mp4')]
1162
+ if not video_files:
1163
+ st.write("No MP4 videos found in the current directory.")
1164
+ return
1165
+
1166
+ video_files_sorted = sorted(video_files, key=lambda x: len(x.split('.')[0]))
1167
+ cols = st.columns(num_columns) # Define num_columns columns outside the loop
1168
+ col_index = 0 # Initialize column index
1169
+
1170
+ for video_file in video_files_sorted:
1171
+ with cols[col_index % num_columns]: # Use modulo to alternate between columns
1172
+ k = video_file.split('.')[0] # Assumes keyword is the file name without extension
1173
+ st.video(video_file, format='video/mp4', start_time=0)
1174
+ display_glossary_entity(k)
1175
+
1176
+ # Add text input for video file
1177
+ video_text_input = st.text_input(f"Video Prompt for {video_file}", key=f"video_prompt_{video_file}")
1178
+ if video_text_input:
1179
+ process_video(video_file, video_text_input)
1180
+
1181
+ col_index += 1 # Increment column index to place the next video in the next column
1182
+
1183
+ #@st.cache_resource
1184
+ def display_images_and_wikipedia_summaries_old(num_columns=4):
1185
+ image_files = [f for f in os.listdir('.') if f.endswith('.png')]
1186
+ if not image_files:
1187
+ st.write("No PNG images found in the current directory.")
1188
+ return
1189
+
1190
+ image_files_sorted = sorted(image_files, key=lambda x: len(x.split('.')[0]))
1191
+
1192
+ cols = st.columns(num_columns) # Use specified num_columns for layout
1193
+ col_index = 0 # Initialize column index for cycling through columns
1194
+
1195
  for image_file in image_files_sorted:
1196
  with cols[col_index % num_columns]: # Cycle through columns based on num_columns
1197
  image = Image.open(image_file)