hmb HF Staff commited on
Commit
ae12abb
·
verified ·
1 Parent(s): 7175168

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. run.ipynb +1 -0
  2. run.py +79 -0
  3. screenshot.png +0 -0
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: matrix_transpose"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import numpy as np\n", "\n", "import gradio as gr\n", "\n", "def transpose(matrix):\n", " return matrix.T\n", "\n", "demo = gr.Interface(\n", " transpose,\n", " gr.Dataframe(type=\"numpy\", datatype=\"number\", row_count=5, col_count=3, show_fullscreen_button=True),\n", " \"numpy\",\n", " examples=[\n", " [np.zeros((3, 3)).tolist()],\n", " [np.ones((2, 2)).tolist()],\n", " [np.random.randint(0, 10, (3, 10)).tolist()],\n", " [np.random.randint(0, 10, (10, 3)).tolist()],\n", " [np.random.randint(0, 10, (10, 10)).tolist()],\n", " ],\n", " cache_examples=False\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import pandas as pd
3
+ import gradio as gr
4
+
5
+ # Load the Pokémon Cards dataset from Hugging Face
6
+ dataset = load_dataset("TheFusion21/PokemonCards")
7
+ df = pd.DataFrame(dataset["train"])
8
+
9
+ display_columns = ["name", "hp", "image_url"] + [col for col in df.columns if col not in ["image_url", "name", "hp"]]
10
+ display_df = df[display_columns]
11
+
12
+ def style_df(df):
13
+ hp_min = df['hp'].min()
14
+ hp_max = df['hp'].max()
15
+
16
+ def color_hp(val):
17
+ norm_val = (val - hp_min) / (hp_max - hp_min)
18
+ r = int(255 * (1 - norm_val))
19
+ g = int(255 * norm_val)
20
+ return f'background-color: rgba({r}, {g}, 0, 0.2)'
21
+
22
+ styled = df.style.applymap(color_hp, subset=['hp'])
23
+ return styled
24
+
25
+ # Function to filter data based on user input
26
+ def filter_cards(set_name=None):
27
+ filtered_df = df.copy()
28
+ if set_name and set_name != "All":
29
+ filtered_df = filtered_df[filtered_df["set_name"] == set_name]
30
+ return style_df(filtered_df[display_columns])
31
+
32
+ def update_display(evt: gr.SelectData):
33
+ selected_data = df.iloc[evt.index[0]]
34
+ stats_df = pd.DataFrame({
35
+ 'Stat': ['Name', 'HP', 'Set Name', 'Caption'],
36
+ 'Value': [selected_data['name'], selected_data['hp'], selected_data['set_name'], selected_data['caption']]
37
+ })
38
+ return selected_data["image_url"], stats_df
39
+
40
+ # Gradio interface
41
+ with gr.Blocks() as demo:
42
+ gr.Markdown("## Pokémon Cards Explorer")
43
+
44
+ with gr.Row():
45
+ set_filter = gr.Dropdown(
46
+ choices=["All"] + df["set_name"].unique().tolist(),
47
+ label="Filter by Set Name"
48
+ )
49
+
50
+ filtered_table = gr.DataFrame(
51
+ style_df(display_df),
52
+ show_fullscreen_button=True,
53
+ show_search="search",
54
+ datatype=["str", "number", "image"] + ["str"] * (len(display_columns) - 3)
55
+ )
56
+
57
+ with gr.Row():
58
+ card_image = gr.Image(label="Card Image", height=400)
59
+ stats_table = gr.DataFrame(
60
+ headers=["Stat", "Value"],
61
+ label="Pokemon Stats",
62
+ interactive=False,
63
+ wrap=True
64
+ )
65
+
66
+ filter_button = gr.Button("Apply Filters")
67
+ filter_button.click(
68
+ filter_cards,
69
+ inputs=[set_filter],
70
+ outputs=filtered_table
71
+ )
72
+
73
+ filtered_table.select(
74
+ update_display,
75
+ None,
76
+ [card_image, stats_table]
77
+ )
78
+
79
+ demo.launch()
screenshot.png ADDED