chrisaldikaraharja commited on
Commit
4338cd1
·
verified ·
1 Parent(s): c59f5ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -6,28 +6,30 @@ from PIL import Image
6
 
7
  # Step 1: Download the latest version of the dataset and get the path
8
  path = kagglehub.dataset_download("imreallyjohn/cartoonset10k")
9
- images_folder = "/home/user/.cache/kagglehub/datasets/imreallyjohn/cartoonset10k/versions/1" # Exact path to image directory
10
-
11
  # Display the dataset path
12
  st.write("Path to dataset files:", images_folder)
13
 
14
- # Button to generate a random avatar
15
- if st.button("Generate Random Avatar"):
16
- # Step 2: List all image files in the dataset directory
17
- try:
18
- image_files = [f for f in os.listdir(images_folder) if f.endswith('.png')] # List .png files
 
 
 
 
 
 
 
19
 
20
- # Check if there are images in the directory
21
- if image_files:
22
- # Select a random available image
23
- random_image_filename = random.choice(image_files)
24
- image_path = os.path.join(images_folder, random_image_filename)
25
-
26
- # Load and display the selected image
27
- selected_image = Image.open(image_path)
28
- st.image(selected_image, caption=f"Displayed image: {random_image_filename}")
29
- st.write(f"Displayed image: {random_image_filename}")
30
- else:
31
- st.error("No available images to display.")
32
- except Exception as e:
33
- st.error(f"Error accessing images: {e}")
 
6
 
7
  # Step 1: Download the latest version of the dataset and get the path
8
  path = kagglehub.dataset_download("imreallyjohn/cartoonset10k")
9
+ images_folder = "/home/user/.cache/kagglehub/datasets/imreallyjohn/cartoonset10k/versions/1"
 
10
  # Display the dataset path
11
  st.write("Path to dataset files:", images_folder)
12
 
13
+ # Step 2: List all image files in the dataset directory
14
+ try:
15
+ image_files = [f for f in os.listdir(images_folder) if f.endswith('.png')] # List .png files
16
+ num_images = len(image_files)
17
+
18
+ # Display the number of images loaded
19
+ st.write(f"Total images loaded: {num_images}")
20
+
21
+ if num_images == 0:
22
+ st.error("No available images to display. Please check the folder path and contents.")
23
+ except Exception as e:
24
+ st.error(f"Error accessing images: {e}")
25
 
26
+ # Button to generate a random avatar if images are available
27
+ if num_images > 0 and st.button("Generate Random Avatar"):
28
+ # Step 3: Select a random available image
29
+ random_image_filename = random.choice(image_files)
30
+ image_path = os.path.join(images_folder, random_image_filename)
31
+
32
+ # Load and display the selected image
33
+ selected_image = Image.open(image_path)
34
+ st.image(selected_image, caption=f"Displayed image: {random_image_filename}")
35
+ st.write(f"Displayed image: {random_image_filename}")