Skip to content

Commit

Permalink
# add comments throughout document to make it easier to understand wh…
Browse files Browse the repository at this point in the history
…at's going on at each step
  • Loading branch information
nemoteric committed Dec 20, 2022
1 parent 2f4c785 commit d18065f
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
openai.api_key = os.getenv("OPENAI_API_KEY")


# riplit key
# using replit method for getting API key
# openai.api_key = os.environ["OPENAI_API_KEY"]

@app.route("/", methods=("GET", "POST"))
def index():
# response to form submission
if request.method == "POST":
# input box value = "ond"
ond = request.form["ond"]
# get Completion response using the text from "prompt"
response = openai.Completion.create(
model="text-davinci-003",
prompt=extract_city_names(ond),
Expand All @@ -25,20 +28,36 @@ def index():
# presence_penalty=0.0,
# stop=["\n"],
)
# append the choices to the end of the index url
# e.g. /?result=+San+Diego%2C+San+Francisco
return redirect(url_for("index", result=response.choices[0].text))

# response to GET request (initial page load) or form submission redirect (GET)
# get the "result" query parameter from the url
result = request.args.get("result")
# get the "ond" query parameter from the url (if it exists)
if type(result) == str:
result = result.split(", ")

if result is not None:
o_url = generate_prompt_image(result[0])
d_url = generate_prompt_image(result[1])
# get the image urls
o_url = generate_prompt_image("Beautiful photo of " + result[0])
d_url = generate_prompt_image("Beautiful photo of " + result[1])
return render_template("index.html", result=', '.join(result), o_url=o_url, d_url=d_url)

# if no result, return the index page without any result
return render_template("index.html", result=result)


def generate_prompt_image(ond_input):
"""Returns a url for an image generated from the given prompt. Note urls expire after 1 hour."""
response = openai.Image.create(prompt=f"{ond_input}", n=1, size="256x256")
image_url = response['data'][0]['url']
return image_url


# ====================Prompts for GPT-3====================
def extract_city_names(ond):
"""Prompt that GPT3 will use to return the city names from the form input."""
return """Extract city names from the following prompts.
Prompt: Prompt: Traveling from Minneapolis to Denver
Expand All @@ -51,7 +70,8 @@ def extract_city_names(ond):
)


def generate_prompt(ond_input):
def extract_codes(ond_input):
"""Prompt that GPT3 will use to find the code for the given city."""
return """Get airport codes from the following prompts.
Prompt: Traveling from Minneapolis to Denver
Expand All @@ -62,10 +82,7 @@ def generate_prompt(ond_input):
Codes:""".format(ond_input).upper()


def generate_prompt_image(ond_input):
response = openai.Image.create(prompt=f"{ond_input}", n=1, size="256x256")
image_url = response['data'][0]['url']
return image_url

# =========================================================

app.run(host='0.0.0.0', port=81)
# for replit, use port 81
app.run(host='127.0.0.1', port=8080)

0 comments on commit d18065f

Please sign in to comment.