Files changed (1) hide show
  1. app.py +7 -21
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import requests
2
  import os
3
  import gradio as gr
4
- from huggingface_hub import update_repo_visibility, whoami, upload_folder, create_repo, upload_file, update_repo_visibility, file_exists
5
  import subprocess
6
 
7
  import gradio as gr
@@ -43,15 +43,12 @@ def download_file(url, file_path, folder, api_key=None):
43
  raise gr.Error(f"Error downloading file: {str(e)}")
44
 
45
 
46
- def get_files_by_username(username, api_key=None):
47
  url = f"https://civitai.com/api/v1/models?username={username}&limit=100&nsfw=true"
48
  output = {}
49
- headers = {}
50
- if api_key:
51
- headers['Authorization'] = f'Bearer {api_key}'
52
 
53
  while url:
54
- response = requests.get(url, headers=headers, timeout=30)
55
  data = response.json()
56
  # Add current page items to the list
57
  for model in data['items']:
@@ -70,14 +67,10 @@ def get_files_by_username(username, api_key=None):
70
  url = metadata.get('nextPage', None)
71
  return output
72
 
73
- def get_files_by_model_id(model_id, api_key=None):
74
  api_url = f"https://civitai.com/api/v1/models/{model_id}"
75
- headers = {}
76
- if api_key:
77
- headers['Authorization'] = f'Bearer {api_key}'
78
-
79
  try:
80
- response = requests.get(api_url, headers=headers)
81
  response.raise_for_status()
82
  model = response.json()
83
 
@@ -100,10 +93,10 @@ def get_files_by_model_id(model_id, api_key=None):
100
  def process_url(url, profile, user_repo_id, oauth_token, folder, api_key=None):
101
  if url.startswith("https://civitai.com/models/"):
102
  model_id = url.split('/')[4]
103
- files = get_files_by_model_id(model_id, api_key)
104
  elif url.startswith("https://civitai.com/user/"):
105
  username = url.split('/')[4]
106
- files = get_files_by_username(username, api_key)
107
  else:
108
  raise gr.Error("Unknown CivitAI URL format, please provide model URL or user profile URL")
109
 
@@ -117,13 +110,6 @@ def process_url(url, profile, user_repo_id, oauth_token, folder, api_key=None):
117
  try:
118
  download_url = data['downloadUrl']
119
  filename = dl_path.split('/')[-1]
120
- if file_exists(
121
- repo_id = user_repo_id,
122
- filename = dl_path,
123
- token = oauth_token
124
- ):
125
- gr.Info(f"Skipping {filename}, folder exists {dl_path}")
126
- continue
127
  gr.Info(f"Downloading {filename} ({current_file}/{total_files})")
128
  download_file(download_url, dl_path, folder, api_key)
129
  # Upload the model and card
 
1
  import requests
2
  import os
3
  import gradio as gr
4
+ from huggingface_hub import update_repo_visibility, whoami, upload_folder, create_repo, upload_file, update_repo_visibility
5
  import subprocess
6
 
7
  import gradio as gr
 
43
  raise gr.Error(f"Error downloading file: {str(e)}")
44
 
45
 
46
+ def get_files_by_username(username):
47
  url = f"https://civitai.com/api/v1/models?username={username}&limit=100&nsfw=true"
48
  output = {}
 
 
 
49
 
50
  while url:
51
+ response = requests.get(url, timeout=30)
52
  data = response.json()
53
  # Add current page items to the list
54
  for model in data['items']:
 
67
  url = metadata.get('nextPage', None)
68
  return output
69
 
70
+ def get_files_by_model_id(model_id):
71
  api_url = f"https://civitai.com/api/v1/models/{model_id}"
 
 
 
 
72
  try:
73
+ response = requests.get(api_url)
74
  response.raise_for_status()
75
  model = response.json()
76
 
 
93
  def process_url(url, profile, user_repo_id, oauth_token, folder, api_key=None):
94
  if url.startswith("https://civitai.com/models/"):
95
  model_id = url.split('/')[4]
96
+ files = get_files_by_model_id(model_id)
97
  elif url.startswith("https://civitai.com/user/"):
98
  username = url.split('/')[4]
99
+ files = get_files_by_username(username)
100
  else:
101
  raise gr.Error("Unknown CivitAI URL format, please provide model URL or user profile URL")
102
 
 
110
  try:
111
  download_url = data['downloadUrl']
112
  filename = dl_path.split('/')[-1]
 
 
 
 
 
 
 
113
  gr.Info(f"Downloading {filename} ({current_file}/{total_files})")
114
  download_file(download_url, dl_path, folder, api_key)
115
  # Upload the model and card