Spaces:
Running
Running
backup
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
import gradio as gr
|
3 |
+
import random
|
4 |
+
import requests
|
5 |
+
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Load the dataset
|
9 |
+
steam_dataset = load_dataset("taesiri/SteamCommunityImagesDailyDumpy")
|
10 |
+
|
11 |
+
# Convert to pandas once and keep it in memory
|
12 |
+
df = steam_dataset["train"].to_pandas()
|
13 |
+
game_counts = df["game_name"].value_counts().to_dict()
|
14 |
+
game_list = list(game_counts.keys())
|
15 |
+
|
16 |
+
# Cache for storing processed game data
|
17 |
+
game_cache = {}
|
18 |
+
|
19 |
+
|
20 |
+
def get_random_image(game_name):
|
21 |
+
# Lazy loading: only process game data when first requested
|
22 |
+
if game_name not in game_cache:
|
23 |
+
game_cache[game_name] = df[df["game_name"] == game_name]["image_url"].tolist()
|
24 |
+
|
25 |
+
# Get random image URL from cached list
|
26 |
+
random_url = random.choice(game_cache[game_name])
|
27 |
+
|
28 |
+
# Download and return image
|
29 |
+
response = requests.get(random_url)
|
30 |
+
img = Image.open(BytesIO(response.content))
|
31 |
+
return img, f"Total images available for {game_name}: {game_counts[game_name]}"
|
32 |
+
|
33 |
+
|
34 |
+
# Create Gradio interface
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# Steam Game Image Preview")
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
game_dropdown = gr.Dropdown(
|
40 |
+
choices=game_list,
|
41 |
+
label="Select a Game",
|
42 |
+
info="Choose a game to see random screenshots",
|
43 |
+
)
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
show_btn = gr.Button("Show Random Image")
|
47 |
+
|
48 |
+
with gr.Column():
|
49 |
+
image_output = gr.Image(type="pil", label="Random Game Image")
|
50 |
+
stats_output = gr.Textbox(label="Statistics")
|
51 |
+
|
52 |
+
show_btn.click(
|
53 |
+
fn=get_random_image,
|
54 |
+
inputs=[game_dropdown],
|
55 |
+
outputs=[image_output, stats_output],
|
56 |
+
)
|
57 |
+
|
58 |
+
demo.launch()
|