awacke1 commited on
Commit
0d8fdd8
1 Parent(s): 3ee1486

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -34
app.py CHANGED
@@ -316,48 +316,36 @@ FileSidebar()
316
 
317
 
318
 
319
-
320
- # ---- Art Card Sidebar with Random Selection of image:
321
- def get_image_as_base64(url):
322
- response = requests.get(url)
323
- if response.status_code == 200:
324
- # Convert the image to base64
325
- return base64.b64encode(response.content).decode("utf-8")
326
- else:
327
- return None
328
 
329
- def create_download_link(filename, base64_str):
330
- href = f'<a href="data:file/png;base64,{base64_str}" download="{filename}">Download Image</a>'
331
- return href
 
 
 
 
 
332
 
333
- # List of image URLs
334
- image_urls = [
335
- "https://cdn-uploads.huggingface.co/production/uploads/620630b603825909dcbeba35/W1omJItftG3OkW9sj-Ckb.png",
336
- "https://cdn-uploads.huggingface.co/production/uploads/620630b603825909dcbeba35/Djx-k4WOxzlXEQPzllP3r.png"
337
- ]
338
 
339
- # Select a random URL from the list
340
- selected_image_url = random.choice(image_urls)
341
 
342
- # Get the base64 encoded string of the selected image
343
- st.write(selected_image_url)
344
  try:
345
- selected_image_base64 = get_image_as_base64(selected_image_url)
346
-
347
- if selected_image_base64 is not None:
348
  with st.sidebar:
349
  st.markdown("""### Graphic Novel AI""")
350
- # Display the image
351
- st.markdown(f"![image](data:image/png;base64,{selected_image_base64})")
352
-
353
- # Create and display the download link
354
- download_link = create_download_link("downloaded_image.png", selected_image_base64)
355
- st.markdown(download_link, unsafe_allow_html=True)
356
  else:
357
- st.sidebar.write("Failed to load the image.")
358
- except:
359
- st.write('Sidebar Fail - Check your Images')
360
- # ---- Art Card Sidebar with random selection of image.
361
 
362
 
363
 
 
316
 
317
 
318
 
319
+ import streamlit as st
320
+ import random
321
+ import os
 
 
 
 
 
 
322
 
323
+ # Function to get a list of image paths from the /images directory
324
+ def get_image_paths(directory="/images"):
325
+ image_paths = [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith(('png', 'jpg', 'jpeg'))]
326
+ return image_paths
327
+
328
+ # Function to select a random image path
329
+ def select_random_image_path(image_paths):
330
+ return random.choice(image_paths)
331
 
332
+ # Get list of image paths from the /images directory
333
+ image_paths = get_image_paths()
 
 
 
334
 
335
+ # Select a random image path from the list
336
+ selected_image_path = select_random_image_path(image_paths)
337
 
338
+ # Display the selected image in the sidebar
 
339
  try:
340
+ if selected_image_path:
 
 
341
  with st.sidebar:
342
  st.markdown("""### Graphic Novel AI""")
343
+ st.image(selected_image_path, caption=selected_image_path)
 
 
 
 
 
344
  else:
345
+ st.sidebar.write("No images found in the directory.")
346
+ except Exception as e:
347
+ st.write(f'Sidebar Fail - Check your Images. Error: {e}')
348
+
349
 
350
 
351