|
1 | 1 | class Cloud |
2 | 2 | class CardGenerator |
3 | | - FLASH_IMAGE_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent" |
4 | | - |
5 | 3 | private attr_reader :cloud, :api_key |
6 | 4 |
|
7 | 5 | delegate :participant, to: :cloud |
8 | 6 |
|
9 | | - def initialize(cloud, api_key: GeminiConfig.api_key) |
| 7 | + def initialize(cloud) |
10 | 8 | @cloud = cloud |
11 | | - @api_key = api_key |
12 | 9 | end |
13 | 10 |
|
14 | 11 | def generate |
15 | | - raise ArgumentError, "Gemini API key not configured" if api_key.blank? |
16 | 12 | raise ArgumentError, "No photo attached" unless cloud.image.attached? |
17 | 13 |
|
18 | | - parts = [ |
19 | | - { |
20 | | - text: build_prompt |
21 | | - }, |
22 | | - build_inline_data(cloud.image.blob.download, cloud.image.content_type), |
23 | | - build_inline_data(Rails.public_path.join("sfruby_character.png").read, "image/png"), |
24 | | - build_inline_data(Rails.public_path.join("sfruby_character_h.png").read, "image/png"), |
25 | | - build_inline_data(Rails.public_path.join("sfruby_character_blue.png").read, "image/png") |
26 | | - ] |
27 | | - |
28 | | - request = { |
29 | | - contents: [ |
30 | | - { |
31 | | - parts: |
32 | | - } |
33 | | - ], |
34 | | - generationConfig: { |
35 | | - responseModalities: ["image"], |
36 | | - temperature: 1.0 |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - response = HTTParty.post( |
41 | | - "#{FLASH_IMAGE_URL}?key=#{api_key}", |
42 | | - body: request.to_json, |
43 | | - headers: {"Content-Type" => "application/json"}, |
44 | | - timeout: 90 |
45 | | - ) |
46 | | - |
47 | | - if response.success? |
48 | | - process_response(response.parsed_response) |
49 | | - else |
50 | | - raise "Gemini API error: #{response.code} - #{response.body}" |
51 | | - end |
52 | | - end |
| 14 | + chat = RubyLLM.chat(model: "gemini-2.5-flash-image") |
| 15 | + .with_temperature(1.0) |
| 16 | + .with_params(generationConfig: {responseModalities: ["image"]}) |
53 | 17 |
|
54 | | - private |
| 18 | + response = chat.ask build_prompt, with: build_attachments |
| 19 | + raise "LLM error: #{response.raw.body.dig("candidates", 0, "finishMessage") || "no data"}" if response.content.blank? |
55 | 20 |
|
56 | | - def build_inline_data(io, content_type) |
57 | | - { |
58 | | - inlineData: { |
59 | | - mimeType: content_type, |
60 | | - data: Base64.strict_encode64(io) |
61 | | - } |
62 | | - } |
| 21 | + response.content[:attachments].first.source |
63 | 22 | end |
64 | 23 |
|
65 | | - def process_response(response) |
66 | | - parts = response.dig("candidates", 0, "content", "parts") |
67 | | - |
68 | | - if parts.nil? |
69 | | - raise "No parts found in response" |
70 | | - end |
71 | | - |
72 | | - image_part = parts.find { it.dig("inlineData", "mimeType")&.start_with?("image/") } |
73 | | - |
74 | | - raise "No image data found in parts. Parts structure: #{parts.map(&:keys)}" unless image_part |
75 | | - |
76 | | - image_data = image_part["inlineData"]["data"] |
77 | | - decoded_image = Base64.decode64(image_data) |
| 24 | + private |
78 | 25 |
|
79 | | - StringIO.new(decoded_image) |
| 26 | + def build_attachments |
| 27 | + [ |
| 28 | + cloud.image.blob, |
| 29 | + Rails.public_path.join("sfruby_character.png"), |
| 30 | + Rails.public_path.join("sfruby_character_h.png"), |
| 31 | + Rails.public_path.join("sfruby_character_blue.png") |
| 32 | + ] |
80 | 33 | end |
81 | 34 |
|
82 | 35 | def build_prompt |
|
0 commit comments