Add raw Deepl module and a progress bar
This commit is contained in:
77
lib/outlook/translators/deepl.ex
Normal file
77
lib/outlook/translators/deepl.ex
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
defmodule Outlook.Translators.Deepl do
|
||||||
|
|
||||||
|
def test(pid) do
|
||||||
|
for n <- 0..100 do
|
||||||
|
send(pid, {:progress, %{progress: n}})
|
||||||
|
Process.sleep 50
|
||||||
|
end
|
||||||
|
send(pid, {:progress, %{progress: nil}})
|
||||||
|
end
|
||||||
|
|
||||||
|
# @options [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 500]
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Upload the content to translate and return document_id and document_key as Map.
|
||||||
|
"""
|
||||||
|
def start_translation %{auth_key: auth_key} = _credentials, content, target_lang do
|
||||||
|
form = get_multipart_form(
|
||||||
|
[
|
||||||
|
{"target_lang", target_lang},
|
||||||
|
{"file", content, {"form-data", [{:name, "file"}, {:filename, "datei.html"}]}, []}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
response_raw = HTTPoison.request!(
|
||||||
|
:post,
|
||||||
|
"https://api-free.deepl.com/v2/document",
|
||||||
|
form,
|
||||||
|
get_multipart_headers(auth_key)
|
||||||
|
)
|
||||||
|
Jason.decode!(response_raw.body, keys: :atoms)
|
||||||
|
|> Map.put(:auth_key, auth_key)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Upload the content to translate and return the estimated time until done.
|
||||||
|
"""
|
||||||
|
def check_status credentials do
|
||||||
|
response_raw = HTTPoison.request!(
|
||||||
|
:post,
|
||||||
|
"https://api-free.deepl.com/v2/document/#{credentials.document_id}",
|
||||||
|
get_multipart_form([{"document_key", credentials.document_key}]),
|
||||||
|
get_multipart_headers(credentials.auth_key)
|
||||||
|
)
|
||||||
|
response = Jason.decode!(response_raw.body, keys: :atoms)
|
||||||
|
|
||||||
|
case response do
|
||||||
|
%{status: "translating"} ->
|
||||||
|
Process.sleep(String.to_integer(response.seconds_remaining) * 1000)
|
||||||
|
check_status(credentials)
|
||||||
|
%{status: "done"} ->
|
||||||
|
response
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_translation credentials do
|
||||||
|
|
||||||
|
response_raw = HTTPoison.request!(
|
||||||
|
:post,
|
||||||
|
"https://api-free.deepl.com/v2/document/#{credentials.document_id}/result",
|
||||||
|
get_multipart_form([{"document_key", credentials.document_key}]),
|
||||||
|
get_multipart_headers(credentials.auth_key)
|
||||||
|
)
|
||||||
|
response_raw.body
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_multipart_form fields do
|
||||||
|
{:multipart, fields}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_multipart_headers(auth_key) do
|
||||||
|
[
|
||||||
|
"Authorization": "DeepL-Auth-Key #{auth_key}",
|
||||||
|
"Content-Type": "multipart/form-data",
|
||||||
|
"Transfer-Encoding": "chunked",
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -39,6 +39,9 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
|
|||||||
</div>
|
</div>
|
||||||
<div class="article">
|
<div class="article">
|
||||||
<%= InternalTree.render_html_preview(@translation.article.content, @myself) |> raw %>
|
<%= InternalTree.render_html_preview(@translation.article.content, @myself) |> raw %>
|
||||||
|
<.button phx-disable-with="Translating..." phx-click="translate-deepl" phx-target={@myself}
|
||||||
|
data-confirm-not="Are you sure? All previously translated text will be lost.">Translate with Deepl</.button>
|
||||||
|
<progress :if={@deepl_progress} max="100" value={@deepl_progress} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
"""
|
"""
|
||||||
@ -51,10 +54,20 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
|
|||||||
{:ok,
|
{:ok,
|
||||||
socket
|
socket
|
||||||
|> assign(assigns)
|
|> assign(assigns)
|
||||||
|> assign(:changeset, changeset)}
|
|> assign(:changeset, changeset)
|
||||||
|
|> assign(:deepl_progress, nil)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def update(%{progress: progress}, socket) do
|
||||||
|
{:ok, socket |> assign(deepl_progress: progress)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
def handle_event("translate-deepl", _, socket) do
|
||||||
|
Task.start_link(Outlook.Translators.Deepl, :test, [self()])
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
def handle_event("validate", %{"translation" => translation_params}, socket) do
|
def handle_event("validate", %{"translation" => translation_params}, socket) do
|
||||||
changeset =
|
changeset =
|
||||||
socket.assigns.translation
|
socket.assigns.translation
|
||||||
|
|||||||
@ -50,4 +50,10 @@ defmodule OutlookWeb.TranslationLive.NewEdit do
|
|||||||
defp common_assigns(socket) do
|
defp common_assigns(socket) do
|
||||||
assign(socket, form_cmpnt_id: @form_cmpnt_id)
|
assign(socket, form_cmpnt_id: @form_cmpnt_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_info({:progress, payload}, socket) do
|
||||||
|
send_update(self(), FormComponent, progress: payload.progress, id: @form_cmpnt_id)
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user