import requests import json import gradio as gr import urllib.request import urllib.parse import urllib.error import os import re import datetime import platform from PIL import Image from io import BytesIO from collections import defaultdict from modules.images import read_info_from_image from modules.shared import cmd_opts, opts from modules.paths import models_path, extensions_dir, data_path from html import escape from scripts.civitai_global import print import scripts.civitai_global as gl import scripts.civitai_download as _download try: from fake_useragent import UserAgent except ImportError: print("Python module 'fake_useragent' has not been imported correctly, please try to restart or install it manually.") gl.init() def contenttype_folder(content_type, desc=None, fromCheck=False, custom_folder=None): use_LORA = getattr(opts, "use_LORA", False) folder = None if desc: desc = desc.upper() else: desc = "PLACEHOLDER" if custom_folder: main_models = custom_folder main_data = custom_folder else: main_models = models_path main_data = data_path if content_type == "modelFolder": folder = os.path.join(main_models) if content_type == "Checkpoint": if cmd_opts.ckpt_dir and not custom_folder: folder = cmd_opts.ckpt_dir else: folder = os.path.join(main_models,"Stable-diffusion") elif content_type == "Hypernetwork": if cmd_opts.hypernetwork_dir and not custom_folder: folder = cmd_opts.hypernetwork_dir else: folder = os.path.join(main_models, "hypernetworks") elif content_type == "TextualInversion": if cmd_opts.embeddings_dir and not custom_folder: folder = cmd_opts.embeddings_dir else: folder = os.path.join(main_data, "embeddings") elif content_type == "AestheticGradient": if not custom_folder: folder = os.path.join(extensions_dir, "stable-diffusion-webui-aesthetic-gradients", "aesthetic_embeddings") else: folder = os.path.join(custom_folder, "aesthetic_embeddings") elif content_type == "LORA": if cmd_opts.lora_dir and not custom_folder: folder = cmd_opts.lora_dir else: folder = folder = os.path.join(main_models, "Lora") elif content_type == "LoCon": folder = os.path.join(main_models, "LyCORIS") if use_LORA and not fromCheck: if cmd_opts.lora_dir and not custom_folder: folder = cmd_opts.lora_dir else: folder = folder = os.path.join(main_models, "Lora") elif content_type == "VAE": if cmd_opts.vae_dir and not custom_folder: folder = cmd_opts.vae_dir else: folder = os.path.join(main_models, "VAE") elif content_type == "Controlnet": folder = os.path.join(main_models, "ControlNet") elif content_type == "Poses": folder = os.path.join(main_models, "Poses") elif content_type == "Upscaler": if "SWINIR" in desc: if cmd_opts.swinir_models_path and not custom_folder: folder = cmd_opts.swinir_models_path else: folder = os.path.join(main_models, "SwinIR") elif "REALESRGAN" in desc: if cmd_opts.realesrgan_models_path and not custom_folder: folder = cmd_opts.realesrgan_models_path else: folder = os.path.join(main_models, "RealESRGAN") elif "GFPGAN" in desc: if cmd_opts.gfpgan_models_path and not custom_folder: folder = cmd_opts.gfpgan_models_path else: folder = os.path.join(main_models, "GFPGAN") elif "BSRGAN" in desc: if cmd_opts.bsrgan_models_path and not custom_folder: folder = cmd_opts.bsrgan_models_path else: folder = os.path.join(main_models, "BSRGAN") else: if cmd_opts.esrgan_models_path and not custom_folder: folder = cmd_opts.esrgan_models_path else: folder = os.path.join(main_models, "ESRGAN") elif content_type == "MotionModule": folder = os.path.join(extensions_dir, "sd-webui-animatediff", "model") elif content_type == "Workflows": folder = os.path.join(main_models, "Workflows") elif content_type == "Other": if "ADETAILER" in desc: folder = os.path.join(main_models, "adetailer") else: folder = os.path.join(main_models, "Other") elif content_type == "Wildcards": folder = os.path.join(extensions_dir, "UnivAICharGen", "wildcards") if not os.path.exists(folder): folder = os.path.join(extensions_dir, "sd-dynamic-prompts", "wildcards") return folder def api_to_data(content_type, sort_type, period_type, use_search_term, current_page, base_filter, only_liked, tile_count, search_term=None, nsfw=None, timeOut=None, isNext=None, inputs_changed=None): if current_page in [0, None, ""]: current_page = 1 if inputs_changed: gl.file_scan = False api_url = f"https://civitai.com/api/v1/models?limit={tile_count}&page=1" else: api_url = f"https://civitai.com/api/v1/models?limit={tile_count}&page={current_page}" if timeOut: if isNext: next_page = str(int(current_page) + 1) else: if current_page not in [1, 0, None, ""]: next_page = str(int(current_page) - 1) api_url = f"https://civitai.com/api/v1/models?limit={tile_count}&page={next_page}" if period_type: period_type = period_type.replace(" ", "") query = {'sort': sort_type, 'period': period_type} types_query_str = "" if content_type: types_query_str = "".join([f"&types={type}" for type in content_type]) query_str = urllib.parse.urlencode(query, quote_via=urllib.parse.quote) if types_query_str: query_str += types_query_str if use_search_term != "None" and search_term: search_term = search_term.replace("\\","\\\\") if "civitai.com" in search_term: match = re.search(r'models/(\d+)', search_term) model_number = match.group(1) query_str = f"&ids={urllib.parse.quote(model_number)}" elif use_search_term == "User name": query_str += f"&username={urllib.parse.quote(search_term)}" elif use_search_term == "Tag": query_str += f"&tag={urllib.parse.quote(search_term)}" else: query_str += f"&query={urllib.parse.quote(search_term)}" if base_filter: for base in base_filter: query_str += f"&baseModels={urllib.parse.quote(base)}" if only_liked: query_str += f"&favorites=true" if nsfw == False: query_str += f"&nsfw=false" full_url = f"{api_url}&{query_str}" if gl.file_scan: highest_number = max(gl.url_list_with_numbers.keys()) full_url = gl.url_list_with_numbers.get(int(current_page)) nextPage = int(current_page) + 1 prevPage = int(current_page) - 1 data = request_civit_api(full_url) data["metadata"]["currentPage"] = current_page data["metadata"]["totalPages"] = highest_number if not nextPage > highest_number: data["metadata"]["nextPage"] = gl.url_list_with_numbers.get(nextPage) if not prevPage == 0: data["metadata"]["prevPage"] = gl.url_list_with_numbers.get(prevPage) else: data = request_civit_api(full_url) return data def model_list_html(json_data): video_playback = getattr(opts, "video_playback", True) playback = "" if video_playback: playback = "autoplay loop" hide_early_access = getattr(opts, "hide_early_access", True) filtered_items = [] current_time = datetime.datetime.utcnow() for item in json_data['items']: versions_to_keep = [] for version in item['modelVersions']: if not version['files']: continue if hide_early_access: early_access_days = version['earlyAccessTimeFrame'] if early_access_days != 0: published_at_str = version.get('publishedAt') if published_at_str is not None: published_at = datetime.datetime.strptime(version['publishedAt'], "%Y-%m-%dT%H:%M:%S.%fZ") adjusted_date = published_at + datetime.timedelta(days=early_access_days) if not current_time > adjusted_date or not published_at_str: continue versions_to_keep.append(version) if versions_to_keep: item['modelVersions'] = versions_to_keep filtered_items.append(item) json_data['items'] = filtered_items HTML = '
')
if model_version is None:
selected_version = item['modelVersions'][0]
else:
for model in item['modelVersions']:
if model['name'] == model_version:
selected_version = model
break
if selected_version['trainedWords']:
output_training = ",".join(selected_version['trainedWords'])
output_training = re.sub(r'<[^>]*:[^>]*>', '', output_training)
output_training = re.sub(r', ?', ', ', output_training)
output_training = output_training.strip(', ')
if selected_version['baseModel']:
output_basemodel = selected_version['baseModel']
for file in selected_version['files']:
dl_dict[file['name']] = file['downloadUrl']
if not model_filename:
model_filename = file['name']
dl_url = file['downloadUrl']
gl.json_info = item
sha256_value = file['hashes'].get('SHA256', 'Unknown')
size = file['metadata'].get('size', 'Unknown')
format = file['metadata'].get('format', 'Unknown')
fp = file['metadata'].get('fp', 'Unknown')
sizeKB = file.get('sizeKB', 0) * 1024
filesize = _download.convert_size(sizeKB)
unique_file_name = f"{size} {format} {fp} ({filesize})"
is_primary = file.get('primary', False)
file_list.append(unique_file_name)
file_dict.append({
"format": format,
"sizeKB": sizeKB
})
if is_primary:
default_file = unique_file_name
model_filename = file['name']
dl_url = file['downloadUrl']
gl.json_info = item
sha256_value = file['hashes'].get('SHA256', 'Unknown')
safe_tensor_found = False
pickle_tensor_found = False
if is_LORA and file_dict:
for file_info in file_dict:
file_format = file_info.get("format", "")
if "SafeTensor" in file_format:
safe_tensor_found = True
if "PickleTensor" in file_format:
pickle_tensor_found = True
if safe_tensor_found and pickle_tensor_found:
if "PickleTensor" in file_dict[0].get("format", ""):
if file_dict[0].get("sizeKB", 0) <= 100:
model_folder = os.path.join(contenttype_folder("TextualInversion"))
model_url = selected_version.get('downloadUrl', '')
model_main_url = f"https://civitai.com/models/{item['id']}"
img_html = ''
for index, pic in enumerate(selected_version['images']):
meta_button = False
meta = pic['meta']
if meta and meta.get('prompt'):
meta_button = True
BtnImage = True
# Change width value in URL to original image width
image_url = re.sub(r'/width=\d+', f'/width={pic["width"]}', pic["url"])
if pic['type'] == "video":
image_url = image_url.replace("width=", "transcode=true,width=")
nsfw = 'class="model-block"'
if pic['nsfw'] not in ["None", "Soft"]:
nsfw = 'class="civnsfw model-block"'
img_html += f'''
'''
if meta_button:
img_html += f'''
'''
else:
img_html += ''
if meta:
img_html += ''
# Define the preferred order of keys and convert them to lowercase
preferred_order = ["prompt", "negativePrompt", "seed", "Size", "Model", "clipSkip", "sampler", "steps", "cfgScale"]
preferred_order_lower = [key.lower() for key in preferred_order]
# Loop through the keys in the preferred order and add them to the HTML
for key in preferred_order:
if key in meta:
value = meta[key]
if meta_btn:
img_html += f'
"""
for key in remaining_keys:
value = meta[key]
img_html += f' '
img_html = img_html + ''
img_html += '
'
else:
img_html += f' '
# Check if there are remaining keys in meta
remaining_keys = [key for key in meta if key.lower() not in preferred_order_lower]
# Add the rest
if remaining_keys:
img_html += f"""
'
img_html = img_html + ''
img_html = img_html + ''
tags_html = ''.join([f'{escape(str(tag))}' for tag in tags])
def perms_svg(color):
return f''\
f''
deny_svg = f'{perms_svg("red")} '
perms_html= ''\
f'{allow_svg if item.get("allowNoCredit") else deny_svg} Use the model without crediting the creator
'\
f'{allow_svg if item.get("allowCommercialUse") in ["Image", "Rent", "RentCivit", "Sell"] else deny_svg} Sell images they generate
'\
f'{allow_svg if item.get("allowCommercialUse") in ["Rent", "Sell"] else deny_svg} Run on services that generate images for money
'\
f'{allow_svg if item.get("allowCommercialUse") in ["RentCivit", "Rent", "Sell"] else deny_svg} Run on Civitai
'\
f'{allow_svg if item.get("allowDerivatives") else deny_svg} Share merges using this model
'\
f'{allow_svg if item.get("allowCommercialUse") == "Sell" else deny_svg} Sell this model or merges using this model
'\
f'{allow_svg if item.get("allowDifferentLicense") else deny_svg} Have different permissions when sharing merges'\
'
'
output_html = f'''
{escape(str(model_name))}
Uploaded by {escape(str(model_uploader))}{uploader_avatar}
- Version
- {escape(str(model_version))}
- Base Model
- {escape(str(output_basemodel))}
- CivitAI Tags
-
{"
- Download Link
" if model_url else ''}
{f'- {model_url}
' if model_url else ''}
{perms_html}
Description
{model_desc}
{img_html}
'''
if only_html:
return output_html
folder_location = "None"
default_subfolder = "None"
sub_folders = ["None"]
for root, dirs, files in os.walk(model_folder, followlinks=True):
for filename in files:
if filename.endswith('.json'):
json_file_path = os.path.join(root, filename)
with open(json_file_path, 'r', encoding="utf-8") as f:
try:
data = json.load(f)
sha256 = data.get('sha256')
if sha256:
sha256 = sha256.upper()
if sha256 == sha256_value:
folder_location = root
BtnDownInt = False
BtnDel = True
break
except Exception as e:
print(f"Error decoding JSON: {str(e)}")
else:
for filename in files:
if filename == model_filename or filename == cleaned_name(model_filename):
folder_location = root
BtnDownInt = False
BtnDel = True
break
if folder_location != "None":
break
insert_sub_1 = getattr(opts, "insert_sub_1", False)
insert_sub_2 = getattr(opts, "insert_sub_2", False)
insert_sub_3 = getattr(opts, "insert_sub_3", False)
insert_sub_4 = getattr(opts, "insert_sub_4", False)
insert_sub_5 = getattr(opts, "insert_sub_5", False)
insert_sub_6 = getattr(opts, "insert_sub_6", False)
insert_sub_7 = getattr(opts, "insert_sub_7", False)
insert_sub_8 = getattr(opts, "insert_sub_8", False)
insert_sub_9 = getattr(opts, "insert_sub_9", False)
insert_sub_10 = getattr(opts, "insert_sub_10", False)
insert_sub_11 = getattr(opts, "insert_sub_11", False)
insert_sub_12 = getattr(opts, "insert_sub_12", False)
insert_sub_13 = getattr(opts, "insert_sub_13", False)
insert_sub_14 = getattr(opts, "insert_sub_14", False)
dot_subfolders = getattr(opts, "dot_subfolders", True)
try:
sub_folders = ["None"]
for root, dirs, _ in os.walk(model_folder, followlinks=True):
if dot_subfolders:
dirs = [d for d in dirs if not d.startswith('.')]
dirs = [d for d in dirs if not any(part.startswith('.') for part in os.path.join(root, d).split(os.sep))]
for d in dirs:
sub_folder = os.path.relpath(os.path.join(root, d), model_folder)
if sub_folder:
sub_folders.append(f'{os.sep}{sub_folder}')
sub_folders.remove("None")
sub_folders = sorted(sub_folders, key=lambda x: (x.lower(), x))
sub_folders.insert(0, "None")
base = cleaned_name(model_uploader)
author = cleaned_name(model_uploader)
name = cleaned_name(model_name)
ver = cleaned_name(model_version)
if insert_sub_1:
sub_folders.insert(1, os.path.join(os.sep, base))
if insert_sub_2:
sub_folders.insert(2, os.path.join(os.sep, base, author))
if insert_sub_3:
sub_folders.insert(3, os.path.join(os.sep, base, author, name))
if insert_sub_4:
sub_folders.insert(4, os.path.join(os.sep, base, author, name, ver))
if insert_sub_5:
sub_folders.insert(5, os.path.join(os.sep, base, name))
if insert_sub_6:
sub_folders.insert(6, os.path.join(os.sep, base, name, ver))
if insert_sub_7:
sub_folders.insert(7, os.path.join(os.sep, author))
if insert_sub_8:
sub_folders.insert(8, os.path.join(os.sep, author, base))
if insert_sub_9:
sub_folders.insert(9, os.path.join(os.sep, author, base, name))
if insert_sub_10:
sub_folders.insert(10, os.path.join(os.sep, author, base, name, ver))
if insert_sub_11:
sub_folders.insert(11, os.path.join(os.sep, author, name))
if insert_sub_12:
sub_folders.insert(12, os.path.join(os.sep, author, name, ver))
if insert_sub_13:
sub_folders.insert(13, os.path.join(os.sep, name))
if insert_sub_14:
sub_folders.insert(14, os.path.join(os.sep, name, ver))
list = set()
sub_folders = [x for x in sub_folders if not (x in list or list.add(x))]
except:
sub_folders = ["None"]
default_sub = sub_folder_value(content_type, desc)
variable_mapping = {
"Base model": base,
"Author name": author,
"Model name": name,
"Model version": ver
}
if any(key in default_sub for key in variable_mapping.keys()):
path_components = [variable_mapping.get(component.strip(os.sep), component.strip(os.sep)) for component in default_sub.split(os.sep)]
default_sub = os.path.join(*path_components)
if folder_location == "None":
folder_location = model_folder
if default_sub != "None":
folder_path = folder_location + default_sub
else:
folder_path = folder_location
else:
folder_path = folder_location
relative_path = os.path.relpath(folder_location, model_folder)
default_subfolder = f'{os.sep}{relative_path}' if relative_path != "." else default_sub if BtnDel == False else "None"
if gl.isDownloading:
item = gl.download_queue[0]
if int(model_id) == int(item['model_id']):
BtnDel = False
BtnDownTxt = "Download model"
if len(gl.download_queue) > 0:
BtnDownTxt = "Add to queue"
for item in gl.download_queue:
if item['version_name'] == model_version and int(item['model_id']) == int(model_id):
BtnDownInt = False
break
return (
gr.HTML.update(value=output_html), # Preview HTML
gr.Textbox.update(value=output_training, interactive=True), # Trained Tags
gr.Textbox.update(value=output_basemodel), # Base Model Number
gr.Button.update(visible=False if BtnDel else True, interactive=BtnDownInt, value=BtnDownTxt), # Download Button
gr.Button.update(interactive=BtnImage), # Images Button
gr.Button.update(visible=BtnDel, interactive=BtnDel), # Delete Button
gr.Dropdown.update(choices=file_list, value=default_file, interactive=True), # File List
gr.Textbox.update(value=cleaned_name(model_filename), interactive=True), # Model File Name
gr.Textbox.update(value=dl_url), # Download URL
gr.Textbox.update(value=model_id), # Model ID
gr.Textbox.update(value=sha256_value), # SHA256
gr.Textbox.update(interactive=True, value=folder_path if model_name else None), # Install Path
gr.Dropdown.update(choices=sub_folders, value=default_subfolder, interactive=True) # Sub Folder List
)
else:
return (
gr.HTML.update(value=None), # Preview HTML
gr.Textbox.update(value=None, interactive=False), # Trained Tags
gr.Textbox.update(value=''), # Base Model Number
gr.Button.update(visible=False if BtnDel else True, value="Download model"), # Download Button
gr.Button.update(interactive=False), # Images Button
gr.Button.update(visible=BtnDel, interactive=BtnDel), # Delete Button
gr.Dropdown.update(choices=None, value=None, interactive=False), # File List
gr.Textbox.update(value=None, interactive=False), # Model File Name
gr.Textbox.update(value=None), # Download URL
gr.Textbox.update(value=None), # Model ID
gr.Textbox.update(value=None), # SHA256
gr.Textbox.update(interactive=False, value=None), # Install Path
gr.Dropdown.update(choices=None, value=None, interactive=False) # Sub Folder List
)
def sub_folder_value(content_type, desc=None):
use_LORA = getattr(opts, "use_LORA", False)
if content_type in ["LORA", "LoCon"] and use_LORA:
folder = getattr(opts, "LORA_LoCon_subfolder", "None")
elif content_type == "Upscaler":
for upscale_type in ["SWINIR", "REALESRGAN", "GFPGAN", "BSRGAN"]:
if upscale_type in desc:
folder = getattr(opts, f"{upscale_type}_subfolder", "None")
folder = getattr(opts, "ESRGAN_subfolder", "None")
else:
folder = getattr(opts, f"{content_type}_subfolder", "None")
if folder == None:
return "None"
return folder
def update_file_info(model_string, model_version, file_metadata):
file_list = []
is_LORA = False
embed_check = False
model_name = None
model_id = None
model_name, model_id = extract_model_info(model_string)
if model_version and "[Installed]" in model_version:
model_version = model_version.replace(" [Installed]", "")
if model_id and model_version:
for item in gl.json_data['items']:
if int(item['id']) == int(model_id):
content_type = item['type']
if content_type == "LORA":
is_LORA = True
desc = item['description']
for model in item['modelVersions']:
if model['name'] == model_version:
for file in model['files']:
size = file['metadata'].get('size', 'Unknown')
format = file['metadata'].get('format', 'Unknown')
unique_file_name = f"{size} {format}"
file_list.append(unique_file_name)
pass
if is_LORA and file_list:
extracted_formats = [file.split(' ')[1] for file in file_list]
if "SafeTensor" in extracted_formats and "PickleTensor" in extracted_formats:
embed_check = True
for file in model['files']:
model_id = item['id']
file_name = file.get('name', 'Unknown')
sha256 = file['hashes'].get('SHA256', 'Unknown')
metadata = file.get('metadata', {})
file_size = metadata.get('size', 'Unknown')
file_format = metadata.get('format', 'Unknown')
file_fp = metadata.get('fp', 'Unknown')
sizeKB = file.get('sizeKB', 0)
sizeB = sizeKB * 1024
filesize = _download.convert_size(sizeB)
if f"{file_size} {file_format} {file_fp} ({filesize})" == file_metadata:
installed = False
folder_location = "None"
model_folder = os.path.join(contenttype_folder(content_type, desc))
if embed_check and file_format == "PickleTensor":
if sizeKB <= 100:
model_folder = os.path.join(contenttype_folder("TextualInversion"))
dl_url = file['downloadUrl']
gl.json_info = item
for root, _, files in os.walk(model_folder, followlinks=True):
if file_name in files:
installed = True
folder_location = root
break
if not installed:
for root, _, files in os.walk(model_folder, followlinks=True):
for filename in files:
if filename.endswith('.json'):
with open(os.path.join(root, filename), 'r', encoding="utf-8") as f:
try:
data = json.load(f)
sha256_value = data.get('sha256')
if sha256_value != None and sha256_value.upper() == sha256:
folder_location = root
installed = True
break
except Exception as e:
print(f"Error decoding JSON: {str(e)}")
default_sub = sub_folder_value(content_type, desc)
if folder_location == "None":
folder_location = model_folder
if default_sub != "None":
folder_path = folder_location + default_sub
else:
folder_path = folder_location
else:
folder_path = folder_location
relative_path = os.path.relpath(folder_location, model_folder)
default_subfolder = f'{os.sep}{relative_path}' if relative_path != "." else default_sub if installed == False else "None"
BtnDownInt = not installed
BtnDownTxt = "Download model"
if len(gl.download_queue) > 0:
BtnDownTxt = "Add to queue"
for item in gl.download_queue:
if item['version_name'] == model_version:
BtnDownInt = False
break
return (
gr.Textbox.update(value=cleaned_name(file['name']), interactive=True), # Model File Name Textbox
gr.Textbox.update(value=dl_url), # Download URL Textbox
gr.Textbox.update(value=model_id), # Model ID Textbox
gr.Textbox.update(value=sha256), # sha256 textbox
gr.Button.update(interactive=BtnDownInt, visible=False if installed else True, value=BtnDownTxt), # Download Button
gr.Button.update(interactive=True if installed else False, visible=True if installed else False), # Delete Button
gr.Textbox.update(interactive=True, value=folder_path if model_name else None), # Install Path
gr.Dropdown.update(value=default_subfolder, interactive=True) # Sub Folder List
)
return (
gr.Textbox.update(value=None, interactive=False), # Model File Name Textbox
gr.Textbox.update(value=None), # Download URL Textbox
gr.Textbox.update(value=None), # Model ID Textbox
gr.Textbox.update(value=None), # sha256 textbox
gr.Button.update(interactive=False, visible=True), # Download Button
gr.Button.update(interactive=False, visible=False), # Delete Button
gr.Textbox.update(interactive=False, value=None), # Install Path
gr.Dropdown.update(choices=None, value=None, interactive=False) # Sub Folder List
)
def get_headers():
api_key = getattr(opts, "custom_api_key", "")
try:
user_agent = UserAgent().chrome
except ImportError:
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
headers = {
'User-Agent': user_agent,
'Sec-Ch-Ua': '"Brave";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Sec-Gpc': '1',
'Upgrade-Insecure-Requests': '1',
}
if api_key:
headers['Authorization'] = f'Bearer {api_key}'
return headers
def request_civit_api(api_url=None):
headers = get_headers()
try:
response = requests.get(api_url, headers=headers, timeout=(10, 30))
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return "timeout"
else:
response.encoding = "utf-8"
try:
data = json.loads(response.text)
except json.JSONDecodeError:
print("The CivitAI servers are currently offline. Please try again later.")
return "timeout"
return data