Skip to content

Commit

Permalink
feat: Render Job, Turbo Frame Image
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarMWarraich committed Jul 1, 2024
1 parent d4b5189 commit 777c489
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
16 changes: 15 additions & 1 deletion app/controllers/txt2_imgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions app/jobs/sd_render_job.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/views/txt2_imgs/_image_maker.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<turbo-frame id="image_maker" action="update">
<%= image_tag "data:image/png;base64,#{render_result.images[0]}", alt: "#{render_result.info}", class: "img-fluid thumbnail" %>
</turbo-frame>
9 changes: 7 additions & 2 deletions app/views/txt2_imgs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
</div>
</div>
<div class='col-md-7'>
<div id='image_holder' class='text-center card h-100 p-2 bg-dark'>

<div id='image_holder' class='text-center card h-100 p-2'>
<%# turbo_stream_from "#{dom_id(current_user)}_main_image" %>
<turbo-frame id="full_image_area">
<turbo-frame id="image_maker">
This will be replaced by the image maker
</turbo-frame>
</turbo-frame>
</div>
</div>
<div class='col-md-2'>
Expand Down
7 changes: 7 additions & 0 deletions test/jobs/sd_render_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class SdRenderJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 777c489

Please sign in to comment.