Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940# PART 1: Import the necessary modulesfrom flask import Flask, render_template, requestimport openai# PART 2: Set your OpenAI API keyapi_key = "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"# 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 valuesreturn 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 submissionprompt = request.form['prompt']# Call the access_openai function passing in the prompt valueresult = access_openai(prompt)# Render the index.html template with the prompt and result valuesreturn render_template('index.html', question=prompt, result=result)# Part C: Define the access_openai functiondef access_openai(prompt_value):openai.api_key = api_keyresponse = openai.Completion.create(model="text-davinci-003",prompt=f"請將以下的內容翻譯為英文:\n\n {prompt_value}",max_tokens=200,)result = response.choices[0].text.strip()print(result)return result12345678910111213141516171819202122232425262728import openaiimport 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 建立 UIinterface = gr.Interface(fn=translate_text,inputs="text",outputs="text",title="中文翻譯器",description="輸入中文句子,將其翻譯為英文。",)# 啟動應用程式interface.launch()
app.py+19-31