-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from OmarMWarraich/15-save-images-and-write-to-db
15 save images and write to db
- Loading branch information
Showing
18 changed files
with
370 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# == Schema Information | ||
# | ||
# Table name: generated_images | ||
# | ||
# id :bigint not null, primary key | ||
# info :json | ||
# negative_prompt :string | ||
# parameters :json | ||
# prompt :string | ||
# style_template :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# user_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_generated_images_on_user_id (user_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
db/migrate/20240701214248_create_active_storage_tables.active_storage.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# This migration comes from active_storage (originally 20170806125915) | ||
class CreateActiveStorageTables < ActiveRecord::Migration[7.0] | ||
def change | ||
# Use Active Record's configured type for primary and foreign keys | ||
primary_key_type, foreign_key_type = primary_and_foreign_key_types | ||
|
||
create_table :active_storage_blobs, id: primary_key_type do |t| | ||
t.string :key, null: false | ||
t.string :filename, null: false | ||
t.string :content_type | ||
t.text :metadata | ||
t.string :service_name, null: false | ||
t.bigint :byte_size, null: false | ||
t.string :checksum | ||
|
||
if connection.supports_datetime_with_precision? | ||
t.datetime :created_at, precision: 6, null: false | ||
else | ||
t.datetime :created_at, null: false | ||
end | ||
|
||
t.index [ :key ], unique: true | ||
end | ||
|
||
create_table :active_storage_attachments, id: primary_key_type do |t| | ||
t.string :name, null: false | ||
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type | ||
t.references :blob, null: false, type: foreign_key_type | ||
|
||
if connection.supports_datetime_with_precision? | ||
t.datetime :created_at, precision: 6, null: false | ||
else | ||
t.datetime :created_at, null: false | ||
end | ||
|
||
t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
|
||
create_table :active_storage_variant_records, id: primary_key_type do |t| | ||
t.belongs_to :blob, null: false, index: false, type: foreign_key_type | ||
t.string :variation_digest, null: false | ||
|
||
t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true | ||
t.foreign_key :active_storage_blobs, column: :blob_id | ||
end | ||
end | ||
|
||
private | ||
def primary_and_foreign_key_types | ||
config = Rails.configuration.generators | ||
setting = config.options[config.orm][:primary_key_type] | ||
primary_key_type = setting || :primary_key | ||
foreign_key_type = setting || :bigint | ||
[primary_key_type, foreign_key_type] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateGeneratedImages < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :generated_images do |t| | ||
t.string :prompt | ||
t.string :negative_prompt | ||
t.string :style_template | ||
t.references :user, null: false, foreign_key: true | ||
t.json :info | ||
t.json :parameters | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# NOTE: only doing this in development as some production environments (Heroku) | ||
# NOTE: are sensitive to local FS writes, and besides -- it's just not proper | ||
# NOTE: to have a dev-mode tool do its thing in production. | ||
if Rails.env.development? | ||
require 'annotate' | ||
task :set_annotation_options do | ||
# You can override any of these by setting an environment variable of the | ||
# same name. | ||
Annotate.set_defaults( | ||
'active_admin' => 'false', | ||
'additional_file_patterns' => [], | ||
'routes' => 'false', | ||
'models' => 'true', | ||
'position_in_routes' => 'before', | ||
'position_in_class' => 'before', | ||
'position_in_test' => 'before', | ||
'position_in_fixture' => 'before', | ||
'position_in_factory' => 'before', | ||
'position_in_serializer' => 'before', | ||
'show_foreign_keys' => 'true', | ||
'show_complete_foreign_keys' => 'false', | ||
'show_indexes' => 'true', | ||
'simple_indexes' => 'false', | ||
'model_dir' => 'app/models', | ||
'root_dir' => '', | ||
'include_version' => 'false', | ||
'require' => '', | ||
'exclude_tests' => 'false', | ||
'exclude_fixtures' => 'false', | ||
'exclude_factories' => 'false', | ||
'exclude_serializers' => 'false', | ||
'exclude_scaffolds' => 'true', | ||
'exclude_controllers' => 'true', | ||
'exclude_helpers' => 'true', | ||
'exclude_sti_subclasses' => 'false', | ||
'ignore_model_sub_dir' => 'false', | ||
'ignore_columns' => nil, | ||
'ignore_routes' => nil, | ||
'ignore_unknown_models' => 'false', | ||
'hide_limit_column_types' => 'integer,bigint,boolean', | ||
'hide_default_column_types' => 'json,jsonb,hstore', | ||
'skip_on_db_migrate' => 'false', | ||
'format_bare' => 'true', | ||
'format_rdoc' => 'false', | ||
'format_yard' => 'false', | ||
'format_markdown' => 'false', | ||
'sort' => 'false', | ||
'force' => 'false', | ||
'frozen' => 'false', | ||
'classified_sort' => 'true', | ||
'trace' => 'false', | ||
'wrapper_open' => nil, | ||
'wrapper_close' => nil, | ||
'with_comment' => 'true' | ||
) | ||
end | ||
|
||
Annotate.load_tasks | ||
end |
Oops, something went wrong.