File size: 1,417 Bytes
c2b596d
 
 
 
cfc0929
 
 
c2b596d
 
 
 
 
 
 
 
 
 
 
 
cfc0929
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b596d
cfc0929
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
import gradio as gr
import os
import openai

def translate(text, api_key, api_organization=None):
    openai.organization = api_organization
    openai.api_key = api_key
    system_prompt = "Translate Japanese to English. Please output only the translation result."
    response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo-0613",
                messages=[
                    {'role': 'system', 'content': system_prompt},
                    {'role': 'user', 'content': text},
                ],
                frequency_penalty = 0.0,
                temperature=0.0,
    )
    return response['choices'][0]['message']['content']

with gr.Blocks() as demo:
    gr.Markdown("Demo-app for Japanese -> English translation using GPT-3.5")
    with gr.Accordion("OpenAI API Settings", open=False):
        api_key = gr.Textbox(label="OpenAI API key", placeholder="OpenAI API key")
        api_organization = gr.Textbox(label="OpenAI API organization", placeholder="OpenAI API organization (optional)")
    with gr.Row():
        inp = gr.Textbox(label="Input", placeholder="Japanese")
        out = gr.Textbox(label="Output")
    examples = gr.Examples(
        [["γ“γ‚“γ«γ‘γ―γ€‚δ»Šζ—₯もいい倩気ですね。"],["θΊ«γ‹γ‚‰ε‡ΊγŸιŒ†"]],
        [inp],
    )
    btn = gr.Button("Translate")
    btn.click(fn=translate, inputs=[inp, api_key, api_organization], outputs=out)

demo.launch()