Skip to content

Commit

Permalink
feat: Progress Holder, TurboStream, Loading, Preview and Image Genera…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
OmarMWarraich committed Jul 1, 2024
1 parent 94e16b5 commit 1743307
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ group :development do

# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
gem "annotate"
gem "letter_opener"
end

group :test do
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
autoprefixer-rails (10.4.16.0)
execjs (~> 2)
base64 (0.2.0)
Expand All @@ -105,6 +108,7 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (5.0.0)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
crass (1.0.6)
Expand Down Expand Up @@ -145,6 +149,11 @@ GEM
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.7.2)
launchy (3.0.1)
addressable (~> 2.8)
childprocess (~> 5.0)
letter_opener (1.10.0)
launchy (>= 2.2, < 4)
logger (1.6.0)
loofah (2.22.0)
crass (~> 1.0.2)
Expand Down Expand Up @@ -304,6 +313,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
annotate
bcrypt (~> 3.1.7)
bootsnap
bootstrap (~> 5.3)
Expand All @@ -313,6 +323,7 @@ DEPENDENCIES
font-awesome-sass (~> 5.15.1)
importmap-rails
jbuilder
letter_opener
pg (~> 1.1)
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.4)
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/txt2_imgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ def create
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace(
'process_starting',
partial: '/txt2_imgs/process_starting'
) +
turbo_stream.replace(
'image_maker',
partial: 'txt2_imgs/image_maker',
partial: '/txt2_imgs/image_maker',
locals: {
render_result: render_result
render_result: render_result,
}
)
end
Expand Down
8 changes: 8 additions & 0 deletions app/jobs/img_progress_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ def perform(task_id, user_id, original_prompt, style_template)

api_instance = RStableDiffusionAI::DefaultApi.new(client)

start_time = Time.now

loop do
sleep 5
result = api_instance.progressapi_internal_progress_post(payload)

Rails.logger.debug "Progress: #{result}"
break if result.completed == true

Rails.logger.debug "Time.now - start_time: #{Time.now - start_time}"

break if Time.now - start_time > 240

ProgressHolder.new.broadcast_updated(user_id, task_id, original_prompt, style_template, result)
end
end
end
38 changes: 38 additions & 0 deletions app/models/progress_holder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# == Schema Information
#
# Table name: progress_holders
#
# id :bigint not null, primary key
# task_ref :string
# live_preview_id :integer
# user_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
#
class ProgressHolder < ApplicationRecord
include ActionView::RecordIdentifier
belongs_to :user

def broadcast_updated(user_id, task_ref, original_prompt, style_template, result)
progress_holder = ProgressHolder.find_or_create_by(task_ref:) do |ph|
ph.user_id = user_id
ph.live_preview_id = result.progress.positive? ? result.id_live_preview : -1
end

user = User.find(user_id)

# Broadcast the updated progress to the user
broadcast_update_to(
"#{dom_id(user)}_main_image",
partial: "txt2_imgs/loading_progress",
locals: {
scroll_to: true,
original_prompt:,
style_template:,
result: JSON.parse(result.to_json)
},
target: "image_maker"
)

end
end
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :string
# password_digest :string
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ApplicationRecord
has_secure_password
validates :email, presence: true, uniqueness: true
Expand Down
22 changes: 22 additions & 0 deletions app/views/txt2_imgs/_loading_progress.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<turbo-frame id="image_maker">
<div class="container">
<div class="spinner-border mt-3" style="width: 5rem; height: 5rem;" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<br>
<div class="m-3 p-3">
<% progress = result['progress'].present? ? (result['progress'].to_f * 100) : 0 %>
<div class="progress" role="progressbar" aria-label="Basic example" aria-valuenow="<%=progress%>" aria-valuemin="0" aria-valuemax="100">
<div class="progress-bar" style="width: <%=progress%>%"></div>
</div>
</div>
<div class="m-3 p-3">
<p>ETA: <%= result['eta'].to_f %> seconds</p>
</div>
<div class="m-3 p-3">
<% if result['live_preview'].present? && !result['completed'] != 'true' %>
<%= image_tag "#{result['live_preview']}", alt: "#{original_prompt}", class: 'img-fluid thumbnail' %>
<% end %>
</div>
</div>
</turbo-frame>
3 changes: 3 additions & 0 deletions app/views/txt2_imgs/_process_starting.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<turbo-frame class="image-maker" action="update">
We will make your image in a moment. Please wait...
</turbo-frame>
2 changes: 1 addition & 1 deletion app/views/txt2_imgs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</div>
<div class='col-md-7'>
<div id='image_holder' class='text-center card h-100 p-2'>
<%# turbo_stream_from "#{dom_id(current_user)}_main_image" %>
<%= 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
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20240701172911_create_progress_holders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateProgressHolders < ActiveRecord::Migration[7.1]
def change
create_table :progress_holders do |t|
t.string :task_ref
t.integer :live_preview_id
t.references :user, null: false, foreign_key: true

t.timestamps
end
add_index :progress_holders, :task_ref, unique: true
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/fixtures/progress_holders.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
task_ref: MyString
live_preview_id: 1
user: one

two:
task_ref: MyString
live_preview_id: 1
user: two
18 changes: 18 additions & 0 deletions test/models/progress_holder_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: progress_holders
#
# id :bigint not null, primary key
# task_ref :string
# live_preview_id :integer
# user_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
#
require "test_helper"

class ProgressHolderTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
10 changes: 10 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :string
# password_digest :string
# created_at :datetime not null
# updated_at :datetime not null
#
require "test_helper"

class UserTest < ActiveSupport::TestCase
Expand Down

0 comments on commit 1743307

Please sign in to comment.