davanstrien HF staff commited on
Commit
1d123d6
1 Parent(s): bc76ee2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import get_collection
4
+ import re
5
+
6
+ def extract_collection_id(input_text):
7
+ # Check if input is a full URL
8
+ url_match = re.match(r'https://huggingface\.co/collections/(.+)$', input_text)
9
+ if url_match:
10
+ return url_match.group(1)
11
+
12
+ # Check if input is already in the correct format
13
+ if re.match(r'^[\w-]+/[\w-]+$', input_text):
14
+ return input_text
15
+
16
+ return None
17
+
18
+ def load_collection():
19
+ collection_input = os.getenv('COLLECTION_SLUG_OR_URL')
20
+ if not collection_input:
21
+ raise ValueError("COLLECTION_SLUG_OR_URL environment variable is not set.")
22
+
23
+ collection_id = extract_collection_id(collection_input)
24
+ if not collection_id:
25
+ raise ValueError("Invalid collection ID or URL in COLLECTION_SLUG_OR_URL environment variable.")
26
+
27
+ collection = get_collection(collection_id)
28
+ dataset_ids = [item.item_id for item in collection.items if item.item_type == 'dataset']
29
+ if not dataset_ids:
30
+ raise ValueError("No datasets found in this collection.")
31
+
32
+ return dataset_ids, collection_id
33
+
34
+ def display_dataset(dataset_ids, index):
35
+ dataset_id = dataset_ids[index]
36
+ return gr.HTML(f"""<iframe
37
+ src="https://huggingface.co/datasets/{dataset_id}/embed/viewer"
38
+ frameborder="0"
39
+ width="100%"
40
+ height="560px"
41
+ ></iframe>""")
42
+
43
+ def navigate_dataset(dataset_ids, index, direction):
44
+ new_index = (index + direction) % len(dataset_ids)
45
+ return new_index, f"Dataset {new_index + 1} of {len(dataset_ids)}: {dataset_ids[new_index]}"
46
+
47
+ try:
48
+ dataset_ids, collection_id = load_collection()
49
+
50
+ with gr.Blocks() as demo:
51
+ gr.Markdown(f"<h1>Dataset Viewer for Collection: {collection_id}</h1>")
52
+
53
+ index_state = gr.State(value=0)
54
+
55
+ with gr.Row():
56
+ left_btn = gr.Button("Previous")
57
+ right_btn = gr.Button("Next")
58
+
59
+ dataset_info = gr.Markdown(f"Dataset 1 of {len(dataset_ids)}: {dataset_ids[0]}")
60
+ iframe_output = gr.HTML()
61
+
62
+ left_btn.click(
63
+ navigate_dataset,
64
+ inputs=[gr.State(dataset_ids), index_state, gr.Number(-1, visible=False)],
65
+ outputs=[index_state, dataset_info]
66
+ )
67
+ right_btn.click(
68
+ navigate_dataset,
69
+ inputs=[gr.State(dataset_ids), index_state, gr.Number(1, visible=False)],
70
+ outputs=[index_state, dataset_info]
71
+ )
72
+
73
+ index_state.change(
74
+ display_dataset,
75
+ inputs=[gr.State(dataset_ids), index_state],
76
+ outputs=[iframe_output]
77
+ )
78
+
79
+ # Initialize the display with the first dataset
80
+ demo.load(
81
+ fn=lambda: display_dataset(dataset_ids, 0),
82
+ inputs=None,
83
+ outputs=[iframe_output],
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch()
88
+
89
+ except Exception as e:
90
+ print(f"Error: {str(e)}")
91
+ print("Please set the COLLECTION_SLUG_OR_URL environment variable with a valid collection ID or URL.")