Spaces:
Runtime error
Runtime error
Update app.py
Browse files12123545678910111213141516171819202122232425262728293031328910113512131415394016171819202122232425262728# PART 1: Import the necessary modulesfrom flask import Flask, render_template, requestimport openaiimport gradio as gr
# PART 2: Set your OpenAI API key# 設定你的 OpenAI API 金鑰api_key = "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"openai.api_key = api_key
# PART 3: Create a Flask web applicationapp = Flask(__name__)
# PART A: Define the home route@app.route('/')def index(): # Render the index.html template with no question and result values return render_template('index.html', question=None, result=None)
# PART B: Define the submit route@app.route('/', methods=['POST'])def submit(): # Retrieve the prompt from the form submission prompt = request.form['prompt'] # Call the access_openai function passing in the prompt value result = access_openai(prompt) # Render the index.html template with the prompt and result values return render_template('index.html', question=prompt, result=result)
# Part C: Define the access_openai function
def access_openai(prompt_value): openai.api_key = api_key# 定義呼叫 OpenAI API 的函數def translate_text(prompt): response = openai.Completion.create( model="text-davinci-003", prompt=f"請將以下的內容翻譯為英文:\n\n {prompt_value}", prompt=f"請將以下的內容翻譯為英文:\n\n{prompt}", max_tokens=200, ) result = response.choices[0].text.strip() print(result) return result return result
# 使用 Gradio 建立 UIinterface = gr.Interface( fn=translate_text, inputs="text", outputs="text", title="中文翻譯器", description="輸入中文句子,將其翻譯為英文。",)
# 啟動應用程式interface.launch()app.py+19-31