Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
from PIL import Image
|
5 |
+
import kagglehub
|
6 |
+
|
7 |
+
# Step 1: Download the latest version of the dataset
|
8 |
+
path = kagglehub.dataset_download("imreallyjohn/cartoonset10k")
|
9 |
+
|
10 |
+
# Display the path to the dataset files
|
11 |
+
st.write("Path to dataset files:", path)
|
12 |
+
|
13 |
+
# Step 2: Set the images folder
|
14 |
+
images_folder = os.path.join(path, "cartoonset10k/renamed_images/renamed_images") # Adjust as necessary
|
15 |
+
|
16 |
+
# Step 3: List all image files in the dataset directory
|
17 |
+
image_files = [f for f in os.listdir(images_folder) if f.endswith('.png')] # Filter for .png files
|
18 |
+
|
19 |
+
# Initialize a variable to hold the selected image path
|
20 |
+
selected_image_path = None
|
21 |
+
|
22 |
+
# Step 4: Create a button to generate a random avatar
|
23 |
+
if st.button("Generate Avatar"):
|
24 |
+
if image_files:
|
25 |
+
# Select a random available image
|
26 |
+
random_image_filename = random.choice(image_files) # Select a random image
|
27 |
+
selected_image_path = os.path.join(images_folder, random_image_filename)
|
28 |
+
|
29 |
+
# Load and display the selected image
|
30 |
+
selected_image = Image.open(selected_image_path)
|
31 |
+
st.image(selected_image, caption=random_image_filename) # Display image with caption
|
32 |
+
st.success(f"Displayed image: {random_image_filename}") # Show success message
|
33 |
+
else:
|
34 |
+
st.warning("No available images to display.") # Show warning if no images are found
|
35 |
+
|
36 |
+
# Optional: Add a download link for the displayed image
|
37 |
+
if selected_image_path:
|
38 |
+
st.download_button(
|
39 |
+
label="Download Avatar",
|
40 |
+
data=open(selected_image_path, "rb").read(),
|
41 |
+
file_name=os.path.basename(selected_image_path),
|
42 |
+
mime="image/png"
|
43 |
+
)
|