File size: 1,643 Bytes
77eafe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import load_dataset
import gradio as gr
import random
import requests
from PIL import Image
from io import BytesIO

# Load the dataset
steam_dataset = load_dataset("taesiri/SteamCommunityImagesDailyDumpy")

# Convert to pandas once and keep it in memory
df = steam_dataset["train"].to_pandas()
game_counts = df["game_name"].value_counts().to_dict()
game_list = list(game_counts.keys())

# Cache for storing processed game data
game_cache = {}


def get_random_image(game_name):
    # Lazy loading: only process game data when first requested
    if game_name not in game_cache:
        game_cache[game_name] = df[df["game_name"] == game_name]["image_url"].tolist()

    # Get random image URL from cached list
    random_url = random.choice(game_cache[game_name])

    # Download and return image
    response = requests.get(random_url)
    img = Image.open(BytesIO(response.content))
    return img, f"Total images available for {game_name}: {game_counts[game_name]}"


# Create Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Steam Game Image Preview")

    with gr.Row():
        game_dropdown = gr.Dropdown(
            choices=game_list,
            label="Select a Game",
            info="Choose a game to see random screenshots",
        )

    with gr.Row():
        show_btn = gr.Button("Show Random Image")

    with gr.Column():
        image_output = gr.Image(type="pil", label="Random Game Image")
        stats_output = gr.Textbox(label="Statistics")

    show_btn.click(
        fn=get_random_image,
        inputs=[game_dropdown],
        outputs=[image_output, stats_output],
    )

demo.launch()