From 9f8caeca53c9dfd037e3f54fccdf0d94c5142cc8 Mon Sep 17 00:00:00 2001 From: Cam Date: Mon, 11 Dec 2023 22:24:35 +0800 Subject: [PATCH] Optimizing Tesla client to convert text to image api --- lib/components/ai_pic.ex | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/components/ai_pic.ex b/lib/components/ai_pic.ex index 900f5bf..7100f94 100644 --- a/lib/components/ai_pic.ex +++ b/lib/components/ai_pic.ex @@ -1,9 +1,7 @@ defmodule Components.AiPic do - use HTTPoison.Base @base "https://clipdrop-api.co" - def text_to_image(txt) do end @@ -11,21 +9,25 @@ defmodule Components.AiPic do Tesla.client([ # TODO: convert input/output type {Tesla.Middleware.BaseUrl, @base}, - {Tesla.Middleware.Headers, [{"content-type", "text/plain"}, {"x-api-key", Constants.clipdrop_key()}]}, - {Tesla.Middleware.JSON, engine_opts: [keys: :atoms]} + {Tesla.Middleware.Headers, [{"x-api-key", Constants.clipdrop_key()}]} ]) end - + def run do + client() |> + do_text_to_image("Draw a dancing dog") + end def do_text_to_image(client, prompt) do - mp = - Tesla.Multipart.new() - |> Tesla.Multipart.add_file_content("prompt=#{prompt}", "sample.txt") - Tesla.post(client, "/text-to-image/v1", "prompt=#{prompt}") + mp = Tesla.Multipart.new() + |> Tesla.Multipart.add_content_type_param("charset=utf-8") + |> Tesla.Multipart.add_field("prompt", prompt, + headers: [{"content-type", "text/plain"}] + ) + Tesla.post(client, "/text-to-image/v1", mp) end # def do_text_to_image(txt) do # IO.puts inspect Constants.clipdrop_key() # body = "prompt=#{txt}" - # heads = + # heads = # [{"Content-Type", "text/plain"}, {"x-api-key", Constants.clipdrop_key()}] # IO.puts inspect heads # ExHttp.http_post(@api.text_to_image, body, heads, 3) @@ -37,15 +39,15 @@ defmodule Components.AiPic do # {"Content-Type", "text/plain"} # ] - # body = + # body = # "prompt=#{prompt}" # response = post!( - # @api.text_to_image, + # @api.text_to_image, # {headers, :multipart, body} # ) # # [:file, output_file]) # {:ok, response} # end -end \ No newline at end of file +end