File size: 1,196 Bytes
98b445f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import os

HF_URL = "http://localhost:7860/completion"  # llama-server inside container

def translate(text, direction):
    if direction == "English β†’ Japanese":
        prompt = f"Translate to Japanese:\n{text}\nJapanese:"
    else:
        prompt = f"Translate to English:\n{text}\nEnglish:"

    resp = requests.post(
        HF_URL,
        json={
            "prompt": prompt,
            "n_predict": 128,
            "temperature": 0.1,
            "top_p": 0.9,
            "repeat_penalty": 1.3,
            "stop": ["\n\n","</s>"]
        }
    )
    return resp.json().get("content", "")

with gr.Blocks() as demo:
    gr.Markdown("# πŸ‡¬πŸ‡§β‡„πŸ‡―πŸ‡΅ English ↔ Japanese Translator (llama.cpp)")
    with gr.Row():
        with gr.Column():
            direction = gr.Radio(["English β†’ Japanese", "Japanese β†’ English"], value="English β†’ Japanese")
            input_box = gr.Textbox(label="Input")
            output_box = gr.Textbox(label="Translation")
            run_btn = gr.Button("Translate")
    run_btn.click(fn=translate, inputs=[input_box, direction], outputs=output_box)

demo.launch(server_name="0.0.0.0", server_port=7860)