ReneeHWT commited on
Commit
6f19564
·
verified ·
1 Parent(s): 5b9a6e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py CHANGED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PART 1: Import the necessary modules
2
+ from flask import Flask, render_template, request
3
+ import openai
4
+
5
+ # PART 2: Set your OpenAI API key
6
+ api_key = "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"
7
+
8
+ # PART 3: Create a Flask web application
9
+ app = Flask(__name__)
10
+
11
+ # PART A: Define the home route
12
+ @app.route('/')
13
+ def index():
14
+ # Render the index.html template with no question and result values
15
+ return render_template('index.html', question=None, result=None)
16
+
17
+
18
+ # PART B: Define the submit route
19
+ @app.route('/', methods=['POST'])
20
+ def submit():
21
+ # Retrieve the prompt from the form submission
22
+ prompt = request.form['prompt']
23
+ # Call the access_openai function passing in the prompt value
24
+ result = access_openai(prompt)
25
+ # Render the index.html template with the prompt and result values
26
+ return render_template('index.html', question=prompt, result=result)
27
+
28
+
29
+ # Part C: Define the access_openai function
30
+
31
+ def access_openai(prompt_value):
32
+ openai.api_key = api_key
33
+ response = openai.Completion.create(
34
+ model="text-davinci-003",
35
+ prompt=f"請將以下的內容翻譯為英文:\n\n {prompt_value}",
36
+ max_tokens=200,
37
+ )
38
+ result = response.choices[0].text.strip()
39
+ print(result)
40
+ return result