added jsonschema for validating. fixed hardcoded basedir path

pull/41/head
GeorgLegato 2023-04-20 02:44:36 +02:00
parent b143c18058
commit 9c49de7a48
2 changed files with 79 additions and 7 deletions

View File

@ -1,9 +1,9 @@
import sys
import os
import time
import json
from jsonschema import validate
basedir = os.getcwd()
sys.path.extend(basedir + "/extensions/infinite-zoom-automatic1111-webui/")
import numpy as np
import gradio as gr
from PIL import Image
@ -12,7 +12,7 @@ import json
from iz_helpers import shrink_and_paste_on_blank, write_video
from webui import wrap_gradio_gpu_call
from modules import script_callbacks
from modules import script_callbacks, scripts
import modules.shared as shared
from modules.processing import (
process_images,
@ -26,6 +26,10 @@ from modules.ui import create_output_panel, plaintext_to_html
import modules.sd_models
import modules.sd_samplers
from modules import scripts
usefulDirs = scripts.basedir().split(os.sep)[-2:] # contains install and our extension foldername
jsonprompt_schemafile = usefulDirs[0]+"/"+usefulDirs[1]+"/scripts/promptschema.json"
available_samplers = [s.name for s in modules.sd_samplers.samplers]
default_prompt = '{"prompts":{"data":[[0,"Cat"],["1","Dog"],["2","Happy Pets"]],"headers":["outpaint steps","prompt"]},"negPrompt":"ugly"}'
@ -409,11 +413,23 @@ def create_zoom_single(
plaintext_to_html(""),
)
def validatePromptJson_throws(data):
with open(jsonprompt_schemafile, "r") as s: schema = json.load(s)
validate(instance=data, schema=schema)
def putPrompts(files):
with open(files.name, 'r') as f:
file_contents = f.read()
data = json.loads(file_contents)
return [gr.DataFrame.update(data["prompts"]), gr.Textbox.update(data["negPrompt"])]
try:
with open(files.name, 'r') as f:
file_contents = f.read()
data = json.loads(file_contents)
validatePromptJson_throws(data)
return [gr.DataFrame.update(data["prompts"]), gr.Textbox.update(data["negPrompt"])]
except Exception:
gr.Error("loading your prompt failed. It seems to be invalid. Your prompt table is preserved.")
return [gr.DataFrame.update(), gr.Textbox.update()]
def clearPrompts():
return [gr.DataFrame.update(value=[[0,"Infinite Zoom. Start over"]]), gr.Textbox.update("")]
@ -452,6 +468,7 @@ def on_ui_tabs():
try:
jpr = json.loads(pr)
validatePromptJson_throws(jpr)
except Exception:
jpr = invalid_prompt

55
scripts/promptschema.json Normal file
View File

@ -0,0 +1,55 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"prompts": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "array",
"items": [
{
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "string"
}
]
},
{
"type": "string"
}
],
"minItems": 2,
"maxItems": 2,
"uniqueItems": true
},
"minItems": 1
},
"headers": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2
}
},
"required": [
"data",
"headers"
]
},
"negPrompt": {
"type": "string"
}
},
"required": [
"prompts",
"negPrompt"
]
}