2023-09-14 11:53:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiHelper
|
|
|
|
class Painter
|
2024-01-05 12:03:23 -05:00
|
|
|
def commission_thumbnails(input, user)
|
|
|
|
return [] if input.blank?
|
2023-09-14 11:53:44 -04:00
|
|
|
|
2024-01-05 12:03:23 -05:00
|
|
|
model = SiteSetting.ai_helper_illustrate_post_model
|
2023-09-14 11:53:44 -04:00
|
|
|
|
2024-01-05 12:03:23 -05:00
|
|
|
if model == "stable_diffusion_xl"
|
2024-01-19 06:51:26 -05:00
|
|
|
stable_diffusion_prompt = diffusion_prompt(input, user)
|
2024-01-05 12:03:23 -05:00
|
|
|
return [] if stable_diffusion_prompt.blank?
|
2023-09-14 11:53:44 -04:00
|
|
|
|
2024-01-05 12:03:23 -05:00
|
|
|
artifacts =
|
|
|
|
DiscourseAi::Inference::StabilityGenerator
|
|
|
|
.perform!(stable_diffusion_prompt)
|
|
|
|
.dig(:artifacts)
|
|
|
|
.to_a
|
|
|
|
.map { |art| art[:base64] }
|
|
|
|
|
|
|
|
base64_to_image(artifacts, user.id)
|
|
|
|
elsif model == "dall_e_3"
|
|
|
|
api_key = SiteSetting.ai_openai_api_key
|
|
|
|
api_url = SiteSetting.ai_openai_dall_e_3_url
|
|
|
|
|
|
|
|
artifacts =
|
|
|
|
DiscourseAi::Inference::OpenAiImageGenerator
|
|
|
|
.perform!(input, api_key: api_key, api_url: api_url)
|
|
|
|
.dig(:data)
|
|
|
|
.to_a
|
|
|
|
.map { |art| art[:b64_json] }
|
|
|
|
|
|
|
|
base64_to_image(artifacts, user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def base64_to_image(artifacts, user_id)
|
|
|
|
attribution =
|
|
|
|
I18n.t(
|
|
|
|
"discourse_ai.ai_helper.painter.attribution.#{SiteSetting.ai_helper_illustrate_post_model}",
|
|
|
|
)
|
|
|
|
|
|
|
|
artifacts.each_with_index.map do |art, i|
|
2023-09-14 11:53:44 -04:00
|
|
|
f = Tempfile.new("v1_txt2img_#{i}.png")
|
|
|
|
f.binmode
|
2024-01-05 12:03:23 -05:00
|
|
|
f.write(Base64.decode64(art))
|
2023-09-14 11:53:44 -04:00
|
|
|
f.rewind
|
2024-01-05 12:03:23 -05:00
|
|
|
upload = UploadCreator.new(f, attribution).create_for(user_id)
|
2023-09-14 11:53:44 -04:00
|
|
|
f.unlink
|
|
|
|
|
2023-12-19 14:17:34 -05:00
|
|
|
UploadSerializer.new(upload, root: false)
|
2023-09-14 11:53:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-19 06:51:26 -05:00
|
|
|
def diffusion_prompt(text, user)
|
2024-01-12 12:36:44 -05:00
|
|
|
prompt =
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
<<~TEXT.strip,
|
2023-11-27 07:33:31 -05:00
|
|
|
Provide me a StableDiffusion prompt to generate an image that illustrates the following post in 40 words or less, be creative.
|
|
|
|
You'll find the post between <input></input> XML tags.
|
|
|
|
TEXT
|
2024-01-12 12:36:44 -05:00
|
|
|
messages: [{ type: :user, content: text, id: user.username }],
|
|
|
|
)
|
2023-09-14 11:53:44 -04:00
|
|
|
|
2024-01-04 07:53:47 -05:00
|
|
|
DiscourseAi::Completions::Llm.proxy(SiteSetting.ai_helper_model).generate(
|
|
|
|
prompt,
|
|
|
|
user: user,
|
2024-05-13 23:28:46 -04:00
|
|
|
feature_name: "illustrate_post",
|
2024-01-04 07:53:47 -05:00
|
|
|
)
|
2023-09-14 11:53:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|