File size: 4,080 Bytes
ab95e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180aa18
ab95e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import tempfile
import gradio as gr
import toml
from aphra import translate

theme = gr.themes.Soft(
    primary_hue="rose",
    secondary_hue="pink",
    spacing_size="lg",
)

def create_config_file(api_key, writer_model, searcher_model, critic_model):
    config = {
        "openrouter": {"api_key": api_key},
        "llms": {
            "writer": writer_model,
            "searcher": searcher_model,
            "critiquer": critic_model
        }
    }
    with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.toml') as tmp:
        toml.dump(config, tmp)
    return tmp.name

def process_input(file, text_input, api_key, writer_model, searcher_model, critic_model, source_lang, target_lang):
    if file is not None:
        with open(file, 'r', encoding='utf-8') as file:
            text = file.read()
    else:
        text = text_input
    config_file = create_config_file(api_key, writer_model, searcher_model, critic_model)
    try:
        translation = translate(
            source_language=source_lang,
            target_language=target_lang,
            text=text,
            config_file=config_file,
            log_calls=False
        )
    finally:
        os.unlink(config_file)
    
    return translation

def create_interface():
    with gr.Blocks(theme=theme) as demo:
        gr.Markdown("<font size=6.5><center>πŸŒπŸ’¬ Aphra</center></font>")
        gr.Markdown(
            """<div style="display: flex;align-items: center;justify-content: center">
            [<a href="https://davidlms.github.io/aphra/">Project Page</a>] | [<a href="https://github.com/DavidLMS/aphra">Github</a>]</div>
            """
        )
        gr.Markdown("πŸŒπŸ’¬ Aphra is an open-source translation agent designed to enhance the quality of text translations by leveraging large language models (LLMs).")
        
        with gr.Row():
            api_key = gr.Textbox(label="OpenRouter API Key", type="password")
            
            writer_model = gr.Dropdown(
                ["anthropic/claude-3.5-sonnet:beta", "openai/gpt-4o-2024-08-06", "google/gemini-pro-1.5-exp"],
                label="Writer Model",
                value="anthropic/claude-3.5-sonnet:beta",
                allow_custom_value=True
            )
            searcher_model = gr.Dropdown(
                ["perplexity/llama-3-sonar-large-32k-online", "perplexity/llama-3.1-sonar-huge-128k-online", "perplexity/llama-3.1-sonar-small-128k-online"],
                label="Searcher Model",
                value="perplexity/llama-3-sonar-large-32k-online",
                allow_custom_value=True
            )
            critic_model = gr.Dropdown(
                ["anthropic/claude-3.5-sonnet:beta", "openai/gpt-4o-2024-08-06", "google/gemini-pro-1.5-exp"],
                label="Critic Model",
                value="anthropic/claude-3.5-sonnet:beta",
                allow_custom_value=True
            )

        with gr.Row():
            source_lang = gr.Dropdown(
                ["Spanish", "English", "French", "German"],
                label="Source Language",
                value="Spanish",
                allow_custom_value=True
            )
            target_lang = gr.Dropdown(
                ["English", "Spanish", "French", "German"],
                label="Target Language",
                value="English",
                allow_custom_value=True
            )

        with gr.Row(): 
            file = gr.File(label="Upload .txt or .md file", file_types=[".txt", ".md"])
            text_input = gr.Textbox(label="Or paste your text here", lines=5)
        
        translate_btn = gr.Button("Translate with πŸŒπŸ’¬ Aphra")
        
        output = gr.Textbox(label="Translation by πŸŒπŸ’¬ Aphra")
        
        translate_btn.click(
            process_input,
            inputs=[file, text_input, api_key, writer_model, searcher_model, critic_model, source_lang, target_lang],
            outputs=[output]
        )
    
    return demo

if __name__ == "__main__":
    interface = create_interface()
    interface.launch()