enhances interface

This commit is contained in:
HappyZ 2023-04-29 00:45:07 -07:00
parent f5d04126fd
commit 1f08dc155e
4 changed files with 152 additions and 44 deletions

13
main.py
View File

@ -35,6 +35,7 @@ from utilities.text2img import Text2Img
app = Flask(__name__)
fast_web_debugging = False
memory_lock = Lock()
event_termination = Event()
logger = Logger(name=LOGGER_NAME)
@ -183,10 +184,11 @@ def load_model(logger: Logger) -> Model:
# "stabilityai/stable-diffusion-2-1"
# "SG161222/Realistic_Vision_V2.0"
# "darkstorm2150/Protogen_x3.4_Official_Release"
# "darkstorm2150/Protogen_x5.8_Official_Release"
# "prompthero/openjourney"
# "naclbit/trinart_stable_diffusion_v2"
# "hakurei/waifu-diffusion"
model_name = "darkstorm2150/Protogen_x3.4_Official_Release"
model_name = "darkstorm2150/Protogen_x5.8_Official_Release"
# inpainting model candidates:
# "runwayml/stable-diffusion-inpainting"
inpainting_model_name = "runwayml/stable-diffusion-inpainting"
@ -239,7 +241,12 @@ def backend(event_termination):
def main():
# app.run(host="0.0.0.0")
if fast_web_debugging:
try:
app.run(host="0.0.0.0")
except KeyboardInterrupt:
pass
return
thread = Thread(target=backend, args=(event_termination,))
thread.start()
# ugly solution for now
@ -249,7 +256,7 @@ def main():
thread.join()
except KeyboardInterrupt:
event_termination.set()
thread.join()
thread.join(1)
if __name__ == "__main__":

View File

@ -13,27 +13,101 @@
<body>
<div class="container">
<form>
<div class="mb-3">
<label for="apikey" class="form-label">API Key</label>
<input type="apikey" class="form-control" id="apikey" value="demo">
</div>
<div class="mb-3">
<label for="prompt" class="form-label">Prompt</label>
<input type="prompt" class="form-control" id="prompt" aria-describedby="promptHelp">
<div id="promptHelp" class="form-text">Less than 77 words otherwise it'll be truncated</div>
</div>
<div class="mb-3">
<label for="negprompt" class="form-label">Negative Prompt</label>
<input type="negprompt" class="form-control" id="negprompt" aria-describedby="negpromptHelp">
<div id="negpromptHelp" class="form-text">Less than 77 words otherwise it'll be truncated</div>
</div>
<button id="newjob" type="submit" class="btn btn-primary">Submit New Job</button>
</form>
<div class="card mb-3">
<div class="card-body" id="newjobresult"></div>
<img id="newjobresultimg" class="card-img-bottom" />
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Text-to-Image</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Image-to-Image</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Inpainting</a>
</li>
</ul>
</div>
<div class="card-body">
<form>
<div class="row mb-3">
<div class="col-sm-8">
<label for="apiKey" class="form-label">API Key</label>
<input type="password" class="form-control" id="apiKey" value="demo">
</div>
<div class="col-sm-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="showPreview" disabled>
<label class="form-check-label" for="showPreview">
Preview Image
</label>
</div>
</div>
</div>
<div class="form-row mb-3">
<label for="prompt" class="form-label">Prompt</label>
<input type="text" class="form-control" id="prompt" aria-describedby="promptHelp"
value="photo of cute cat, RAW photo, (high detailed skin:1.2), 8k uhd, dslr, soft lighting, high quality, film grain, Fujifilm XT3">
<div id="promptHelp" class="form-text">Less than 77 words otherwise it'll be truncated</div>
</div>
<div class="form-row mb-3">
<label for="negPrompt" class="form-label">Negative Prompt</label>
<input type="text" class="form-control" id="negPrompt" aria-describedby="negPromptHelp"
value="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck">
<div id="negPromptHelp" class="form-text">Less than 77 words otherwise it'll be truncated</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="form-row col-md-6">
<label for="inputSeed">Seed</label>
<input type="number" class="form-control" id="inputSeed"
aria-describedby="inputSeedHelp" value="">
<div id="inputSeedHelp" class="form-text">Leave it empty or put 0 to use a random
seed
</div>
</div>
<div class="form-row col-md-6">
<label for="inputSteps">Steps</label>
<input type="number" class="form-control" id="inputSteps"
aria-describedby="inputStepsHelp" placeholder="default is 20">
<div id="inputStepsHelp" class="form-text">Each step is about 38s (CPU) or 0.1s
(GPU)
</div>
</div>
</div>
<div class="row">
<div class="form-row col-md-6">
<label for="inputWidth">Width</label>
<input type="number" class="form-control" id="inputWidth" placeholder="512" min="1"
max="1024">
</div>
<div class="form-row col-md-6">
<label for="inputHeight">Height</label>
<input type="number" class="form-control" id="inputHeight" placeholder="512" min="1"
max="1024">
</div>
</div>
<button id="newJob" type="submit" class="btn btn-primary">Let's Go!</button>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">
Result
</div>
<div class="card-body">
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center"
id="resultStatus"></li>
<li class="list-group-item d-flex justify-content-between align-items-center"
id="resultSeed"></li>
</ul>
</div>
<img class="card-img-bottom" id="newJobImg">
</div>
</div>
</div>
</form>
</div>
</div>
<form>
@ -41,8 +115,8 @@
<label for="jobuuid" class="form-label">Job UUID</label>
<input type="jobuuid" class="form-control" id="jobuuid" aria-describedby="">
</div>
<button id="getjob" type="submit" class="btn btn-primary">Get Jobs</button>
<button id="canceljob" type="submit" class="btn btn-primary">Cancel Job</button>
<button id="getjob" type="submit" class="btn btn-primary" disabled>Get Jobs</button>
<button id="canceljob" type="submit" class="btn btn-primary" disabled>Cancel Job</button>
</form>
<div class="mb-3" id="joblist">
@ -57,31 +131,31 @@
<script>
function waitForImage(uuidValue) {
function waitForImage(apikeyVal, uuidValue) {
// Wait until image is done
$.ajax({
type: 'POST',
url: '/get_jobs',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify({ 'api_key': 'demo', 'uuid': uuidValue }),
data: JSON.stringify({ 'api_key': apikeyVal, 'uuid': uuidValue }),
success: function (response) {
console.log(response);
if (response.jobs.length == 1) {
$('#resultStatus').html(response.jobs[0].status)
$('#resultSeed').html(response.jobs[0].seed)
if (response.jobs[0].status == "done") {
$('#newjobresult').html('');
$('#newjobresultimg').attr('src', response.jobs[0].img);
$('#resultStatus').html(response.jobs[0].status);
$('#newJobImg').attr('src', response.jobs[0].img);
return;
} else {
$('#newjobresult').append("<p>current status: " + response.jobs[0].status + "</p>")
}
}
setTimeout(function () { waitForImage(uuidValue); }, 1000); // refresh every second
setTimeout(function () { waitForImage(apikeyVal, uuidValue); }, 1000); // refresh every second
},
error: function (xhr, status, error) {
// Handle error response
console.log(xhr.responseText);
$('#newjobresult').html('<p>failed to run, see console error for more details<p>');
$('#resultStatus').html('failed');
}
});
}
@ -102,13 +176,29 @@
}
});
$('#newjob').click(function (e) {
$('#newJob').click(function (e) {
e.preventDefault(); // Prevent the default form submission
// Gather input field values
var apikeyVal = $('#apikey').val();
var apikeyVal = $('#apiKey').val();
var promptVal = $('#prompt').val();
var negPromptVal = $('#negprompt').val();
var negPromptVal = $('#negPrompt').val();
var seedVal = parseInt($('#inputSeed').val());
if (isNaN(seedVal)) {
seedVal = 0;
}
var stepsVal = parseInt($('#inputSteps').val());
if (isNaN(stepsVal)) {
stepsVal = 20;
}
var widthVal = parseInt($('#inputWidth').val());
if (isNaN(widthVal)) {
widthVal = 512;
}
var heightVal = parseInt($('#inputHeight').val());
if (isNaN(heightVal)) {
heightVal = 512;
}
if (promptVal == "") {
alert("needs to write a prompt!");
@ -121,18 +211,27 @@
url: '/add_job',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify({ 'api_key': apikeyVal, 'prompt': promptVal, 'neg_prompt': negPromptVal }),
data: JSON.stringify({
'api_key': apikeyVal,
'prompt': promptVal,
'seed': seedVal,
'steps': stepsVal,
'width': widthVal,
'height': heightVal,
'neg_prompt': negPromptVal
}),
success: function (response) {
console.log(response);
if (response.uuid) {
$('#jobuuid').val(response.uuid);
}
$('#newjobresult').html('<p>waiting for result...<p>');
waitForImage(response.uuid);
$('#resultStatus').html('submitting new job..');
waitForImage(apikeyVal, response.uuid);
},
error: function (xhr, status, error) {
// Handle error response
console.log(xhr.responseText);
$('#resultStatus').html('failed');
}
});
});

View File

@ -54,7 +54,7 @@ class Config:
return self
def get_guidance_scale(self) -> float:
return self.__config.get(KEY_GUIDANCE_SCALE, VALUE_GUIDANCE_SCALE_DEFAULT)
return float(self.__config.get(KEY_GUIDANCE_SCALE, VALUE_GUIDANCE_SCALE_DEFAULT))
def set_guidance_scale(self, scale: float):
self.__logger.info("{} changed from {} to {}".format(KEY_GUIDANCE_SCALE, self.get_guidance_scale(), scale))
@ -62,7 +62,7 @@ class Config:
return self
def get_height(self) -> int:
return self.__config.get(KEY_HEIGHT, VALUE_HEIGHT_DEFAULT)
return int(self.__config.get(KEY_HEIGHT, VALUE_HEIGHT_DEFAULT))
def set_height(self, value: int):
self.__logger.info("{} changed from {} to {}".format(KEY_HEIGHT, self.get_height(), value))
@ -88,7 +88,7 @@ class Config:
return self
def get_seed(self) -> int:
seed = self.__config.get(KEY_SEED, VALUE_SEED_DEFAULT)
seed = int(self.__config.get(KEY_SEED, VALUE_SEED_DEFAULT))
if seed == 0:
random.seed(int(time.time_ns()))
seed = random.getrandbits(64)
@ -100,7 +100,7 @@ class Config:
return self
def get_steps(self) -> int:
return self.__config.get(KEY_STEPS, VALUE_STEPS_DEFAULT)
return int(self.__config.get(KEY_STEPS, VALUE_STEPS_DEFAULT))
def set_steps(self, steps: int):
self.__logger.info("{} changed from {} to {}".format(KEY_STEPS, self.get_steps(), steps))
@ -108,7 +108,7 @@ class Config:
return self
def get_width(self) -> int:
return self.__config.get(KEY_WIDTH, VALUE_WIDTH_DEFAULT)
return int(self.__config.get(KEY_WIDTH, VALUE_WIDTH_DEFAULT))
def set_width(self, value: int):
self.__logger.info("{} changed from {} to {}".format(KEY_WIDTH, self.get_width(), value))

View File

@ -31,6 +31,8 @@ class Model:
if use_gpu and torch.cuda.is_available():
self.__use_gpu = True
logger.info("running on {}".format(torch.cuda.get_device_name("cuda:0")))
else:
logger.info("running on CPU (expect it to be verrry sloooow)")
self.__logger = logger
self.__torch_dtype = torch.float64