Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevnayak committed Jan 28, 2022
0 parents commit 0a2e22b
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FLASK_APP=app
FLASK_ENV=development
OPENAI_API_KEY=
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
venv/

*.pyc
__pycache__/

instance/

.pytest_cache/
.coverage
htmlcov/

dist/
build/
*.egg-info/

.DS_STORE

.env
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# OpenAI API Quickstart - Python example app

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.

## Setup

1. If you don’t have Python installed, [install it from here](https://www.python.org/downloads/)

2. Clone this repository

3. Navigate into the project directory

```bash
$ cd openai-quickstart-python
```

4. Create a new virtual environment

```bash
$ python -m venv venv
$ . venv/bin/activate
```

5. Install the requirements

```bash
$ pip install -r requirements.txt
```

6. Make a copy of the example environment variables file

```bash
$ cp .env.example .env
```

7. Add your [API key](https://beta.openai.com/account/api-keys) to the newly created `.env` file

8. Run the app

```bash
$ flask run
```

You should now be able to access the app at [http:https://localhost:5000](http:https://localhost:5000)! For the full context behind this example app, check out the [tutorial](https://beta.openai.com/docs/quickstart).
35 changes: 35 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

import openai
from flask import Flask, redirect, render_template, request, url_for

app = Flask(__name__)
openai.api_key = os.getenv("OPENAI_API_KEY")


@app.route("/", methods=("GET", "POST"))
def index():
if request.method == "POST":
animal = request.form["animal"]
response = openai.Completion.create(
engine="text-davinci-001",
prompt=generate_prompt(animal),
temperature=0.6,
)
return redirect(url_for("index", result=response.choices[0].text))

result = request.args.get("result")
return render_template("index.html", result=result)


def generate_prompt(animal):
return """Suggest three names for an animal that is a superhero.
Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: {}
Names:""".format(
animal.capitalize()
)
25 changes: 25 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
autopep8==1.6.0
certifi==2021.10.8
charset-normalizer==2.0.7
click==8.0.3
et-xmlfile==1.1.0
Flask==2.0.2
idna==3.3
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
numpy==1.21.3
openai==0.11.0
openpyxl==3.0.9
pandas==1.3.4
pandas-stubs==1.2.0.35
pycodestyle==2.8.0
python-dateutil==2.8.2
python-dotenv==0.19.2
pytz==2021.3
requests==2.26.0
six==1.16.0
toml==0.10.2
tqdm==4.62.3
urllib3==1.26.7
Werkzeug==2.0.2
Binary file added static/dog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions static/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@font-face {
font-family: "ColfaxAI";
src: url(https://cdn.openai.com/API/fonts/ColfaxAIRegular.woff2)
format("woff2"),
url(https://cdn.openai.com/API/fonts/ColfaxAIRegular.woff) format("woff");
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: "ColfaxAI";
src: url(https://cdn.openai.com/API/fonts/ColfaxAIBold.woff2) format("woff2"),
url(https://cdn.openai.com/API/fonts/ColfaxAIBold.woff) format("woff");
font-weight: bold;
font-style: normal;
}
body,
input {
font-size: 16px;
line-height: 24px;
color: #353740;
font-family: "ColfaxAI", Helvetica, sans-serif;
}
body {
display: flex;
flex-direction: column;
align-items: center;
padding-top: 60px;
}
.icon {
width: 34px;
}
h3 {
font-size: 32px;
line-height: 40px;
font-weight: bold;
color: #202123;
margin: 16px 0 40px;
}
form {
display: flex;
flex-direction: column;
width: 320px;
}
input[type="text"] {
padding: 12px 16px;
border: 1px solid #10a37f;
border-radius: 4px;
margin-bottom: 24px;
}
::placeholder {
color: #8e8ea0;
opacity: 1;
}
input[type="submit"] {
padding: 12px 0;
color: #fff;
background-color: #10a37f;
border: none;
border-radius: 4px;
text-align: center;
cursor: pointer;
}
.result {
font-weight: bold;
margin-top: 40px;
}
21 changes: 21 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<head>
<title>OpenAI Quickstart</title>
<link
rel="shortcut icon"
href="{{ url_for('static', filename='dog.png') }}"
/>
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}" />
</head>

<body>
<img src="{{ url_for('static', filename='dog.png') }}" class="icon" />
<h3>Name my pet</h3>
<form action="/" method="post">
<input type="text" name="animal" placeholder="Enter an animal" required />
<input type="submit" value="Generate names" />
</form>
{% if result %}
<div class="result">{{ result }}</div>
{% endif %}
</body>

0 comments on commit 0a2e22b

Please sign in to comment.