File size: 762 Bytes
46ae407
 
 
6f19564
86d1d1d
6f19564
86d1d1d
6f19564
86d1d1d
6f19564
86d1d1d
 
6f19564
 
86d1d1d
6f19564
 
 
86d1d1d
 
 
 
 
 
 
 
 
 
 
 
0e5dc89
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
pip install openai
pip install gradio

import openai
import gradio as gr

# 設定你的 OpenAI API 金鑰
api_key = "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"
openai.api_key = api_key

# 定義呼叫 OpenAI API 的函數
def translate_text(prompt):
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=f"請將以下的內容翻譯為英文:\n\n{prompt}",
        max_tokens=200,
    )
    result = response.choices[0].text.strip()
    return result

# 使用 Gradio 建立 UI
interface = gr.Interface(
    fn=translate_text, 
    inputs="text", 
    outputs="text", 
    title="中文翻譯器",
    description="輸入中文句子,將其翻譯為英文。",
)

# 啟動應用程式
interface.launch()