Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,18 +31,24 @@ def zip_folder(source_folder):
|
|
| 31 |
shutil.make_archive(tmp_zip_path.replace(".zip", ""), 'zip', source_folder)
|
| 32 |
return tmp_zip_path
|
| 33 |
|
| 34 |
-
def download_hf_repo_temp(repo_id):
|
| 35 |
repo_id = repo_id.strip()
|
| 36 |
if not repo_id or "/" not in repo_id:
|
| 37 |
-
return "β Invalid repository ID", None
|
| 38 |
|
| 39 |
-
|
| 40 |
-
if
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Create temporary folder
|
| 44 |
temp_dir = tempfile.mkdtemp()
|
| 45 |
-
log
|
| 46 |
|
| 47 |
try:
|
| 48 |
# Download repo
|
|
@@ -55,7 +61,9 @@ def download_hf_repo_temp(repo_id):
|
|
| 55 |
|
| 56 |
# Zip temp folder
|
| 57 |
zip_path = zip_folder(temp_dir)
|
| 58 |
-
log += "β
Download complete and zipped
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Cleanup temp folder after creating zip
|
| 61 |
shutil.rmtree(temp_dir)
|
|
@@ -64,29 +72,180 @@ def download_hf_repo_temp(repo_id):
|
|
| 64 |
except Exception as e:
|
| 65 |
# Cleanup on error
|
| 66 |
shutil.rmtree(temp_dir)
|
| 67 |
-
return f"β Error: {str(e)}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Gradio UI
|
| 70 |
-
with gr.Blocks(theme=
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
log_output = gr.Textbox(label="Status Log", lines=12, interactive=False)
|
| 84 |
-
file_output = gr.File(label="π¦ Download ZIP")
|
| 85 |
-
|
| 86 |
download_btn.click(
|
| 87 |
fn=download_hf_repo_temp,
|
| 88 |
-
inputs=[repo_input],
|
| 89 |
outputs=[log_output, file_output]
|
| 90 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
shutil.make_archive(tmp_zip_path.replace(".zip", ""), 'zip', source_folder)
|
| 32 |
return tmp_zip_path
|
| 33 |
|
| 34 |
+
def download_hf_repo_temp(repo_id, repo_type_override=None):
|
| 35 |
repo_id = repo_id.strip()
|
| 36 |
if not repo_id or "/" not in repo_id:
|
| 37 |
+
return "β Invalid repository ID. Format should be: username/repo_name", None
|
| 38 |
|
| 39 |
+
# Use override if provided, otherwise auto-detect
|
| 40 |
+
if repo_type_override and repo_type_override != "auto":
|
| 41 |
+
repo_type = repo_type_override
|
| 42 |
+
log = f"π Using specified repo type: `{repo_type}`\n"
|
| 43 |
+
else:
|
| 44 |
+
repo_type = detect_repo_type(repo_id)
|
| 45 |
+
if not repo_type:
|
| 46 |
+
return f"β Repository '{repo_id}' not found or inaccessible", None
|
| 47 |
+
log = f"π Auto-detected repo type: `{repo_type}`\n"
|
| 48 |
|
| 49 |
# Create temporary folder
|
| 50 |
temp_dir = tempfile.mkdtemp()
|
| 51 |
+
log += f"β¬οΈ Downloading '{repo_id}' to temporary folder...\n"
|
| 52 |
|
| 53 |
try:
|
| 54 |
# Download repo
|
|
|
|
| 61 |
|
| 62 |
# Zip temp folder
|
| 63 |
zip_path = zip_folder(temp_dir)
|
| 64 |
+
log += "β
Download complete and zipped successfully!\n"
|
| 65 |
+
log += f"π¦ ZIP file created: {os.path.basename(zip_path)}\n"
|
| 66 |
+
log += "ποΈ Temporary files will be automatically cleaned up."
|
| 67 |
|
| 68 |
# Cleanup temp folder after creating zip
|
| 69 |
shutil.rmtree(temp_dir)
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
# Cleanup on error
|
| 74 |
shutil.rmtree(temp_dir)
|
| 75 |
+
return f"β Error during download: {str(e)}", None
|
| 76 |
+
|
| 77 |
+
# Custom CSS for better styling
|
| 78 |
+
custom_css = """
|
| 79 |
+
#main-header {
|
| 80 |
+
text-align: center;
|
| 81 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 82 |
+
padding: 2rem;
|
| 83 |
+
border-radius: 10px;
|
| 84 |
+
margin-bottom: 1rem;
|
| 85 |
+
color: white;
|
| 86 |
+
}
|
| 87 |
+
#main-header h1 {
|
| 88 |
+
margin: 0;
|
| 89 |
+
font-size: 2.5rem;
|
| 90 |
+
font-weight: 700;
|
| 91 |
+
}
|
| 92 |
+
#main-header p {
|
| 93 |
+
margin: 0.5rem 0 0 0;
|
| 94 |
+
opacity: 0.9;
|
| 95 |
+
font-size: 1.1rem;
|
| 96 |
+
}
|
| 97 |
+
.feature-grid {
|
| 98 |
+
display: grid;
|
| 99 |
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
| 100 |
+
gap: 1rem;
|
| 101 |
+
margin: 1.5rem 0;
|
| 102 |
+
}
|
| 103 |
+
.feature-card {
|
| 104 |
+
background: white;
|
| 105 |
+
padding: 1rem;
|
| 106 |
+
border-radius: 8px;
|
| 107 |
+
border-left: 4px solid #667eea;
|
| 108 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 109 |
+
}
|
| 110 |
+
.download-btn {
|
| 111 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 112 |
+
color: white !important;
|
| 113 |
+
font-weight: 600 !important;
|
| 114 |
+
border: none !important;
|
| 115 |
+
}
|
| 116 |
+
.download-btn:hover {
|
| 117 |
+
transform: translateY(-2px);
|
| 118 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
| 119 |
+
}
|
| 120 |
+
.log-output {
|
| 121 |
+
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
| 122 |
+
font-size: 0.9em;
|
| 123 |
+
}
|
| 124 |
+
"""
|
| 125 |
|
| 126 |
# Gradio UI
|
| 127 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 128 |
+
|
| 129 |
+
# Header Section
|
| 130 |
+
with gr.Column(elem_id="main-header"):
|
| 131 |
+
gr.HTML("""
|
| 132 |
+
<div style="text-align: center;">
|
| 133 |
+
<h1>π€ HF Downloader</h1>
|
| 134 |
+
<p>Download & Package Hugging Face Repositories</p>
|
| 135 |
+
</div>
|
| 136 |
+
""")
|
| 137 |
+
|
| 138 |
+
# Main Description
|
| 139 |
+
gr.Markdown("""
|
| 140 |
+
**Easily download any Hugging Face model, dataset, or space repository and get it as a ZIP file.**
|
| 141 |
+
Perfect for backups, offline use, or sharing repositories.
|
| 142 |
+
""")
|
| 143 |
+
|
| 144 |
+
# Feature Cards
|
| 145 |
+
with gr.Row():
|
| 146 |
+
with gr.Column():
|
| 147 |
+
gr.Markdown("### π How to Use")
|
| 148 |
+
gr.Markdown("""
|
| 149 |
+
1. Enter repository ID (e.g., `Jonny001/HF-Downloader`)
|
| 150 |
+
2. Choose repo type or leave as 'Auto-detect'
|
| 151 |
+
3. Click **Download & ZIP**
|
| 152 |
+
4. Wait for processing and download your ZIP file
|
| 153 |
+
""")
|
| 154 |
+
|
| 155 |
+
with gr.Column():
|
| 156 |
+
gr.Markdown("### π Supported Types")
|
| 157 |
+
gr.Markdown("""
|
| 158 |
+
- **Models**: Machine learning models
|
| 159 |
+
- **Datasets**: Training/evaluation datasets
|
| 160 |
+
- **Spaces**: Gradio/demo applications
|
| 161 |
+
- **Auto-detection**: Automatic type recognition
|
| 162 |
+
""")
|
| 163 |
|
| 164 |
+
gr.Markdown("---")
|
| 165 |
+
|
| 166 |
+
# Input Section
|
| 167 |
+
with gr.Row():
|
| 168 |
+
with gr.Column(scale=3):
|
| 169 |
+
repo_input = gr.Textbox(
|
| 170 |
+
label="Hugging Face Repository ID",
|
| 171 |
+
placeholder="e.g. Jonny001/HF-Downloader, microsoft/DialoGPT-medium, huggingface/datasets",
|
| 172 |
+
info="Enter the full repository ID including username"
|
| 173 |
+
)
|
| 174 |
+
with gr.Column(scale=1):
|
| 175 |
+
repo_type = gr.Dropdown(
|
| 176 |
+
choices=["auto", "model", "dataset", "space"],
|
| 177 |
+
value="auto",
|
| 178 |
+
label="Repository Type",
|
| 179 |
+
info="Choose type or let us auto-detect"
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# Action Section
|
| 183 |
+
with gr.Row():
|
| 184 |
+
download_btn = gr.Button(
|
| 185 |
+
"π Download & Create ZIP",
|
| 186 |
+
variant="primary",
|
| 187 |
+
size="lg",
|
| 188 |
+
elem_classes="download-btn"
|
| 189 |
+
)
|
| 190 |
+
clear_btn = gr.Button("ποΈ Clear", size="lg")
|
| 191 |
+
|
| 192 |
+
# Output Section
|
| 193 |
+
with gr.Row():
|
| 194 |
+
with gr.Column():
|
| 195 |
+
log_output = gr.Textbox(
|
| 196 |
+
label="π Process Log",
|
| 197 |
+
lines=10,
|
| 198 |
+
interactive=False,
|
| 199 |
+
elem_classes="log-output",
|
| 200 |
+
show_copy_button=True
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
with gr.Row():
|
| 204 |
+
file_output = gr.File(
|
| 205 |
+
label="π¦ Download ZIP File",
|
| 206 |
+
file_types=[".zip"],
|
| 207 |
+
height=100
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
# Examples Section
|
| 211 |
+
gr.Markdown("### π― Quick Examples")
|
| 212 |
+
examples = gr.Examples(
|
| 213 |
+
examples=[
|
| 214 |
+
["Jonny001/HF-Downloader", "auto"],
|
| 215 |
+
["Jonny001/R.I.O.W", "auto"],
|
| 216 |
+
["huggingface/datasets", "auto"],
|
| 217 |
+
["Jonny001/HF-Downloader", "space"]
|
| 218 |
+
],
|
| 219 |
+
inputs=[repo_input, repo_type],
|
| 220 |
+
label="Click any example to try it out!"
|
| 221 |
)
|
| 222 |
+
|
| 223 |
+
# Event Handlers
|
|
|
|
|
|
|
|
|
|
| 224 |
download_btn.click(
|
| 225 |
fn=download_hf_repo_temp,
|
| 226 |
+
inputs=[repo_input, repo_type],
|
| 227 |
outputs=[log_output, file_output]
|
| 228 |
)
|
| 229 |
+
|
| 230 |
+
def clear_all():
|
| 231 |
+
return "", None, "auto"
|
| 232 |
+
|
| 233 |
+
clear_btn.click(
|
| 234 |
+
fn=clear_all,
|
| 235 |
+
outputs=[repo_input, file_output, repo_type]
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
# Footer
|
| 239 |
+
gr.Markdown("---")
|
| 240 |
+
gr.Markdown("""
|
| 241 |
+
<div style="text-align: center; color: #666;">
|
| 242 |
+
<p>π‘ <strong>Tip:</strong> Large repositories may take several minutes to download and process.</p>
|
| 243 |
+
<p>π All temporary files are automatically cleaned up after download.</p>
|
| 244 |
+
</div>
|
| 245 |
+
""")
|
| 246 |
|
| 247 |
+
if __name__ == "__main__":
|
| 248 |
+
demo.launch(
|
| 249 |
+
share=False,
|
| 250 |
+
show_error=True
|
| 251 |
+
)
|