civitaiarchive commited on
Commit
fb24a43
·
1 Parent(s): a2e360c

Add civitai-to-hf-uploader

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -43,12 +43,15 @@ 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):
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,10 +70,14 @@ def get_files_by_username(username):
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,10 +100,10 @@ def get_files_by_model_id(model_id):
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
 
 
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
  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
  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