Spaces:
Runtime error
Runtime error
| 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() | |