Skip to content

Commit

Permalink
feat: 支持更多翻译服务;
Browse files Browse the repository at this point in the history
  • Loading branch information
shuiRong committed Dec 24, 2024
1 parent b8b82e4 commit ec98d43
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 22 deletions.
4 changes: 4 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@ config :honyaku, deepl_api_key: System.get_env("DEEPL_API_KEY")
config :honyaku, rapid_api_key: System.get_env("RAPID_API_KEY")
# Open Router API key 生产环境
config :honyaku, open_router_api_key: System.get_env("OPEN_ROUTER_API_KEY")
# Oh My GPT API key 生产环境
config :honyaku, oh_my_gpt_api_key: System.get_env("OH_MY_GPT_API_KEY")
# Tencent API key 生产环境
config :honyaku, tencent_api_key: System.get_env("TENCENT_API_KEY")
4 changes: 2 additions & 2 deletions lib/honyaku/external/deepl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ defmodule Honyaku.External.DeepL do
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败#{inspect(reason)}")
Logger.error("DeepL Translator API调用失败,未知错误#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("DeepL API调用失败:#{inspect(reason)}")
Logger.error("DeepL Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
Expand Down
36 changes: 36 additions & 0 deletions lib/honyaku/external/deepl_translator/google.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Honyaku.External.DeeplTranslator.Google do
require Logger

@base_url "https://deep-translator-api.azurewebsites.net"

def translate(text, target_lang, source_lang) do
body = %{
"source" => source_lang,
"target" => target_lang,
"text" => text
}

case Req.post(
"#{@base_url}/google",
json: body
) do
{:ok,
%Req.Response{
status: 200,
body: %{"translation" => translated_text}
}} ->
{:ok, translated_text}

{:ok, %Req.Response{status: 429}} ->
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("Google Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("Google Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
end
36 changes: 36 additions & 0 deletions lib/honyaku/external/deepl_translator/mymemory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Honyaku.External.DeeplTranslator.MyMemory do
require Logger

@base_url "https://deep-translator-api.azurewebsites.net/"

def translate(text, target_lang, source_lang) do
body = %{
"source" => source_lang,
"target" => target_lang,
"text" => text
}

case Req.post(
"#{@base_url}/mymemory",
json: body
) do
{:ok,
%Req.Response{
status: 200,
body: %{"translation" => translated_text}
}} ->
{:ok, translated_text}

{:ok, %Req.Response{status: 429}} ->
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("MyMemory Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("MyMemory Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
end
4 changes: 2 additions & 2 deletions lib/honyaku/external/gemini/flash_1_5.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ defmodule Honyaku.External.Gemini.Flash1_5 do
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败,未知错误:#{inspect(reason)}")
Logger.error("Gemini Flash 1.5 Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("Gemini API调用失败:#{inspect(reason)}")
Logger.error("Gemini Flash 1.5 Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/honyaku/external/gemini/flash_2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ defmodule Honyaku.External.Gemini.Flash2 do
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败,未知错误:#{inspect(reason)}")
Logger.error("Gemini Flash 2 Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("Gemini API调用失败:#{inspect(reason)}")
Logger.error("Gemini Flash 2 Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/honyaku/external/groq/gemma2_2b.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ defmodule Honyaku.External.Groq.Gemma2_2b do
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败,未知错误:#{inspect(reason)}")
Logger.error("Gemma 2 2B Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("Gemini API调用失败:#{inspect(reason)}")
Logger.error("Gemma 2 2B Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/honyaku/external/load_balancer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ defmodule Honyaku.External.TranslationBalancer do
Rapid.FreeGoogleTranslator,
Rapid.AiBitTranslator,
Rapid.DeepLTranslator,
Groq.Gemma2_2b,
OpenRouter.Gemini2_Flash
OpenRouter.Gemini2_Flash,
OhMyGPT.GPT4o_Mini,
DeeplTranslator.Google,
DeeplTranslator.MyMemory
}

@apis [
Expand All @@ -21,8 +23,10 @@ defmodule Honyaku.External.TranslationBalancer do
FreeGoogleTranslator,
AiBitTranslator,
DeepLTranslator,
Gemma2_2b,
Gemini2_Flash
Gemini2_Flash,
GPT4o_Mini,
Google,
MyMemory
]

@doc """
Expand Down
11 changes: 9 additions & 2 deletions lib/honyaku/external/open_router/gemini_2_flash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,22 @@ defmodule Honyaku.External.OpenRouter.Gemini2_Flash do
}} ->
{:ok, content}

{:ok,
%Req.Response{
status: 200,
body: %{"error" => %{"code" => 429}}
}} ->
{:error, :quota_exhausted}

{:ok, %Req.Response{status: 429}} ->
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败,未知错误:#{inspect(reason)}")
Logger.error("Gemini 2 Flash Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("Gemini API调用失败:#{inspect(reason)}")
Logger.error("Gemini 2 Flash Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
Expand Down
60 changes: 60 additions & 0 deletions lib/honyaku/external/openai/gpt.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule Honyaku.External.OhMyGPT.GPT4o_Mini do
require Logger

@base_url "https://aigptx.top/v1"

def translate(text, target_lang, source_lang) do
key = Application.fetch_env!(:honyaku, :oh_my_gpt_api_key)

headers = [
{"Authorization", "Bearer #{key}"}
]

body = %{
"messages" => [
%{
"role" => "user",
"content" => """
Translate the following text from #{source_lang} to #{target_lang}, and return the translated text only:
#{text}
"""
}
],
"model" => "gpt-4o-mini",
"temperature" => 1,
"stream" => false
}

case Req.post(
"#{@base_url}/chat/completions",
headers: headers,
json: body
) do
{:ok,
%Req.Response{
status: 200,
body: %{"choices" => [%{"message" => %{"content" => content}}]}
}} ->
{:ok, content}

{:ok,
%Req.Response{
status: 200,
body: %{"error" => %{"code" => 429}}
}} ->
{:error, :quota_exhausted}

{:ok, %Req.Response{status: 429}} ->
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("Gemini 2 Flash Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("Gemini 2 Flash Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
end
3 changes: 1 addition & 2 deletions lib/honyaku/external/rapid/aibit_translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ defmodule Honyaku.External.Rapid.AiBitTranslator do

case Req.post("#{@base_url}/translator/html", headers: headers, json: body) do
{:ok, %Req.Response{status: 200, body: %{"trans" => translated_html}}} ->
Logger.info("翻译成功:#{translated_html}")
{:ok, translated_html}

{:ok, %Req.Response{status: 429}} ->
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败,未知错误:#{inspect(reason)}")
Logger.error("AiBit Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Expand Down
4 changes: 2 additions & 2 deletions lib/honyaku/external/rapid/deepl_translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ defmodule Honyaku.External.Rapid.DeepLTranslator do
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败#{inspect(reason)}")
Logger.error("DeepL Translator API调用失败,未知错误#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Logger.error("API调用失败:#{inspect(reason)}")
Logger.error("DeepL Translator API调用失败:#{inspect(reason)}")
{:error, reason}
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/honyaku/external/rapid/free_google_translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ defmodule Honyaku.External.Rapid.FreeGoogleTranslator do

case Req.post("#{@base_url}/free-google-translator", headers: headers, json: body) do
{:ok, %Req.Response{status: 200, body: %{"translation" => translated_text}}} ->
Logger.info("翻译成功:#{translated_text}")
{:ok, translated_text}

{:ok, %Req.Response{status: 200, body: %{"message" => reason}}} ->
Expand All @@ -36,7 +35,7 @@ defmodule Honyaku.External.Rapid.FreeGoogleTranslator do
{:error, :quota_exhausted}

{:ok, reason} ->
Logger.error("翻译失败,未知错误:#{inspect(reason)}")
Logger.error("Free Google Translator API调用失败,未知错误:#{inspect(reason)}")
{:error, :unknown_error}

{:error, reason} ->
Expand Down
2 changes: 0 additions & 2 deletions lib/honyaku_web/controllers/feed_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ defmodule HonyakuWeb.FeedController do
action_fallback HonyakuWeb.FallbackController

def index(conn, %{"url" => url, "target_lang" => target_lang, "source_lang" => source_lang}) do
Logger.info("解析 RSS 订阅源: #{url}")

case FeedContext.load_translated_feed(url, target_lang, source_lang) do
{:ok, translated_feed} ->
xml_content = translated_feed |> BuildFeed.build_feed()
Expand Down

0 comments on commit ec98d43

Please sign in to comment.