Skip to content

Commit

Permalink
feat: Store Images
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarMWarraich committed Jul 23, 2024
1 parent e7397c5 commit 765cdbd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions app/controllers/txt2_imgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions app/jobs/sd_render_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
34 changes: 34 additions & 0 deletions app/models/generated_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/generated_images/_display_main_image.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= image_tag generated_image.sketch, alt: "#{generated_image.info}", class: "img-fluid" %>
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 765cdbd

Please sign in to comment.