diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..f65d34c2c --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +FLASK_APP=app +FLASK_ENV=development +OPENAI_API_KEY= diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..83a1033d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +venv/ + +*.pyc +__pycache__/ + +instance/ + +.pytest_cache/ +.coverage +htmlcov/ + +dist/ +build/ +*.egg-info/ + +.DS_STORE + +.env diff --git a/README.md b/README.md new file mode 100644 index 000000000..ed2030343 --- /dev/null +++ b/README.md @@ -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://localhost:5000](http://localhost:5000)! For the full context behind this example app, check out the [tutorial](https://beta.openai.com/docs/quickstart). diff --git a/app.py b/app.py new file mode 100644 index 000000000..c682193f4 --- /dev/null +++ b/app.py @@ -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() + ) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..b5fc61620 --- /dev/null +++ b/requirements.txt @@ -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 diff --git a/static/dog.png b/static/dog.png new file mode 100644 index 000000000..d8bb6ca9c Binary files /dev/null and b/static/dog.png differ diff --git a/static/main.css b/static/main.css new file mode 100644 index 000000000..ccbad9038 --- /dev/null +++ b/static/main.css @@ -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; +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 000000000..6790d6aaa --- /dev/null +++ b/templates/index.html @@ -0,0 +1,21 @@ + + + OpenAI Quickstart + + + + + + +

Name my pet

+
+ + +
+ {% if result %} +
{{ result }}
+ {% endif %} +