Skip to content

Commit

Permalink
feat: allow user to upload image while chatting
Browse files Browse the repository at this point in the history
  • Loading branch information
al3rez committed Jan 10, 2024
1 parent 2ec5905 commit 8186a77
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ gem "tzinfo-data", platforms: %i[windows jruby]
gem "bootsnap", require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
gem "image_processing", "~> 1.2"
gem "heroicon", "~> 0.4.0"

group :development, :test do
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,17 @@ GEM
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.0.2)
ffi (1.16.3)
globalid (1.2.1)
activesupport (>= 6.1)
heroicon (0.4.0)
rails (>= 5.2)
htmlbeautifier (1.4.2)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
importmap-rails (1.2.3)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
Expand All @@ -144,6 +148,7 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.20.0)
msgpack (1.7.2)
Expand Down Expand Up @@ -269,6 +274,8 @@ GEM
faraday (>= 1)
faraday-multipart (>= 1)
ruby-progressbar (1.13.0)
ruby-vips (2.2.0)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
selenium-webdriver (4.15.0)
Expand Down Expand Up @@ -348,6 +355,7 @@ DEPENDENCIES
debug
heroicon (~> 0.4.0)
htmlbeautifier
image_processing (~> 1.2)
importmap-rails
pg (~> 1.1)
pgreset
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/chats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ChatsController < ApplicationController
before_action :set_chat

def create
note = @chat.notes.create!(content: params[:content])
note = @chat.notes.create!(content: params[:content], image: params[:image])
note.send_to_openai!
redirect_to chat_path(@chat)
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Note < ApplicationRecord

validates :content, presence: true

has_one_attached :image

broadcasts_refreshes

def send_to_openai!
Expand Down
1 change: 1 addition & 0 deletions app/views/shared/_chat_messages.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<img src="https://i.pravatar.cc/57" class="max-w-min rounded" alt='Open AI logo' />
</div>
<p><%= note.content %></p>
<p><%= image_tag(note.image) if note.image.present? %></p>
</div>
<%= heroicon "pencil-suqare" , options: { class: 'h-6 w-6' } %>
</div>
Expand Down
3 changes: 2 additions & 1 deletion app/views/shared/_message_input.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

<div class='flex bottom-0 inset-x-0 mx-auto w-full px-4 py-6 justify-center items-center bg-[#343541]'>
<%= form_with(url: chats_path, method: :post, data: { remote: "true" }, class: "border border-gray-300 flex justify-center items-center space-x-2 shadow-md rounded px-2 w-1/2") do |f| %>
<input name="content" class='flex-1 bg-[#343541] text-white p-2 border-0 focus:outline-none' />
<%= f.file_field :image %>
<%= f.text_field :content, class: "flex-1 bg-[#343541] text-white p-2 border-0 focus:outline-none" %>
<button type="submit">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="text-black cursor-pointer bg-white rounded dark:text-black"><path d="M7 11L12 6L17 11M12 18V7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path></svg>
</button>
Expand Down
2 changes: 0 additions & 2 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,3 @@

# Allow puma to be restarted by `bin/rails restart` command.
plugin :tmp_restart

plugin :solid_queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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
32 changes: 31 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,38 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_01_07_233601) do
ActiveRecord::Schema[7.1].define(version: 2024_01_10_111346) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", force: :cascade 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"
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "active_storage_variant_records", force: :cascade do |t|
t.bigint "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "assistants", force: :cascade do |t|
t.bigint "user_id", null: false
t.string "model"
Expand Down Expand Up @@ -267,6 +295,8 @@
t.string "openai_key"
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "assistants", "users"
add_foreign_key "chats", "users"
add_foreign_key "conversations", "assistants"
Expand Down

0 comments on commit 8186a77

Please sign in to comment.