Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
-
from huggingface_hub import HfApi
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
def download_and_push_model(progress=gr.Progress()):
|
|
@@ -9,11 +9,31 @@ def download_and_push_model(progress=gr.Progress()):
|
|
| 9 |
Download SAM model and push it to Hugging Face Space
|
| 10 |
"""
|
| 11 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Initialize Hugging Face API
|
| 13 |
api = HfApi()
|
| 14 |
space_id = "lyimo/downloadmodel"
|
| 15 |
model_url = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Create a temporary directory for downloading
|
| 18 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 19 |
progress(0.1, desc="Started download process...")
|
|
@@ -34,21 +54,23 @@ def download_and_push_model(progress=gr.Progress()):
|
|
| 34 |
progress(0.2 + 0.6 * (downloaded_size/total_size),
|
| 35 |
desc=f"Downloading... {downloaded_size/(1024*1024):.1f}MB / {total_size/(1024*1024):.1f}MB")
|
| 36 |
|
| 37 |
-
progress(0.8, desc="
|
| 38 |
|
| 39 |
-
# Upload to Hugging Face
|
| 40 |
api.upload_file(
|
| 41 |
path_or_fileobj=local_path,
|
| 42 |
path_in_repo="sam_vit_h_4b8939.pth",
|
| 43 |
repo_id=space_id,
|
| 44 |
-
repo_type="space"
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
progress(1.0, desc="Complete!")
|
| 48 |
return "β
Model successfully downloaded and pushed to your Space!"
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"β Error: {str(e)}"
|
| 52 |
|
| 53 |
# Create Gradio interface
|
| 54 |
with gr.Blocks() as demo:
|
|
@@ -59,6 +81,16 @@ with gr.Blocks() as demo:
|
|
| 59 |
download_button = gr.Button("π₯ Download & Push Model", variant="primary")
|
| 60 |
status_text = gr.Textbox(label="Status", interactive=False)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
download_button.click(
|
| 63 |
fn=download_and_push_model,
|
| 64 |
outputs=status_text,
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from huggingface_hub import HfApi, login, create_repo
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
def download_and_push_model(progress=gr.Progress()):
|
|
|
|
| 9 |
Download SAM model and push it to Hugging Face Space
|
| 10 |
"""
|
| 11 |
try:
|
| 12 |
+
# Login to Hugging Face
|
| 13 |
+
token = os.environ.get('HF_TOKEN')
|
| 14 |
+
if not token:
|
| 15 |
+
return "β Error: HF_TOKEN not found in environment variables. Please add it to Space secrets."
|
| 16 |
+
|
| 17 |
+
login(token) # Authenticate with Hugging Face
|
| 18 |
+
|
| 19 |
# Initialize Hugging Face API
|
| 20 |
api = HfApi()
|
| 21 |
space_id = "lyimo/downloadmodel"
|
| 22 |
model_url = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth"
|
| 23 |
|
| 24 |
+
progress(0.05, desc="Ensuring repository exists...")
|
| 25 |
+
try:
|
| 26 |
+
# Try to create the repo (will fail if it already exists, which is fine)
|
| 27 |
+
create_repo(
|
| 28 |
+
repo_id=space_id,
|
| 29 |
+
repo_type="space",
|
| 30 |
+
token=token,
|
| 31 |
+
exist_ok=True
|
| 32 |
+
)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
progress(0.1, desc="Repository already exists, continuing...")
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
# Create a temporary directory for downloading
|
| 38 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 39 |
progress(0.1, desc="Started download process...")
|
|
|
|
| 54 |
progress(0.2 + 0.6 * (downloaded_size/total_size),
|
| 55 |
desc=f"Downloading... {downloaded_size/(1024*1024):.1f}MB / {total_size/(1024*1024):.1f}MB")
|
| 56 |
|
| 57 |
+
progress(0.8, desc="Uploading to Hugging Face Space...")
|
| 58 |
|
| 59 |
+
# Upload to Hugging Face using commit operation
|
| 60 |
api.upload_file(
|
| 61 |
path_or_fileobj=local_path,
|
| 62 |
path_in_repo="sam_vit_h_4b8939.pth",
|
| 63 |
repo_id=space_id,
|
| 64 |
+
repo_type="space",
|
| 65 |
+
token=token,
|
| 66 |
+
commit_message="Upload SAM model weights"
|
| 67 |
)
|
| 68 |
|
| 69 |
progress(1.0, desc="Complete!")
|
| 70 |
return "β
Model successfully downloaded and pushed to your Space!"
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
+
return f"β Error: {str(e)}\nToken status: {'Token exists' if token else 'No token found'}"
|
| 74 |
|
| 75 |
# Create Gradio interface
|
| 76 |
with gr.Blocks() as demo:
|
|
|
|
| 81 |
download_button = gr.Button("π₯ Download & Push Model", variant="primary")
|
| 82 |
status_text = gr.Textbox(label="Status", interactive=False)
|
| 83 |
|
| 84 |
+
gr.Markdown("""
|
| 85 |
+
### Important Setup Steps:
|
| 86 |
+
1. Get your Hugging Face token from https://huggingface.co/settings/tokens
|
| 87 |
+
2. Add the token to Space secrets:
|
| 88 |
+
- Go to Space Settings > Secrets
|
| 89 |
+
- Add new secret named `HF_TOKEN`
|
| 90 |
+
- Paste your token as the value
|
| 91 |
+
3. Restart the Space after adding the token
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
download_button.click(
|
| 95 |
fn=download_and_push_model,
|
| 96 |
outputs=status_text,
|