mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-10-27 12:38:45 +00:00
Previous to this change image generation did not work on multisite There was a background thread generating the images and it was getting site settings from the default site in the cluster This also removes referer header which is not needed
63 lines
1.6 KiB
Ruby
63 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
module Inference
|
|
class StabilityGenerator
|
|
def self.perform!(prompt, width: nil, height: nil, api_key: nil, engine: nil, api_url: nil)
|
|
api_key ||= SiteSetting.ai_stability_api_key
|
|
engine ||= SiteSetting.ai_stability_engine
|
|
api_url ||= SiteSetting.ai_stability_api_url
|
|
|
|
headers = {
|
|
"Content-Type" => "application/json",
|
|
"Accept" => "application/json",
|
|
"Authorization" => "Bearer #{api_key}",
|
|
}
|
|
|
|
sdxl_allowed_dimentions = [
|
|
[1024, 1024],
|
|
[1152, 896],
|
|
[1216, 832],
|
|
[1344, 768],
|
|
[1536, 640],
|
|
[640, 1536],
|
|
[768, 1344],
|
|
[832, 1216],
|
|
[896, 1152],
|
|
]
|
|
|
|
if (!width && !height)
|
|
if engine.include? "xl"
|
|
width, height = sdxl_allowed_dimentions[0]
|
|
else
|
|
width, height = [512, 512]
|
|
end
|
|
end
|
|
|
|
payload = {
|
|
text_prompts: [{ text: prompt }],
|
|
cfg_scale: 7,
|
|
clip_guidance_preset: "FAST_BLUE",
|
|
height: width,
|
|
width: height,
|
|
samples: 4,
|
|
steps: 30,
|
|
}
|
|
|
|
endpoint = "v1/generation/#{engine}/text-to-image"
|
|
|
|
response = Faraday.post("#{api_url}/#{endpoint}", payload.to_json, headers)
|
|
|
|
if response.status != 200
|
|
Rails.logger.error(
|
|
"AI stability generator failed with status #{response.status}: #{response.body}}",
|
|
)
|
|
raise Net::HTTPBadResponse
|
|
end
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
end
|
|
end
|
|
end
|
|
end
|