-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI_Programmer.py
75 lines (60 loc) · 1.72 KB
/
AI_Programmer.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from flask import Flask, render_template, request
import openai
import os
app = Flask(__name__, template_folder="./templates")
app.template_engine = "mako"
openai.api_key = os.getenv("OPENAI_API_KEY")
from dotenv import load_dotenv
def create_prompt_form_config(config):
prompt = ""
for word in config:
prompt += word + ""
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
# Get user inputs from form
text = request.form["text"]
config = request.form.getlist("config")
# Build prompt string
prompt = create_prompt_form_config(config)
# Call OpenAI GPT API
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# Display response to user
return render_template(
"result.html",
text=text,
config=config,
response=response.choices[0].text,
)
else:
config = [
"flask",
"openai",
"requirements.txt",
"app.py",
"templates",
"index.html",
"result.html",
"Procfile",
"runtime.txt",
"README.md",
"LICENSE",
"setup.sh",
"requirements.txt",
"ru",
]
# Display form for user input
return render_template("index.html", config=config)
if __name__ == "__main__":
load_dotenv()
openai.organization = os.getenv("OPENAI_ORG_ID")
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Model.list()
app.run(debug=True)