multimodalart HF Staff commited on
Commit
acf180f
·
verified ·
1 Parent(s): ff20abd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -39
app.py CHANGED
@@ -42,6 +42,21 @@ MODEL_MAPPING_VIDEO = {
42
 
43
  SUPPORTED_CIVITAI_BASE_MODELS = list(MODEL_MAPPING_IMAGE.keys()) + list(MODEL_MAPPING_VIDEO.keys())
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  def get_json_data(url):
47
  url_split = url.split('/')
@@ -112,9 +127,10 @@ def get_prompts_from_image(image_id_str: str):
112
  prompt = ""
113
  negative_prompt = ""
114
  try:
115
- response = requests.get(url, timeout=10) # Added timeout
116
  response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
117
  data = response.json()
 
118
  # Expected structure: {'result': {'data': {'json': {'meta': {'prompt': '...', 'negativePrompt': '...'}}}}}
119
  meta = data.get('result', {}).get('data', {}).get('json', {}).get('meta')
120
  if meta: # meta can be None
@@ -150,7 +166,6 @@ def extract_info(json_data: Dict[str, Any], hunyuan_type: Optional[str] = None)
150
  elif civitai_base_model_name in MODEL_MAPPING_IMAGE:
151
  base_model_hf = MODEL_MAPPING_IMAGE[civitai_base_model_name]
152
  else:
153
- # Should not happen if SUPPORTED_CIVITAI_BASE_MODELS is derived correctly
154
  print(f"Logic error: {civitai_base_model_name} in supported list but not mapped.")
155
  continue
156
 
@@ -161,7 +176,6 @@ def extract_info(json_data: Dict[str, Any], hunyuan_type: Optional[str] = None)
161
  break
162
 
163
  if not primary_file_info:
164
- # Sometimes primary might not be explicitly set, take first 'Model' type safetensors
165
  for file_entry in model_version.get("files", []):
166
  if file_entry.get("type") == "Model" and file_entry.get("name","").endswith(".safetensors"):
167
  primary_file_info = file_entry
@@ -171,54 +185,43 @@ def extract_info(json_data: Dict[str, Any], hunyuan_type: Optional[str] = None)
171
  print(f"No primary or suitable safetensors model file found for version {model_version.get('name')}")
172
  continue
173
 
174
-
175
  urls_to_download = [{"url": primary_file_info["downloadUrl"], "filename": primary_file_info["name"], "type": "weightName"}]
176
 
177
  for image_obj in model_version.get("images", []):
178
- # Skip if image/video itself is too NSFW for non-trusted, extract_info is called by check_civit_link too early for nsfw check
179
- # The main nsfw check will handle this before download. Here we just gather info.
180
- # if image_obj.get("nsfwLevel", 0) > 5: # This check belongs in check_nsfw for non-trusted.
181
- # continue
182
-
183
  image_url = image_obj.get("url")
184
  if not image_url:
185
  continue
186
 
187
- # Extract image ID for fetching prompts. This can be fragile.
188
- # Example URLs:
189
- # https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/.../12345.jpeg (where 12345 is the id)
190
- # https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/.../width=1024/12345.jpeg
191
- # https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/.../12345.mp4
192
- filename_part = os.path.basename(image_url) # e.g., 12345.jpeg or 12345.mp4
193
  image_id_str = filename_part.split('.')[0]
194
 
195
  prompt, negative_prompt = "", ""
196
- if image_obj.get("hasMeta", False) and image_obj.get("type") == "image": # Often only images have reliable meta via this endpoint
197
  prompt, negative_prompt = get_prompts_from_image(image_id_str)
198
 
199
  urls_to_download.append({
200
  "url": image_url,
201
- "filename": filename_part, # Use the extracted filename part
202
- "type": "imageName", # Keep as imageName for consistency with README generation
203
  "prompt": prompt,
204
  "negative_prompt": negative_prompt,
205
- "media_type": image_obj.get("type", "image") # store if it's 'image' or 'video'
206
  })
207
 
208
  info = {
209
  "urls_to_download": urls_to_download,
210
  "id": model_version["id"],
211
- "baseModel": base_model_hf, # This is the HF model ID
212
- "civitai_base_model_name": civitai_base_model_name, # Original name from Civitai
213
  "is_video_model": is_video,
214
- "modelId": json_data.get("id", ""), # Main model ID from Civitai
215
  "name": json_data["name"],
216
- "description": json_data.get("description", ""), # Description can be None
217
  "trainedWords": model_version.get("trainedWords", []),
218
  "creator": json_data.get("creator", {}).get("username", "Unknown"),
219
  "tags": json_data.get("tags", []),
220
  "allowNoCredit": json_data.get("allowNoCredit", True),
221
- "allowCommercialUse": json_data.get("allowCommercialUse", "Sell"), # Default to most permissive if missing
222
  "allowDerivatives": json_data.get("allowDerivatives", True),
223
  "allowDifferentLicense": json_data.get("allowDifferentLicense", True)
224
  }
@@ -507,21 +510,6 @@ def get_creator(username):
507
  return None # Cannot proceed without cookie for this specific call
508
 
509
  url = f"https://civitai.com/api/trpc/user.getCreator?input=%7B%22json%22%3A%7B%22username%22%3A%22{username}%22%2C%22authed%22%3Atrue%7D%7D"
510
- headers = {
511
- "authority": "civitai.com",
512
- "accept": "*/*",
513
- "accept-language": "en-US,en;q=0.9", # Simplified
514
- "content-type": "application/json",
515
- "cookie": cookie_info, # Use the env var
516
- "referer": f"https://civitai.com/user/{username}/models",
517
- "sec-ch-ua": "\"Chromium\";v=\"118\", \"Not_A Brand\";v=\"99\"", # Example, update if needed
518
- "sec-ch-ua-mobile": "?0",
519
- "sec-ch-ua-platform": "\"Windows\"", # Example
520
- "sec-fetch-dest": "empty",
521
- "sec-fetch-mode": "cors",
522
- "sec-fetch-site": "same-origin",
523
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" # Example
524
- }
525
  try:
526
  response = requests.get(url, headers=headers, timeout=10)
527
  response.raise_for_status()
 
42
 
43
  SUPPORTED_CIVITAI_BASE_MODELS = list(MODEL_MAPPING_IMAGE.keys()) + list(MODEL_MAPPING_VIDEO.keys())
44
 
45
+ headers = {
46
+ "authority": "civitai.com",
47
+ "accept": "*/*",
48
+ "accept-language": "en-US,en;q=0.9", # Simplified
49
+ "content-type": "application/json",
50
+ "cookie": cookie_info, # Use the env var
51
+ "referer": f"https://civitai.com/user/{username}/models",
52
+ "sec-ch-ua": "\"Chromium\";v=\"118\", \"Not_A Brand\";v=\"99\"", # Example, update if needed
53
+ "sec-ch-ua-mobile": "?0",
54
+ "sec-ch-ua-platform": "\"Windows\"", # Example
55
+ "sec-fetch-dest": "empty",
56
+ "sec-fetch-mode": "cors",
57
+ "sec-fetch-site": "same-origin",
58
+ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" # Example
59
+ }
60
 
61
  def get_json_data(url):
62
  url_split = url.split('/')
 
127
  prompt = ""
128
  negative_prompt = ""
129
  try:
130
+ response = requests.get(url, headers=headers, timeout=10) # Added timeout
131
  response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
132
  data = response.json()
133
+ print("Response from image: ", data)
134
  # Expected structure: {'result': {'data': {'json': {'meta': {'prompt': '...', 'negativePrompt': '...'}}}}}
135
  meta = data.get('result', {}).get('data', {}).get('json', {}).get('meta')
136
  if meta: # meta can be None
 
166
  elif civitai_base_model_name in MODEL_MAPPING_IMAGE:
167
  base_model_hf = MODEL_MAPPING_IMAGE[civitai_base_model_name]
168
  else:
 
169
  print(f"Logic error: {civitai_base_model_name} in supported list but not mapped.")
170
  continue
171
 
 
176
  break
177
 
178
  if not primary_file_info:
 
179
  for file_entry in model_version.get("files", []):
180
  if file_entry.get("type") == "Model" and file_entry.get("name","").endswith(".safetensors"):
181
  primary_file_info = file_entry
 
185
  print(f"No primary or suitable safetensors model file found for version {model_version.get('name')}")
186
  continue
187
 
 
188
  urls_to_download = [{"url": primary_file_info["downloadUrl"], "filename": primary_file_info["name"], "type": "weightName"}]
189
 
190
  for image_obj in model_version.get("images", []):
 
 
 
 
 
191
  image_url = image_obj.get("url")
192
  if not image_url:
193
  continue
194
 
195
+ filename_part = os.path.basename(image_url)
 
 
 
 
 
196
  image_id_str = filename_part.split('.')[0]
197
 
198
  prompt, negative_prompt = "", ""
199
+ if image_obj.get("hasMeta", False):
200
  prompt, negative_prompt = get_prompts_from_image(image_id_str)
201
 
202
  urls_to_download.append({
203
  "url": image_url,
204
+ "filename": filename_part,
205
+ "type": "imageName",
206
  "prompt": prompt,
207
  "negative_prompt": negative_prompt,
208
+ "media_type": image_obj.get("type", "image")
209
  })
210
 
211
  info = {
212
  "urls_to_download": urls_to_download,
213
  "id": model_version["id"],
214
+ "baseModel": base_model_hf,
215
+ "civitai_base_model_name": civitai_base_model_name,
216
  "is_video_model": is_video,
217
+ "modelId": json_data.get("id", ""),
218
  "name": json_data["name"],
219
+ "description": json_data.get("description", ""),
220
  "trainedWords": model_version.get("trainedWords", []),
221
  "creator": json_data.get("creator", {}).get("username", "Unknown"),
222
  "tags": json_data.get("tags", []),
223
  "allowNoCredit": json_data.get("allowNoCredit", True),
224
+ "allowCommercialUse": json_data.get("allowCommercialUse", "Sell"),
225
  "allowDerivatives": json_data.get("allowDerivatives", True),
226
  "allowDifferentLicense": json_data.get("allowDifferentLicense", True)
227
  }
 
510
  return None # Cannot proceed without cookie for this specific call
511
 
512
  url = f"https://civitai.com/api/trpc/user.getCreator?input=%7B%22json%22%3A%7B%22username%22%3A%22{username}%22%2C%22authed%22%3Atrue%7D%7D"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
  try:
514
  response = requests.get(url, headers=headers, timeout=10)
515
  response.raise_for_status()