From d4b518962a42d0978427ac8d1299e9ae36928bea Mon Sep 17 00:00:00 2001 From: OmarMWarraich Date: Mon, 1 Jul 2024 19:53:33 +0500 Subject: [PATCH 1/2] feat: Log SD Settings --- app/controllers/txt2_imgs_controller.rb | 36 ++++++++++++++++++----- app/views/txt2_imgs/_settings.html.erb | 38 ++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/app/controllers/txt2_imgs_controller.rb b/app/controllers/txt2_imgs_controller.rb index 5e6d0d3..150e230 100644 --- a/app/controllers/txt2_imgs_controller.rb +++ b/app/controllers/txt2_imgs_controller.rb @@ -1,35 +1,57 @@ class Txt2ImgsController < ApplicationController + before_action :authenticate_user! + def index @styles = Styles.load - api_instance = RStableDiffusionAI::DefaultApi.new + config = RStableDiffusionAI::Configuration.new + config.host = ENV['SD_API_HOST'] + config.scheme = ENV['SD_API_SCHEME'] + config.debugging = true + + client = RStableDiffusionAI::ApiClient.new(config) + + api_instance = RStableDiffusionAI::DefaultApi.new(client) + result = api_instance.get_sd_models_sdapi_v1_sd_models_get @models = result.map { |model| model.model_name } end def create - style = Styles.find_by_name(params[:style_template]) - style = { name: '++ None', prompt: '{prompt}', negative_prompt: '' } if style.nil? + style = Styles.find_by_name(image_create_params[:style_template]) + style ||= { name: 'default', prompt: '{prompt}', negative_prompt: '' } - prompt = params[:prompt] + prompt = image_create_params[:prompt] merged_prompt = style[:prompt].gsub('{prompt}', prompt) - merged_negative_prompt = [style[:negative_prompt], image_maker_params[:negative_prompt]].compact.join(', ') + merged_negative_prompt = [style[:negative_prompt], image_create_params[:negative_prompt]].compact.join(', ') + + aspect_ratios = { + '3:2' => { height: 682, width: 1024 }, + '1:1' => { height: 1024, width: 1024 }, + '2:3' => { height: 1024, width: 682 } + } + + height, width = aspect_ratios.fetch(image_create_params[:aspect_ratio], { height: 1024, width: 1024 }).values_at(:height, + :width) sd_settings = { original_prompt: image_create_params[:prompt], prompt: merged_prompt, negative_prompt: merged_negative_prompt, sampler_name: 'DPM++ 2M Karras', + width: width, + height: height, override_settings: { sd_model_checkpoint: image_create_params.delete(:sd_model) } }.deep_symbolize_keys - new_settings = image_create_params.to_h.deep_symbolize_keys.merge(sd_settings) + Rails.logger.info("sd_settings: #{sd_settings}") + # new_settings = image_create_params.to_h.deep_symbolize_keys.merge(sd_settings) end private def image_create_params - params.require(:image_maker).permit(:prompt, :style_template) + params.require(:image_maker).permit(:style_template, :prompt, :negative_prompt, :sd_model, :aspect_ratio) end end diff --git a/app/views/txt2_imgs/_settings.html.erb b/app/views/txt2_imgs/_settings.html.erb index 28e28d0..4419bb2 100644 --- a/app/views/txt2_imgs/_settings.html.erb +++ b/app/views/txt2_imgs/_settings.html.erb @@ -1,7 +1,9 @@ <%= form_with(model: ImageMaker.new, url: txt2_imgs_path, method: :post) do |form|%>
- <%= form.label :style_template, "Style", class: "form-label" %> + + <%= form.label :style_template, "Style", class: "form-label" %> +
<%= form.select :style_template, @styles.map { |s| [s[:name].titleize, s[:name]] }.sort, { include_blank: true }, {class: "form-select w-100"} %> @@ -10,7 +12,9 @@
- <%= form.label :sd_model, "Model", class: "form-label" %> + + <%= form.label :sd_model, "Model", class: "form-label" %> +
<%= form.select :sd_model, @models.sort, {}, {class: "form-select w-100"} %> @@ -27,7 +31,7 @@
-
+
<%= form.radio_button :aspect_ratio, "3:2", class: 'btn-check' %> <%= form.label :aspect_ratio_32, class: "btn btn-outline-default" do %> 3:2 @@ -51,7 +55,9 @@
- <%= form.label :seed, "Seed", class: "form-label" %> + + <%= form.label :seed, "Seed", class: "form-label" %> +
<%= form.number_field :seed, in: -1..9999999999, class: "form-control", data: { random_seed_target: "inputField", toggle_target: "inputField" } %> @@ -70,4 +76,28 @@
+
+
+ + Q + +
+
+
+ <%= form.radio_button :steps, "14", class: 'btn-check' %> + <%= form.label :steps_14, class: "btn btn-outline-default" do %> + Fast + <% end %> + <%= form.radio_button :steps, "32", class: 'btn-check', checked: true %> + <%= form.label :steps_32, class: "btn btn-outline-default" do %> + Medium + <% end %> + <%= form.radio_button :steps, "50", class: 'btn-check' %> + <%= form.label :steps_50, class: "btn btn-outline-default" do %> + High + <% end %> +
+
+
+ <% end %> From 777c4894d370e3e4710488d895d1ec05cc712d5b Mon Sep 17 00:00:00 2001 From: OmarMWarraich Date: Mon, 1 Jul 2024 20:49:14 +0500 Subject: [PATCH 2/2] feat: Render Job, Turbo Frame Image --- app/controllers/txt2_imgs_controller.rb | 16 +++++++++++++++- app/jobs/sd_render_job.rb | 20 ++++++++++++++++++++ app/views/txt2_imgs/_image_maker.html.erb | 3 +++ app/views/txt2_imgs/index.html.erb | 9 +++++++-- test/jobs/sd_render_job_test.rb | 7 +++++++ 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 app/jobs/sd_render_job.rb create mode 100644 app/views/txt2_imgs/_image_maker.html.erb create mode 100644 test/jobs/sd_render_job_test.rb diff --git a/app/controllers/txt2_imgs_controller.rb b/app/controllers/txt2_imgs_controller.rb index 150e230..695b3fd 100644 --- a/app/controllers/txt2_imgs_controller.rb +++ b/app/controllers/txt2_imgs_controller.rb @@ -42,16 +42,30 @@ def create sampler_name: 'DPM++ 2M Karras', width: width, height: height, + steps: image_create_params[:steps], override_settings: { sd_model_checkpoint: image_create_params.delete(:sd_model) } }.deep_symbolize_keys Rails.logger.info("sd_settings: #{sd_settings}") # new_settings = image_create_params.to_h.deep_symbolize_keys.merge(sd_settings) + render_result = SdRenderJob.perform_now(sd_settings, current_user.id) + + respond_to do |format| + format.turbo_stream do + render turbo_stream: turbo_stream.replace( + 'image_maker', + partial: 'txt2_imgs/image_maker', + locals: { + render_result: render_result + } + ) + end + end end private def image_create_params - params.require(:image_maker).permit(:style_template, :prompt, :negative_prompt, :sd_model, :aspect_ratio) + params.require(:image_maker).permit(:style_template, :prompt, :negative_prompt, :sd_model, :aspect_ratio, :steps) end end diff --git a/app/jobs/sd_render_job.rb b/app/jobs/sd_render_job.rb new file mode 100644 index 0000000..168142f --- /dev/null +++ b/app/jobs/sd_render_job.rb @@ -0,0 +1,20 @@ +class SdRenderJob < ApplicationJob + queue_as :default + + def perform(render_settings, user_id) + original_prompt = render_settings.delete(:original_prompt) + style_template = render_settings.delete(:style_template) + + config = RStableDiffusionAI::Configuration.new + config.host = ENV['SD_API_HOST'] + config.scheme = ENV['SD_API_SCHEME'] + config.debugging = true + + client = RStableDiffusionAI::ApiClient.new(config) + + api_instance = RStableDiffusionAI::DefaultApi.new(client) + + result = api_instance.text2imgapi_sdapi_v1_txt2img_post(render_settings) + + end +end diff --git a/app/views/txt2_imgs/_image_maker.html.erb b/app/views/txt2_imgs/_image_maker.html.erb new file mode 100644 index 0000000..1c27b89 --- /dev/null +++ b/app/views/txt2_imgs/_image_maker.html.erb @@ -0,0 +1,3 @@ + + <%= image_tag "data:image/png;base64,#{render_result.images[0]}", alt: "#{render_result.info}", class: "img-fluid thumbnail" %> + \ No newline at end of file diff --git a/app/views/txt2_imgs/index.html.erb b/app/views/txt2_imgs/index.html.erb index dba9a5d..e43038f 100644 --- a/app/views/txt2_imgs/index.html.erb +++ b/app/views/txt2_imgs/index.html.erb @@ -6,8 +6,13 @@
-
- +
+ <%# turbo_stream_from "#{dom_id(current_user)}_main_image" %> + + + This will be replaced by the image maker + +
diff --git a/test/jobs/sd_render_job_test.rb b/test/jobs/sd_render_job_test.rb new file mode 100644 index 0000000..916ea7a --- /dev/null +++ b/test/jobs/sd_render_job_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class SdRenderJobTest < ActiveJob::TestCase + # test "the truth" do + # assert true + # end +end