Class: SmartPrompt::VideoGenerationAdapter

Inherits:
LLMAdapter
  • Object
show all
Defined in:
lib/smart_prompt/video_generation_adapter.rb

Constant Summary collapse

SUPPORTED_IMAGE_FORMATS =
%w[jpg jpeg png gif bmp webp]
SUPPORTED_VIDEO_FORMATS =
%w[mp4 mov avi mkv webm]

Instance Attribute Summary

Attributes inherited from LLMAdapter

#last_response

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ VideoGenerationAdapter

Returns a new instance of VideoGenerationAdapter.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smart_prompt/video_generation_adapter.rb', line 11

def initialize(config)
  super
  api_key = @config["api_key"]
  if api_key.is_a?(String) && api_key.start_with?("ENV[") && api_key.end_with?("]")
    api_key = eval(api_key)
  end
  begin
    @client = OpenAI::Client.new(
      access_token: api_key,
      uri_base: @config["url"],
      request_timeout: 600, # Longer timeout for video generation
    )
  rescue OpenAI::ConfigurationError => e
    SmartPrompt.logger.error "Failed to initialize VideoGeneration client: #{e.message}"
    raise LLMAPIError, "Invalid VideoGeneration configuration: #{e.message}"
  rescue OpenAI::Error => e
    SmartPrompt.logger.error "Failed to initialize VideoGeneration client: #{e.message}"
    raise LLMAPIError, "VideoGeneration authentication failed: #{e.message}"
  rescue SocketError => e
    SmartPrompt.logger.error "Failed to initialize VideoGeneration client: #{e.message}"
    raise LLMAPIError, "Network error: Unable to connect to VideoGeneration API"
  rescue => e
    SmartPrompt.logger.error "Failed to initialize VideoGeneration client: #{e.message}"
    raise Error, "Unexpected error initializing VideoGeneration client: #{e.message}"
  ensure
    SmartPrompt.logger.info "Successfully created a VideoGeneration client."
  end
end

Instance Method Details

#check_video_status(job_id) ⇒ Object

Check video generation status



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/smart_prompt/video_generation_adapter.rb', line 156

def check_video_status(job_id)
  SmartPrompt.logger.info "VideoGenerationAdapter: Checking video generation status"

  begin
    response = check_video_generation_status(job_id)

    @last_response = response

    if response["data"]
      status_data = {
        job_id: response["data"]["id"],
        status: response["data"]["status"],
        video_url: response["data"]["video_url"],
        progress: response["data"]["progress"],
        created_at: response["data"]["created_at"],
        updated_at: response["data"]["updated_at"]
      }

      SmartPrompt.logger.info "Video status: #{status_data[:status]}, Progress: #{status_data[:progress]}"
      return status_data
    else
      SmartPrompt.logger.error "No status data in response"
      raise LLMAPIError, "No status data in response"
    end

  rescue => e
    SmartPrompt.logger.error "Error checking video status: #{e.message}"
    raise Error, "Error checking video status: #{e.message}"
  end
end

#create_video_from_image(image_file, prompt, model: nil, duration: 4, resolution: "720p", fps: 24, seed: nil) ⇒ Object

Image-to-video generation



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/smart_prompt/video_generation_adapter.rb', line 96

def create_video_from_image(image_file, prompt, model: nil, duration: 4, resolution: "720p", fps: 24, seed: nil)
  SmartPrompt.logger.info "VideoGenerationAdapter: Creating video from image"

  model_name = model || @config["model"]

  begin
    # Prepare image file
    unless File.exist?(image_file)
      raise Error, "Image file not found: #{image_file}"
    end

    file_ext = File.extname(image_file).downcase.delete(".")
    unless SUPPORTED_IMAGE_FORMATS.include?(file_ext)
      raise Error, "Unsupported image format: #{file_ext}"
    end

    # Convert image to base64 for API submission
    image_data = File.binread(image_file)
    base64_image = Base64.strict_encode64(image_data)

    parameters = {
      model: model_name,
      image: base64_image,
      prompt: prompt,
      duration: duration,
      resolution: resolution,
      fps: fps
    }

    parameters[:seed] = seed if seed

    SmartPrompt.logger.info "Image-to-video parameters: #{parameters}"

    # Custom implementation for image-to-video generation
    response = submit_image_to_video_request(parameters)

    @last_response = response

    if response["data"] && response["data"]["video_url"]
      video_data = {
        video_url: response["data"]["video_url"],
        status: response["data"]["status"],
        job_id: response["data"]["id"],
        created_at: response["data"]["created_at"]
      }

      SmartPrompt.logger.info "Image-to-video job submitted successfully"
      return video_data
    else
      SmartPrompt.logger.error "No video data in image-to-video response"
      raise LLMAPIError, "No video data in image-to-video response"
    end

  rescue => e
    SmartPrompt.logger.error "Unexpected error during image-to-video generation: #{e.message}"
    raise Error, "Unexpected error during image-to-video generation: #{e.message}"
  end
end

#download_video(video_url, output_path) ⇒ Object

Download video to file



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/smart_prompt/video_generation_adapter.rb', line 188

def download_video(video_url, output_path)
  SmartPrompt.logger.info "VideoGenerationAdapter: Downloading video"

  begin
    uri = URI.parse(video_url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.scheme == 'https')

    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)

    if response.is_a?(Net::HTTPSuccess)
      # Create directory if it doesn't exist
      FileUtils.mkdir_p(File.dirname(output_path))

      File.binwrite(output_path, response.body)
      SmartPrompt.logger.info "Video downloaded successfully to: #{output_path}"
      return output_path
    else
      SmartPrompt.logger.error "Failed to download video: #{response.code}"
      raise Error, "Failed to download video: #{response.code}"
    end

  rescue => e
    SmartPrompt.logger.error "Error downloading video: #{e.message}"
    raise Error, "Error downloading video: #{e.message}"
  end
end

#generate_video(prompt, model: nil, duration: 4, resolution: "720p", fps: 24, seed: nil) ⇒ Object

Text-to-video generation



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/smart_prompt/video_generation_adapter.rb', line 41

def generate_video(prompt, model: nil, duration: 4, resolution: "720p", fps: 24, seed: nil)
  SmartPrompt.logger.info "VideoGenerationAdapter: Generating video from text"

  model_name = model || @config["model"]

  begin
    # SiliconFlow uses OpenAI-compatible API format for video generation
    # Note: This might require custom implementation as OpenAI gem doesn't have video endpoints
    parameters = {
      model: model_name,
      prompt: prompt,
      duration: duration,
      resolution: resolution,
      fps: fps
    }

    parameters[:seed] = seed if seed

    SmartPrompt.logger.info "Video generation parameters: #{parameters}"

    # Custom implementation for video generation
    # Since OpenAI gem doesn't support video endpoints, we'll use direct HTTP calls
    response = submit_video_generation_request(parameters)

    @last_response = response

    # Process response
    if response["data"] && response["data"]["video_url"]
      video_data = {
        video_url: response["data"]["video_url"],
        status: response["data"]["status"],
        job_id: response["data"]["id"],
        created_at: response["data"]["created_at"]
      }

      SmartPrompt.logger.info "Video generation job submitted successfully"
      return video_data
    else
      SmartPrompt.logger.error "No video data in response"
      raise LLMAPIError, "No video data in response"
    end

  rescue OpenAI::Error => e
    SmartPrompt.logger.error "Video generation API error: #{e.message}"
    raise LLMAPIError, "Video generation API error: #{e.message}"
  rescue JSON::ParserError => e
    SmartPrompt.logger.error "Failed to parse video generation response"
    raise LLMAPIError, "Failed to parse video generation response"
  rescue => e
    SmartPrompt.logger.error "Unexpected error during video generation: #{e.message}"
    raise Error, "Unexpected error during video generation: #{e.message}"
  end
end

#wait_for_video_completion(job_id, check_interval: 10, timeout: 600) ⇒ Object

Wait for video generation to complete



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/smart_prompt/video_generation_adapter.rb', line 218

def wait_for_video_completion(job_id, check_interval: 10, timeout: 600)
  SmartPrompt.logger.info "VideoGenerationAdapter: Waiting for video generation to complete"

  start_time = Time.now

  loop do
    status = check_video_status(job_id)

    case status[:status]
    when "completed"
      SmartPrompt.logger.info "Video generation completed successfully"
      return status
    when "failed"
      SmartPrompt.logger.error "Video generation failed"
      raise LLMAPIError, "Video generation failed"
    when "cancelled"
      SmartPrompt.logger.error "Video generation was cancelled"
      raise LLMAPIError, "Video generation was cancelled"
    else
      # Still processing
      elapsed_time = Time.now - start_time
      if elapsed_time > timeout
        SmartPrompt.logger.error "Video generation timeout after #{timeout} seconds"
        raise LLMAPIError, "Video generation timeout"
      end

      SmartPrompt.logger.info "Video generation in progress: #{status[:progress]}%"
      sleep(check_interval)
    end
  end
end