diff --git a/Gemfile b/Gemfile index bb8d2ef..42e2751 100644 --- a/Gemfile +++ b/Gemfile @@ -77,3 +77,5 @@ gem 'font-awesome-sass', '~> 5.15.1' gem 'ruby-stableDiffusion', path: '/home/owa/Docs/ror/ruby-stablediffusion/' gem "sidekiq" + +gem "ruby-vips", "~> 2.2" diff --git a/Gemfile.lock b/Gemfile.lock index ee64089..ac42444 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -248,6 +248,9 @@ GEM io-console (~> 0.5) rexml (3.3.0) strscan + ruby-vips (2.2.2) + ffi (~> 1.12) + logger rubyzip (2.3.2) sassc (2.4.0) ffi (~> 1.9) @@ -329,6 +332,7 @@ DEPENDENCIES rails (~> 7.1.3, >= 7.1.3.4) redis (>= 4.0.1) ruby-stableDiffusion! + ruby-vips (~> 2.2) sassc-rails (~> 2.1) selenium-webdriver sidekiq diff --git a/app/controllers/txt2_imgs_controller.rb b/app/controllers/txt2_imgs_controller.rb index 7510911..965ac7d 100644 --- a/app/controllers/txt2_imgs_controller.rb +++ b/app/controllers/txt2_imgs_controller.rb @@ -37,6 +37,7 @@ def create sd_settings = { original_prompt: image_create_params[:prompt], + style_template: image_create_params[:style_template], prompt: merged_prompt, negative_prompt: merged_negative_prompt, sampler_name: 'DPM++ 2M Karras', @@ -55,13 +56,6 @@ def create render turbo_stream: turbo_stream.replace( 'process_starting', partial: '/txt2_imgs/process_starting' - ) + - turbo_stream.replace( - 'image_maker', - partial: '/txt2_imgs/image_maker', - locals: { - render_result: render_result, - } ) end end diff --git a/app/jobs/sd_render_job.rb b/app/jobs/sd_render_job.rb index f3c4dec..d6ed1ca 100644 --- a/app/jobs/sd_render_job.rb +++ b/app/jobs/sd_render_job.rb @@ -2,8 +2,6 @@ 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'] @@ -19,9 +17,12 @@ def perform(render_settings, user_id) render_settings[:task_id] = our_task_id render_settings[:id_task] = our_task_id - ImgProgressJob.perform_later(our_task_id, user_id, original_prompt, style_template) + ImgProgressJob.perform_later(our_task_id, user_id, render_settings[:original_prompt], render_settings[:style_template]) - result = api_instance.text2imgapi_sdapi_v1_txt2img_post(render_settings) + result = api_instance.text2imgapi_sdapi_v1_txt2img_post(render_settings.except(:original_prompt, :style_template)) + GeneratedImage.create_from_sd_render_job(render_settings, user_id, result) + + result end end diff --git a/app/models/generated_image.rb b/app/models/generated_image.rb index 212cbf6..b7aa451 100644 --- a/app/models/generated_image.rb +++ b/app/models/generated_image.rb @@ -21,6 +21,40 @@ # fk_rails_... (user_id => users.id) # class GeneratedImage < ApplicationRecord + include ActionView::RecordIdentifier + belongs_to :user has_one_attached :sketch + + after_create_commit { broadcast_created } + + def broadcast_created + broadcast_update_to( + "#{dom_id(user)}_main_image", + partial: "generated_images/display_main_image", + locals: { + scroll_to: true, + generated_image: self + }, + target: "image_maker" + ) + end + + def self.create_from_sd_render_job(render_settings, user_id, result) + user = User.find(user_id) + result.images.each do |src_image| + generated_image = user.generated_images.create( + prompt: render_settings[:original_prompt], + negative_prompt: render_settings[:negative_prompt], + style_template: render_settings[:style_template], + parameters: result.parameters, + info: result.info, + sketch: { + io: StringIO.new(Base64.decode64(src_image)), + content_type: 'image/png', + filename: "#{render_settings[:original_prompt]}-#{SecureRandom.uuid}.png".last(256) + } + ) + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index 55512d6..890c795 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -14,6 +14,8 @@ class User < ApplicationRecord normalizes :email, with: ->(email) { email.strip.downcase } + has_many :generated_images, dependent: :destroy + generates_token_for :password_reset, expires_in: 10.minutes do password_salt&.last(10) end diff --git a/app/views/generated_images/_display_main_image.html.erb b/app/views/generated_images/_display_main_image.html.erb new file mode 100644 index 0000000..267ca44 --- /dev/null +++ b/app/views/generated_images/_display_main_image.html.erb @@ -0,0 +1 @@ +<%= image_tag generated_image.sketch, alt: "#{generated_image.info}", class: "img-fluid" %> \ No newline at end of file diff --git a/config/environments/development.rb b/config/environments/development.rb index 532181f..481b75e 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -89,4 +89,4 @@ config.action_mailer.raise_delivery_errors = true end -Rails.application.default_url_options = { host: "localhost", port: 3000 } +Rails.application.routes.default_url_options = { host: "localhost", port: 3000 }