File size: 3,330 Bytes
e9f8b4f
680e8a6
c2058a3
56c75b8
f5e157a
c2058a3
f5e157a
 
e9f8b4f
f5e157a
 
c2058a3
f5e157a
 
c2058a3
e9f8b4f
f5e157a
 
e9f8b4f
15219ef
c2058a3
f5e157a
 
 
 
 
 
c2058a3
f5e157a
 
15219ef
 
 
 
 
 
 
 
 
 
 
 
 
f5e157a
 
 
15219ef
f5e157a
c2058a3
f5e157a
 
e9f8b4f
f5e157a
 
 
 
2aa8325
178d7f7
 
 
 
 
 
 
f5e157a
 
 
 
 
15219ef
f5e157a
 
 
c2058a3
f5e157a
178d7f7
f5e157a
c2058a3
f5e157a
15219ef
 
 
 
8836ad1
f5e157a
 
178d7f7
f5e157a
c2058a3
f5e157a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Importing required libraries
import gradio as gr
from PIL import Image
import random
import os

# Define the folder containing images
CELEB_FOLDER = "./Celebs"

# Get the list of celebrity images from the folder
celebs = {os.path.splitext(file)[0]: os.path.join(CELEB_FOLDER, file) for file in os.listdir(CELEB_FOLDER)}

# Define pixel sizes for different levels of clarity
PIXEL_SIZES = [128, 96, 64, 48, 32, 24, 16, 12, 8, 4, 2]

# Initialize global variables
current_image = None
current_name = None
current_pixel_size = 256
used_images = set()  # Track used images

# Function to pixelate an image
def pixelate(image, pixel_size):
    # Reduce the image size
    small = image.resize((image.size[0] // pixel_size, image.size[1] // pixel_size), Image.Resampling.NEAREST)
    # Scale back to the original size
    return small.resize(image.size, Image.Resampling.NEAREST)

# Start a new game
def start_game():
    global current_image, current_name, current_pixel_size, used_images

    # Check if all images have been used; if so, reset
    if len(used_images) == len(celebs):
        used_images.clear()

    # Select a random image that has not been used
    available_images = list(set(celebs.keys()) - used_images)
    current_name = random.choice(available_images)
    used_images.add(current_name)

    # Load the selected image
    image_path = celebs[current_name]
    current_image = Image.open(image_path)
    current_pixel_size = PIXEL_SIZES[0]
    pixelated_image = pixelate(current_image, current_pixel_size)

    return pixelated_image, "Image Loaded. Guess the Celebrity!"

# Reveal more of the image
def clear_more():
    global current_pixel_size
    if current_pixel_size > PIXEL_SIZES[-1]:
        current_pixel_size = PIXEL_SIZES[PIXEL_SIZES.index(current_pixel_size) + 1]
    pixelated_image = pixelate(current_image, current_pixel_size)
    return pixelated_image, "Getting clearer! Keep guessing!"

# Check the user's guess
def check_guess(user_guess):
    if user_guess.strip().lower() == current_name.strip().lower():
        return "Correct! 🎉 You guessed it right!"
    else:
        return "Incorrect. Keep trying!"

# Reveal the answer
def reveal_answer():
    return f"The correct answer is: {current_name}!"

# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
    gr.Markdown("# 🎭 Guess the Celebrity Game!")
    gr.Markdown("Can you identify the celebrity as the image becomes clearer?")

    with gr.Row():
        pixelated_image = gr.Image(type="pil", label="Pixelated Image")
        answer_input = gr.Textbox(label="Your Guess", placeholder="Type your guess here...")
        result = gr.Label(label="Game Status")

    with gr.Row():
        start_button = gr.Button("Start Game", variant='huggingface')
        clear_button = gr.Button("Clear More", variant='primary')
        guess_button = gr.Button("Submit Guess", variant='secondary')
        reveal_button = gr.Button("Reveal Answer", variant='success')

    start_button.click(start_game, inputs=[], outputs=[pixelated_image, result])
    clear_button.click(clear_more, inputs=[], outputs=[pixelated_image, result])
    guess_button.click(check_guess, inputs=answer_input, outputs=result)
    reveal_button.click(reveal_answer, inputs=[], outputs=result)

demo.launch(debug=True, show_error=True)