Skip to content

Commit 0a2e22b

Browse files
committed
initial commit
0 parents  commit 0a2e22b

File tree

8 files changed

+212
-0
lines changed

8 files changed

+212
-0
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FLASK_APP=app
2+
FLASK_ENV=development
3+
OPENAI_API_KEY=

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
venv/
2+
3+
*.pyc
4+
__pycache__/
5+
6+
instance/
7+
8+
.pytest_cache/
9+
.coverage
10+
htmlcov/
11+
12+
dist/
13+
build/
14+
*.egg-info/
15+
16+
.DS_STORE
17+
18+
.env

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# OpenAI API Quickstart - Python example app
2+
3+
This is an example pet name generator app used in the OpenAI API [quickstart tutorial](https://beta.openai.com/docs/quickstart). It uses the [Flask](https://flask.palletsprojects.com/en/2.0.x/) web framework. Check out the tutorial or follow the instructions below to get set up.
4+
5+
## Setup
6+
7+
1. If you don’t have Python installed, [install it from here](https://www.python.org/downloads/)
8+
9+
2. Clone this repository
10+
11+
3. Navigate into the project directory
12+
13+
```bash
14+
$ cd openai-quickstart-python
15+
```
16+
17+
4. Create a new virtual environment
18+
19+
```bash
20+
$ python -m venv venv
21+
$ . venv/bin/activate
22+
```
23+
24+
5. Install the requirements
25+
26+
```bash
27+
$ pip install -r requirements.txt
28+
```
29+
30+
6. Make a copy of the example environment variables file
31+
32+
```bash
33+
$ cp .env.example .env
34+
```
35+
36+
7. Add your [API key](https://beta.openai.com/account/api-keys) to the newly created `.env` file
37+
38+
8. Run the app
39+
40+
```bash
41+
$ flask run
42+
```
43+
44+
You should now be able to access the app at [http://localhost:5000](http://localhost:5000)! For the full context behind this example app, check out the [tutorial](https://beta.openai.com/docs/quickstart).

app.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
3+
import openai
4+
from flask import Flask, redirect, render_template, request, url_for
5+
6+
app = Flask(__name__)
7+
openai.api_key = os.getenv("OPENAI_API_KEY")
8+
9+
10+
@app.route("/", methods=("GET", "POST"))
11+
def index():
12+
if request.method == "POST":
13+
animal = request.form["animal"]
14+
response = openai.Completion.create(
15+
engine="text-davinci-001",
16+
prompt=generate_prompt(animal),
17+
temperature=0.6,
18+
)
19+
return redirect(url_for("index", result=response.choices[0].text))
20+
21+
result = request.args.get("result")
22+
return render_template("index.html", result=result)
23+
24+
25+
def generate_prompt(animal):
26+
return """Suggest three names for an animal that is a superhero.
27+
28+
Animal: Cat
29+
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
30+
Animal: Dog
31+
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
32+
Animal: {}
33+
Names:""".format(
34+
animal.capitalize()
35+
)

requirements.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
autopep8==1.6.0
2+
certifi==2021.10.8
3+
charset-normalizer==2.0.7
4+
click==8.0.3
5+
et-xmlfile==1.1.0
6+
Flask==2.0.2
7+
idna==3.3
8+
itsdangerous==2.0.1
9+
Jinja2==3.0.2
10+
MarkupSafe==2.0.1
11+
numpy==1.21.3
12+
openai==0.11.0
13+
openpyxl==3.0.9
14+
pandas==1.3.4
15+
pandas-stubs==1.2.0.35
16+
pycodestyle==2.8.0
17+
python-dateutil==2.8.2
18+
python-dotenv==0.19.2
19+
pytz==2021.3
20+
requests==2.26.0
21+
six==1.16.0
22+
toml==0.10.2
23+
tqdm==4.62.3
24+
urllib3==1.26.7
25+
Werkzeug==2.0.2

static/dog.png

1.71 KB
Loading

static/main.css

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
@font-face {
2+
font-family: "ColfaxAI";
3+
src: url(https://cdn.openai.com/API/fonts/ColfaxAIRegular.woff2)
4+
format("woff2"),
5+
url(https://cdn.openai.com/API/fonts/ColfaxAIRegular.woff) format("woff");
6+
font-weight: normal;
7+
font-style: normal;
8+
}
9+
@font-face {
10+
font-family: "ColfaxAI";
11+
src: url(https://cdn.openai.com/API/fonts/ColfaxAIBold.woff2) format("woff2"),
12+
url(https://cdn.openai.com/API/fonts/ColfaxAIBold.woff) format("woff");
13+
font-weight: bold;
14+
font-style: normal;
15+
}
16+
body,
17+
input {
18+
font-size: 16px;
19+
line-height: 24px;
20+
color: #353740;
21+
font-family: "ColfaxAI", Helvetica, sans-serif;
22+
}
23+
body {
24+
display: flex;
25+
flex-direction: column;
26+
align-items: center;
27+
padding-top: 60px;
28+
}
29+
.icon {
30+
width: 34px;
31+
}
32+
h3 {
33+
font-size: 32px;
34+
line-height: 40px;
35+
font-weight: bold;
36+
color: #202123;
37+
margin: 16px 0 40px;
38+
}
39+
form {
40+
display: flex;
41+
flex-direction: column;
42+
width: 320px;
43+
}
44+
input[type="text"] {
45+
padding: 12px 16px;
46+
border: 1px solid #10a37f;
47+
border-radius: 4px;
48+
margin-bottom: 24px;
49+
}
50+
::placeholder {
51+
color: #8e8ea0;
52+
opacity: 1;
53+
}
54+
input[type="submit"] {
55+
padding: 12px 0;
56+
color: #fff;
57+
background-color: #10a37f;
58+
border: none;
59+
border-radius: 4px;
60+
text-align: center;
61+
cursor: pointer;
62+
}
63+
.result {
64+
font-weight: bold;
65+
margin-top: 40px;
66+
}

templates/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<title>OpenAI Quickstart</title>
4+
<link
5+
rel="shortcut icon"
6+
href="{{ url_for('static', filename='dog.png') }}"
7+
/>
8+
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}" />
9+
</head>
10+
11+
<body>
12+
<img src="{{ url_for('static', filename='dog.png') }}" class="icon" />
13+
<h3>Name my pet</h3>
14+
<form action="/" method="post">
15+
<input type="text" name="animal" placeholder="Enter an animal" required />
16+
<input type="submit" value="Generate names" />
17+
</form>
18+
{% if result %}
19+
<div class="result">{{ result }}</div>
20+
{% endif %}
21+
</body>

0 commit comments

Comments
 (0)