15 def mesh_gen(image_url, options)
16 api_key = options.api_key || ENV["REPLICATE_API_KEY"]
17 raise Rager::Errors::CredentialsError.new("Replicate", env_var: ["REPLICATE_API_KEY"]) if api_key.nil?
18
19 body = {
20 version: options.version,
21 input: {
22 images: [image_url],
23 texture_size: 2048,
24 mesh_simplify: 0.9,
25 generate_model: true,
26 save_gaussian_ply: true,
27 ss_sampling_steps: 38
28 }
29 }.tap do |b|
30 b[:input][:seed] = options.seed if options.seed
31 end
32
33 request = Rager::Http::Request.new(
34 url: "https://api.replicate.com/v1/predictions",
35 verb: Rager::Http::Verb::Post,
36 headers: {
37 "Authorization" => "Bearer #{api_key}",
38 "Content-Type" => "application/json"
39 },
40 body: body.to_json,
41 timeout: options.timeout || Rager.config.timeout
42 )
43
44 response = Rager.config.http_adapter.make_request(request)
45 response_body = T.cast(T.must(response.body), String)
46
47 raise Rager::Errors::HttpError.new(Rager.config.http_adapter, request.url, response.status, body: response_body) unless [200, 201, 202].include?(response.status)
48
49 json = JSON.parse(response_body)
50 json.fetch("urls").fetch("get")
51 rescue JSON::ParserError, KeyError => e
52 raise Rager::Errors::ParseError.new(response_body || "", details: e.message)
53 end