Spaces:
Running
Running
Update data_explorer.py
Browse files- data_explorer.py +11 -51
data_explorer.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
from datasets import load_dataset
|
| 4 |
-
from huggingface_hub import HfApi
|
| 5 |
import os
|
| 6 |
|
| 7 |
# --- Backend Functions ---
|
|
@@ -34,59 +33,23 @@ def preview_dataset(dataset_id, config_name, split_name, num_rows):
|
|
| 34 |
except Exception as e:
|
| 35 |
return pd.DataFrame(), f"❌ Error: {str(e)}"
|
| 36 |
|
| 37 |
-
def upload_to_hub(files,
|
| 38 |
"""
|
| 39 |
-
|
|
|
|
| 40 |
"""
|
| 41 |
if not files:
|
| 42 |
-
return "⚠️
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return "⚠️ Please enter a Repository Name (e.g., username/dataset)."
|
| 47 |
-
|
| 48 |
-
try:
|
| 49 |
-
api = HfApi(token=hf_token)
|
| 50 |
-
|
| 51 |
-
# 1. Create the Repo (if it doesn't exist)
|
| 52 |
-
repo_url = api.create_repo(
|
| 53 |
-
repo_id=repo_name,
|
| 54 |
-
repo_type="dataset",
|
| 55 |
-
private=is_private,
|
| 56 |
-
exist_ok=True
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
# 2. Upload Files
|
| 60 |
-
uploaded_count = 0
|
| 61 |
-
# Handle file input for different Gradio versions
|
| 62 |
-
file_list = files if isinstance(files, list) else [files]
|
| 63 |
-
|
| 64 |
-
for file_obj in file_list:
|
| 65 |
-
# file_obj is a temp path string or object depending on version
|
| 66 |
-
file_path = file_obj.name if hasattr(file_obj, 'name') else file_obj
|
| 67 |
-
file_name = os.path.basename(file_path)
|
| 68 |
-
|
| 69 |
-
api.upload_file(
|
| 70 |
-
path_or_fileobj=file_path,
|
| 71 |
-
path_in_repo=file_name,
|
| 72 |
-
repo_id=repo_name,
|
| 73 |
-
repo_type="dataset"
|
| 74 |
-
)
|
| 75 |
-
uploaded_count += 1
|
| 76 |
-
|
| 77 |
-
return f"🚀 Success! {uploaded_count} files pushed to: {repo_url}"
|
| 78 |
-
|
| 79 |
-
except Exception as e:
|
| 80 |
-
return f"❌ Upload failed: {str(e)}"
|
| 81 |
|
| 82 |
# --- Frontend Interface ---
|
| 83 |
|
| 84 |
def render_explorer():
|
| 85 |
"""
|
| 86 |
Returns the layout (Column) and the back button.
|
| 87 |
-
Does NOT use gr.Blocks() internally.
|
| 88 |
"""
|
| 89 |
-
# 1. Use a Column, hidden by default
|
| 90 |
with gr.Column(visible=False) as explorer_view:
|
| 91 |
|
| 92 |
gr.Markdown("# 🤗 Hugging Face Dataset Manager")
|
|
@@ -117,8 +80,9 @@ def render_explorer():
|
|
| 117 |
with gr.TabItem("☁️ Upload New Dataset"):
|
| 118 |
gr.Markdown("Upload CSV, JSON, or Parquet files to create a new dataset.")
|
| 119 |
|
|
|
|
|
|
|
| 120 |
with gr.Row():
|
| 121 |
-
in_token = gr.Textbox(label="HF Write Token", type="password", placeholder="hf_...")
|
| 122 |
in_repo = gr.Textbox(label="New Repo Name", placeholder="username/my-new-dataset")
|
| 123 |
in_private = gr.Checkbox(label="Private Dataset", value=True)
|
| 124 |
|
|
@@ -126,25 +90,21 @@ def render_explorer():
|
|
| 126 |
btn_upload = gr.Button("Push to Hub", variant="primary")
|
| 127 |
out_upload_status = gr.Markdown()
|
| 128 |
|
|
|
|
| 129 |
btn_upload.click(
|
| 130 |
fn=upload_to_hub,
|
| 131 |
-
inputs=[in_files,
|
| 132 |
outputs=[out_upload_status]
|
| 133 |
)
|
| 134 |
|
| 135 |
gr.Markdown("---")
|
| 136 |
|
| 137 |
-
# 2. Add the Back Button
|
| 138 |
btn_back = gr.Button("⬅️ Back to Home", variant="secondary")
|
| 139 |
|
| 140 |
-
# 3. Return the container and button to app.py
|
| 141 |
return explorer_view, btn_back
|
| 142 |
|
| 143 |
-
# --- Optional: Standalone Testing ---
|
| 144 |
-
# This block allows you to run `python data_explorer.py` just to test this file alone.
|
| 145 |
if __name__ == "__main__":
|
| 146 |
with gr.Blocks() as demo:
|
| 147 |
-
# We manually render it and default it to visible for testing
|
| 148 |
view, _ = render_explorer()
|
| 149 |
view.visible = True
|
| 150 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
from datasets import load_dataset
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# --- Backend Functions ---
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return pd.DataFrame(), f"❌ Error: {str(e)}"
|
| 35 |
|
| 36 |
+
def upload_to_hub(files, repo_name, is_private):
|
| 37 |
"""
|
| 38 |
+
Placeholder function for development.
|
| 39 |
+
The Token field was removed, so this functionality is currently disabled.
|
| 40 |
"""
|
| 41 |
if not files:
|
| 42 |
+
return "⚠️ No files selected."
|
| 43 |
+
|
| 44 |
+
# Just return a message for now since we removed the token logic
|
| 45 |
+
return f"🛠️ Dev Mode: Upload to '{repo_name}' triggered (functionality disabled)."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# --- Frontend Interface ---
|
| 48 |
|
| 49 |
def render_explorer():
|
| 50 |
"""
|
| 51 |
Returns the layout (Column) and the back button.
|
|
|
|
| 52 |
"""
|
|
|
|
| 53 |
with gr.Column(visible=False) as explorer_view:
|
| 54 |
|
| 55 |
gr.Markdown("# 🤗 Hugging Face Dataset Manager")
|
|
|
|
| 80 |
with gr.TabItem("☁️ Upload New Dataset"):
|
| 81 |
gr.Markdown("Upload CSV, JSON, or Parquet files to create a new dataset.")
|
| 82 |
|
| 83 |
+
# --- TOKEN FIELD REMOVED ---
|
| 84 |
+
|
| 85 |
with gr.Row():
|
|
|
|
| 86 |
in_repo = gr.Textbox(label="New Repo Name", placeholder="username/my-new-dataset")
|
| 87 |
in_private = gr.Checkbox(label="Private Dataset", value=True)
|
| 88 |
|
|
|
|
| 90 |
btn_upload = gr.Button("Push to Hub", variant="primary")
|
| 91 |
out_upload_status = gr.Markdown()
|
| 92 |
|
| 93 |
+
# Updated inputs to exclude the token
|
| 94 |
btn_upload.click(
|
| 95 |
fn=upload_to_hub,
|
| 96 |
+
inputs=[in_files, in_repo, in_private],
|
| 97 |
outputs=[out_upload_status]
|
| 98 |
)
|
| 99 |
|
| 100 |
gr.Markdown("---")
|
| 101 |
|
|
|
|
| 102 |
btn_back = gr.Button("⬅️ Back to Home", variant="secondary")
|
| 103 |
|
|
|
|
| 104 |
return explorer_view, btn_back
|
| 105 |
|
|
|
|
|
|
|
| 106 |
if __name__ == "__main__":
|
| 107 |
with gr.Blocks() as demo:
|
|
|
|
| 108 |
view, _ = render_explorer()
|
| 109 |
view.visible = True
|
| 110 |
demo.launch()
|