| import requests |
| import os |
| from google.colab import output |
|
|
| |
| |
| |
|
|
| |
| |
| download_link = "" |
|
|
| |
| |
| custom_filename = "" |
|
|
| |
| destination_folder = "/content/downloaded_models" |
|
|
| |
| |
| |
|
|
| def download_civitai_model(url, folder, filename=None): |
| if not url: |
| print("Error: Please paste the download link in the 'download_link' variable.") |
| return |
|
|
| |
| os.makedirs(folder, exist_ok=True) |
| |
| print(f"Connecting to Civitai...") |
|
|
| |
| |
| headers = { |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' |
| } |
| |
| try: |
| |
| response = requests.head(url, headers=headers, allow_redirects=True) |
| |
| |
| if not filename: |
| |
| content_disp = response.headers.get('Content-Disposition') |
| if content_disp and 'filename=' in content_disp: |
| |
| fname_str = content_disp.split('filename=')[1].strip('"') |
| final_filename = fname_str |
| else: |
| |
| print("Warning: Could not determine filename automatically. Using 'downloaded_model.safetensors'") |
| final_filename = "downloaded_model.safetensors" |
| else: |
| final_filename = filename |
|
|
| file_path = os.path.join(folder, final_filename) |
|
|
| print(f"Downloading: {final_filename}") |
| print(f"Saving to: {file_path}") |
| |
| |
| response = requests.get(url, headers=headers, stream=True) |
| |
| |
| if response.status_code != 200: |
| print(f"Error: Failed to download. Status Code: {response.status_code}") |
| print("Response:", response.text[:500]) |
| return |
|
|
| |
| total_size = int(response.headers.get('content-length', 0)) |
| bytes_downloaded = 0 |
| |
| with open(file_path, 'wb') as f: |
| for chunk in response.iter_content(chunk_size=8192): |
| if chunk: |
| f.write(chunk) |
| bytes_downloaded += len(chunk) |
| |
| if total_size > 0: |
| percent = (bytes_downloaded / total_size) * 100 |
| print(f"\rProgress: {percent:.1f}% ({bytes_downloaded/1024/1024:.1f} MB / {total_size/1024/1024:.1f} MB)", end="") |
|
|
| print("\n\n✅ Download Complete!") |
| |
| |
| file_size = os.path.getsize(file_path) / (1024 * 1024) |
| print(f"File Size: {file_size:.2f} MB") |
|
|
| except Exception as e: |
| print(f"\nAn error occurred: {e}") |
|
|
| |
| download_civitai_model(download_link, destination_folder, custom_filename) |