FEATURE: Basic StableDiffusion text2img support (#72)

This commit is contained in:
Rafael dos Santos Silva 2023-05-19 20:38:08 -03:00 committed by GitHub
parent ba65d42940
commit 262ed4753e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View File

@ -94,6 +94,13 @@ plugins:
ai_anthropic_api_key:
default: ""
secret: true
ai_stability_api_key:
default: ""
secret: true
ai_stability_api_url:
default: "https://api.stability.ai"
ai_stability_engine:
default: "stable-diffusion-xl-beta-v2-2-2"
composer_ai_helper_enabled:
default: false

View File

@ -0,0 +1,36 @@
# frozen_string_literal: true
module ::DiscourseAi
module Inference
class StabilityGenerator
def self.perform!(prompt)
headers = {
"Referer" => Discourse.base_url,
"Content-Type" => "application/json",
"Accept" => "application/json",
"Authorization" => "Bearer #{SiteSetting.ai_stability_api_key}",
}
payload = {
text_prompts: [{ text: prompt }],
cfg_scale: 7,
clip_guidance_preset: "FAST_BLUE",
height: 512,
width: 512,
samples: 4,
steps: 30,
}
base_url = SiteSetting.ai_stability_api_url
engine = SiteSetting.ai_stability_engine
endpoint = "v1/generation/#{engine}/text-to-image"
response = Faraday.post("#{base_url}/#{endpoint}", payload.to_json, headers)
raise Net::HTTPBadResponse if response.status != 200
JSON.parse(response.body, symbolize_names: true)
end
end
end
end

View File

@ -32,6 +32,7 @@ after_initialize do
require_relative "lib/shared/inference/openai_completions"
require_relative "lib/shared/inference/openai_embeddings"
require_relative "lib/shared/inference/anthropic_completions"
require_relative "lib/shared/inference/stability_generator"
require_relative "lib/shared/classificator"
require_relative "lib/shared/post_classificator"