From 0a2e22b13723e097bfe5a0814b75df81ffc92dcd Mon Sep 17 00:00:00 2001 From: Rajeev Nayak Date: Fri, 28 Jan 2022 11:44:25 -0800 Subject: [PATCH] initial commit --- .env.example | 3 ++ .gitignore | 18 ++++++++++++ README.md | 44 +++++++++++++++++++++++++++++ app.py | 35 +++++++++++++++++++++++ requirements.txt | 25 ++++++++++++++++ static/dog.png | Bin 0 -> 1746 bytes static/main.css | 66 +++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 21 ++++++++++++++ 8 files changed, 212 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app.py create mode 100644 requirements.txt create mode 100644 static/dog.png create mode 100644 static/main.css create mode 100644 templates/index.html 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 0000000000000000000000000000000000000000..d8bb6ca9c3830baeba045c20092a6ab32504e369 GIT binary patch literal 1746 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