Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import toml
|
| 5 |
+
from aphra import translate
|
| 6 |
+
|
| 7 |
+
theme = gr.themes.Soft(
|
| 8 |
+
primary_hue="rose",
|
| 9 |
+
secondary_hue="pink",
|
| 10 |
+
spacing_size="lg",
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def create_config_file(api_key, writer_model, searcher_model, critic_model):
|
| 14 |
+
config = {
|
| 15 |
+
"openrouter": {"api_key": api_key},
|
| 16 |
+
"llms": {
|
| 17 |
+
"writer": writer_model,
|
| 18 |
+
"searcher": searcher_model,
|
| 19 |
+
"critiquer": critic_model
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.toml') as tmp:
|
| 23 |
+
toml.dump(config, tmp)
|
| 24 |
+
return tmp.name
|
| 25 |
+
|
| 26 |
+
def process_input(file, text_input, api_key, writer_model, searcher_model, critic_model, source_lang, target_lang):
|
| 27 |
+
if file is not None:
|
| 28 |
+
with open(file, 'r', encoding='utf-8') as file:
|
| 29 |
+
text = file.read()
|
| 30 |
+
else:
|
| 31 |
+
text = text_input
|
| 32 |
+
config_file = create_config_file(api_key, writer_model, searcher_model, critic_model)
|
| 33 |
+
try:
|
| 34 |
+
translation = translate(
|
| 35 |
+
source_language=source_lang,
|
| 36 |
+
target_language=target_lang,
|
| 37 |
+
text=text,
|
| 38 |
+
config_file=config_file,
|
| 39 |
+
log_calls=False
|
| 40 |
+
)
|
| 41 |
+
finally:
|
| 42 |
+
os.unlink(config_file)
|
| 43 |
+
|
| 44 |
+
return translation
|
| 45 |
+
|
| 46 |
+
def create_interface():
|
| 47 |
+
with gr.Blocks(theme=theme) as demo:
|
| 48 |
+
gr.Markdown("<font size=6.5><center>ππ¬ Aphra</center></font>")
|
| 49 |
+
gr.Markdown(
|
| 50 |
+
"""<div style="display: flex;align-items: center;justify-content: center">
|
| 51 |
+
[<a href="https://davidlms.github.io/aphra/">Project Page</a>] | [<a href="https://github.com/DavidLMS/aphra">Github</a>]</div>
|
| 52 |
+
"""
|
| 53 |
+
)
|
| 54 |
+
gr.Markdown("ππ¬ Aphra is an open-source translation agent designed to enhance the quality of text translations by leveraging large language models (LLMs).")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
api_key = gr.Textbox(label="Openrouter API Key", type="password")
|
| 58 |
+
|
| 59 |
+
writer_model = gr.Dropdown(
|
| 60 |
+
["anthropic/claude-3.5-sonnet:beta", "openai/gpt-4o-2024-08-06", "google/gemini-pro-1.5-exp"],
|
| 61 |
+
label="Writer Model",
|
| 62 |
+
value="anthropic/claude-3.5-sonnet:beta",
|
| 63 |
+
allow_custom_value=True
|
| 64 |
+
)
|
| 65 |
+
searcher_model = gr.Dropdown(
|
| 66 |
+
["perplexity/llama-3-sonar-large-32k-online", "perplexity/llama-3.1-sonar-huge-128k-online", "perplexity/llama-3.1-sonar-small-128k-online"],
|
| 67 |
+
label="Searcher Model",
|
| 68 |
+
value="perplexity/llama-3-sonar-large-32k-online",
|
| 69 |
+
allow_custom_value=True
|
| 70 |
+
)
|
| 71 |
+
critic_model = gr.Dropdown(
|
| 72 |
+
["anthropic/claude-3.5-sonnet:beta", "openai/gpt-4o-2024-08-06", "google/gemini-pro-1.5-exp"],
|
| 73 |
+
label="Critic Model",
|
| 74 |
+
value="anthropic/claude-3.5-sonnet:beta",
|
| 75 |
+
allow_custom_value=True
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
source_lang = gr.Dropdown(
|
| 80 |
+
["Spanish", "English", "French", "German"],
|
| 81 |
+
label="Source Language",
|
| 82 |
+
value="Spanish",
|
| 83 |
+
allow_custom_value=True
|
| 84 |
+
)
|
| 85 |
+
target_lang = gr.Dropdown(
|
| 86 |
+
["English", "Spanish", "French", "German"],
|
| 87 |
+
label="Target Language",
|
| 88 |
+
value="English",
|
| 89 |
+
allow_custom_value=True
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
file = gr.File(label="Upload .txt or .md file", file_types=[".txt", ".md"])
|
| 94 |
+
text_input = gr.Textbox(label="Or paste your text here", lines=5)
|
| 95 |
+
|
| 96 |
+
translate_btn = gr.Button("Translate with ππ¬ Aphra")
|
| 97 |
+
|
| 98 |
+
output = gr.Textbox(label="Translation by ππ¬ Aphra")
|
| 99 |
+
|
| 100 |
+
translate_btn.click(
|
| 101 |
+
process_input,
|
| 102 |
+
inputs=[file, text_input, api_key, writer_model, searcher_model, critic_model, source_lang, target_lang],
|
| 103 |
+
outputs=[output]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
return demo
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
interface = create_interface()
|
| 110 |
+
interface.launch()
|