|
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() |