-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
693 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import contextlib | ||
|
||
import gradio as gr | ||
# from modules import scripts, shared, script_callbacks | ||
# from modules.ui_components import FormRow, FormColumn, FormGroup, ToolButton | ||
import json | ||
import os | ||
import random | ||
stylespath = "sdxl_styles.json" | ||
|
||
|
||
def get_json_content(file_path): | ||
try: | ||
with open(file_path, 'rt', encoding="utf-8") as file: | ||
json_data = json.load(file) | ||
return json_data | ||
except Exception as e: | ||
print(f"A Problem occurred: {str(e)}") | ||
|
||
|
||
def read_sdxl_styles(json_data): | ||
# Check that data is a list | ||
if not isinstance(json_data, list): | ||
print("Error: input data must be a list") | ||
return None | ||
|
||
names = [] | ||
|
||
# Iterate over each item in the data list | ||
for item in json_data: | ||
# Check that the item is a dictionary | ||
if isinstance(item, dict): | ||
# Check that 'name' is a key in the dictionary | ||
if 'name' in item: | ||
# Append the value of 'name' to the names list | ||
names.append(item['name']) | ||
names.sort() | ||
return names | ||
|
||
|
||
def getStyles(): | ||
global stylespath | ||
json_path = os.path.join(scripts.basedir(), 'sdxl_styles.json') | ||
stylespath = json_path | ||
json_data = get_json_content(json_path) | ||
styles = read_sdxl_styles(json_data) | ||
return styles | ||
|
||
|
||
def createPositive(style, positive): | ||
json_data = get_json_content(stylespath) | ||
try: | ||
# Check if json_data is a list | ||
if not isinstance(json_data, list): | ||
raise ValueError( | ||
"Invalid JSON data. Expected a list of templates.") | ||
|
||
for template in json_data: | ||
# Check if template contains 'name' and 'prompt' fields | ||
if 'name' not in template or 'prompt' not in template: | ||
raise ValueError( | ||
"Invalid template. Missing 'name' or 'prompt' field.") | ||
|
||
# Replace {prompt} in the matching template | ||
if template['name'] == style: | ||
positive = template['prompt'].replace( | ||
'{prompt}', positive) | ||
|
||
return positive | ||
|
||
# If function hasn't returned yet, no matching template was found | ||
raise ValueError(f"No template found with name '{style}'.") | ||
|
||
except Exception as e: | ||
print(f"An error occurred: {str(e)}") | ||
|
||
|
||
def createNegative(style, negative): | ||
json_data = get_json_content(stylespath) | ||
try: | ||
# Check if json_data is a list | ||
if not isinstance(json_data, list): | ||
raise ValueError( | ||
"Invalid JSON data. Expected a list of templates.") | ||
|
||
for template in json_data: | ||
# Check if template contains 'name' and 'prompt' fields | ||
if 'name' not in template or 'prompt' not in template: | ||
raise ValueError( | ||
"Invalid template. Missing 'name' or 'prompt' field.") | ||
|
||
# Replace {prompt} in the matching template | ||
if template['name'] == style: | ||
json_negative_prompt = template.get('negative_prompt', "") | ||
if negative: | ||
negative = f"{json_negative_prompt}, {negative}" if json_negative_prompt else negative | ||
else: | ||
negative = json_negative_prompt | ||
|
||
return negative | ||
|
||
# If function hasn't returned yet, no matching template was found | ||
raise ValueError(f"No template found with name '{style}'.") | ||
|
||
except Exception as e: | ||
print(f"An error occurred: {str(e)}") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
import os | ||
from modules import scripts | ||
from modules.api.models import TextToImageJsonModel | ||
|
||
def get_json_content(file_path): | ||
try: | ||
with open(file_path, 'rt', encoding="utf-8") as file: | ||
json_data = json.load(file) | ||
return json_data | ||
except Exception as e: | ||
print(f"A Problem occurred: {str(e)}") | ||
return [] | ||
|
||
def get_text2img_data(model_id: str): | ||
json_path = os.path.join(scripts.basedir(), 'text2img.json') | ||
json_data = get_json_content(json_path) | ||
if not isinstance(json_data, list): | ||
print("Error: input data must be a list") | ||
return None | ||
|
||
model_id_key = 'model_id' | ||
sampeller_method = 'sampeller_method' | ||
step = 'step' | ||
cfg = 'cfg' | ||
prompt = 'prompt' | ||
negative_prompt = 'negative_prompt' | ||
|
||
# Iterate over each item in the data list | ||
for item in json_data: | ||
# Check that the item is a dictionary | ||
if isinstance(item, dict): | ||
# Check that all required key is in the dictionary | ||
if model_id_key in item and item[model_id_key] == model_id: | ||
if sampeller_method in item and step in item and cfg in item and prompt in item and negative_prompt in item: | ||
return TextToImageJsonModel(model_id = model_id, | ||
sampeller_method = item[sampeller_method], | ||
step = item[step], | ||
cfg = item[cfg], | ||
prompt = item[prompt], | ||
negative_prompt = item[negative_prompt]) | ||
print("Error: model not found") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.