Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,76 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
from tqdm import tqdm
|
| 4 |
-
import logging
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
-
def download_sam_model():
|
| 8 |
"""
|
| 9 |
-
Download the SAM model weights
|
| 10 |
"""
|
| 11 |
url = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth"
|
| 12 |
|
| 13 |
try:
|
| 14 |
-
# Get the repository root directory
|
| 15 |
root_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
filename = url.split('/')[-1]
|
| 17 |
save_path = os.path.join(root_dir, filename)
|
| 18 |
|
| 19 |
-
# Setup logging
|
| 20 |
-
logging.basicConfig(level=logging.INFO)
|
| 21 |
-
logger = logging.getLogger(__name__)
|
| 22 |
-
|
| 23 |
# Check if file already exists
|
| 24 |
if os.path.exists(save_path):
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
#
|
| 28 |
response = requests.head(url)
|
| 29 |
-
|
| 30 |
|
| 31 |
-
# Download
|
| 32 |
-
logger.info(f"Downloading {filename} to Space repository root")
|
| 33 |
response = requests.get(url, stream=True)
|
| 34 |
-
progress
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
with open(save_path, 'wb') as file:
|
| 37 |
-
for data in response.iter_content(chunk_size=
|
| 38 |
-
progress.update(len(data))
|
| 39 |
file.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
return
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
# Create a
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
gr.Markdown("# Download SAM Model")
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
def download_sam_model(progress=gr.Progress()):
|
| 7 |
"""
|
| 8 |
+
Download the SAM model weights with visible progress indicator
|
| 9 |
"""
|
| 10 |
url = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth"
|
| 11 |
|
| 12 |
try:
|
| 13 |
+
# Get the repository root directory
|
| 14 |
root_dir = os.path.dirname(os.path.abspath(__file__))
|
| 15 |
filename = url.split('/')[-1]
|
| 16 |
save_path = os.path.join(root_dir, filename)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Check if file already exists
|
| 19 |
if os.path.exists(save_path):
|
| 20 |
+
return "Model file already exists in the repository root"
|
| 21 |
|
| 22 |
+
# Get file size
|
| 23 |
response = requests.head(url)
|
| 24 |
+
total_size = int(response.headers.get('content-length', 0))
|
| 25 |
|
| 26 |
+
# Download with progress tracking
|
|
|
|
| 27 |
response = requests.get(url, stream=True)
|
| 28 |
+
progress(0, desc="Starting download...")
|
| 29 |
+
|
| 30 |
+
downloaded_size = 0
|
| 31 |
+
chunk_size = 1024 * 1024 # 1MB chunks
|
| 32 |
|
| 33 |
with open(save_path, 'wb') as file:
|
| 34 |
+
for data in response.iter_content(chunk_size=chunk_size):
|
|
|
|
| 35 |
file.write(data)
|
| 36 |
+
downloaded_size += len(data)
|
| 37 |
+
# Update progress bar
|
| 38 |
+
progress(downloaded_size/total_size,
|
| 39 |
+
desc=f"Downloading... {downloaded_size/(1024*1024):.1f}MB / {total_size/(1024*1024):.1f}MB")
|
| 40 |
|
| 41 |
+
if downloaded_size != total_size:
|
| 42 |
+
if os.path.exists(save_path):
|
| 43 |
+
os.remove(save_path)
|
| 44 |
+
return "Error: Download incomplete. Please try again."
|
| 45 |
|
| 46 |
+
return f"✅ Model successfully downloaded! Saved as {filename}"
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
+
if os.path.exists(save_path):
|
| 50 |
+
os.remove(save_path)
|
| 51 |
+
return f"❌ Error downloading file: {str(e)}"
|
| 52 |
|
| 53 |
+
# Create a Gradio interface
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("# Download SAM Model")
|
| 56 |
+
gr.Markdown("This will download the Segment Anything Model (SAM) weights (~2.4GB)")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
download_button = gr.Button("📥 Download Model", variant="primary")
|
| 60 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
| 61 |
+
|
| 62 |
+
download_button.click(
|
| 63 |
+
fn=download_sam_model,
|
| 64 |
+
outputs=status_text,
|
| 65 |
+
show_progress=True,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
gr.Markdown("""
|
| 69 |
+
### Notes:
|
| 70 |
+
- Download size is approximately 2.4GB
|
| 71 |
+
- The model will be saved in the Space's root directory
|
| 72 |
+
- Please wait for the download to complete
|
| 73 |
+
""")
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
demo.launch()
|