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