Skip to content

Commit

Permalink
feat: Log SD Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarMWarraich committed Jul 1, 2024
1 parent 9a4c69a commit d4b5189
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
36 changes: 29 additions & 7 deletions app/controllers/txt2_imgs_controller.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 34 additions & 4 deletions app/views/txt2_imgs/_settings.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<%= form_with(model: ImageMaker.new, url: txt2_imgs_path, method: :post) do |form|%>
<div class='row mb-2'>
<div class='col-2'>
<%= form.label :style_template, "Style", class: "form-label" %>
<strong>
<%= form.label :style_template, "Style", class: "form-label" %>
</strong>
</div>
<div class='col'>
<%= form.select :style_template, @styles.map { |s| [s[:name].titleize, s[:name]] }.sort, { include_blank: true }, {class: "form-select w-100"} %>
Expand All @@ -10,7 +12,9 @@

<div class='row mb-2'>
<div class='col-2'>
<%= form.label :sd_model, "Model", class: "form-label" %>
<strong>
<%= form.label :sd_model, "Model", class: "form-label" %>
</strong>
</div>
<div class='col'>
<%= form.select :sd_model, @models.sort, {}, {class: "form-select w-100"} %>
Expand All @@ -27,7 +31,7 @@

<div class='row mb-2'>
<div class='col'>
<div class="btn-group w-100 d-flex" role="group" aria-label="Basic radio toggle button group">
<div class="btn-group w-100 d-flex" role="group" aria-label="Choose aspect ratio">
<%= form.radio_button :aspect_ratio, "3:2", class: 'btn-check' %>
<%= form.label :aspect_ratio_32, class: "btn btn-outline-default" do %>
<i class='fas fa-regular fa-rectangle-wide'></i> 3:2
Expand All @@ -51,7 +55,9 @@
<div class="container" data-controller="random-seed toggle">
<div class="row mb-2">
<div class="col-3 pt-1">
<%= form.label :seed, "Seed", class: "form-label" %>
<strong>
<%= form.label :seed, "Seed", class: "form-label" %>
</strong>
</div>
<div class="col-9">
<%= form.number_field :seed, in: -1..9999999999, class: "form-control", data: { random_seed_target: "inputField", toggle_target: "inputField" } %>
Expand All @@ -70,4 +76,28 @@
</div>
</div>

<div class="mb-2 row">
<div class="col-1 pt-1">
<strong>
Q
</strong>
</div>
<div class="col">
<div class="btn-group w-100 d-flex" role="group" aria-label="Set image quality">
<%= 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 %>
</div>
</div>
</div>

<% end %>

0 comments on commit d4b5189

Please sign in to comment.