Upload app.py
Browse filesA recipe builder app
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import openai
|
| 3 |
+
#from flask import redirect, render_template, request, url_for
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# with open("apikey.pkl", "rb") as f:
|
| 7 |
+
# apikey = pickle.load(f)
|
| 8 |
+
# print(apikey)
|
| 9 |
+
|
| 10 |
+
def get_open_ai_output(recipe_titles):
|
| 11 |
+
with open("apikey.pkl", "rb") as f:
|
| 12 |
+
apikey = pickle.load(f)
|
| 13 |
+
openai.api_key = apikey
|
| 14 |
+
response = openai.Completion.create(
|
| 15 |
+
model="text-davinci-003",
|
| 16 |
+
prompt=generate_prompt(recipe_titles),
|
| 17 |
+
temperature=0.98,
|
| 18 |
+
max_tokens = 4000
|
| 19 |
+
)
|
| 20 |
+
response = response.choices[0].text
|
| 21 |
+
return response
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def generate_prompt(recipe_titles):
|
| 26 |
+
return """Suggest a recipe title based on the food item inputted, then acting as a cookbook give the full recipe for the title suggested, include ingredients and instructions
|
| 27 |
+
|
| 28 |
+
Example:
|
| 29 |
+
|
| 30 |
+
Food: {}
|
| 31 |
+
Titles:""".format(
|
| 32 |
+
recipe_titles.capitalize()
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
#@app.route("/", methods=("GET", "POST"))
|
| 36 |
+
# def index():
|
| 37 |
+
# if request.method == "POST":
|
| 38 |
+
# recipe_titles = request.form["recipe_titles"]
|
| 39 |
+
# response = openai.Completion.create(
|
| 40 |
+
# model="text-davinci-003",
|
| 41 |
+
# prompt=generate_prompt(recipe_titles),
|
| 42 |
+
# temperature=0.98,
|
| 43 |
+
# max_tokens = 4000
|
| 44 |
+
# )
|
| 45 |
+
# return redirect(url_for("index", result=response.choices[0].text))
|
| 46 |
+
|
| 47 |
+
# result = request.args.get("result")
|
| 48 |
+
# return render_template("index.html", result=result)
|
| 49 |
+
|
| 50 |
+
#io1 = gr.Interface.load("huggingface/openai-gpt")
|
| 51 |
+
|
| 52 |
+
#io2 = gr.Interface.load("huggingface/CoffeeAddict93/gpt1-modest-proposal")
|
| 53 |
+
|
| 54 |
+
def inference(recipe_titles):
|
| 55 |
+
output = get_open_ai_output(recipe_titles)
|
| 56 |
+
return output
|
| 57 |
+
input = gr.Textbox(label="Food Ingredient",max_lines=1, placeholder = "Enter ONE food ingredient here")
|
| 58 |
+
output = gr.Textbox(label="Recipe")
|
| 59 |
+
|
| 60 |
+
with gr.Blocks(css = ".gradio-container {background-color: #E7ECF3}") as demo:
|
| 61 |
+
|
| 62 |
+
gr.Interface(
|
| 63 |
+
inference,
|
| 64 |
+
input,output,title = """
|
| 65 |
+
|
| 66 |
+
# **<span style="color:#3526A">Something Sweet...</span>**
|
| 67 |
+
|
| 68 |
+
""" ,
|
| 69 |
+
description = "**Generate different recipes from just ONE ingredient!**", allow_flagging="never")
|
| 70 |
+
gr.Examples(
|
| 71 |
+
[["Milk"], ["Butter"]],
|
| 72 |
+
input, output,
|
| 73 |
+
inference,
|
| 74 |
+
cache_examples= False)
|
| 75 |
+
demo.launch(enable_queue=True)
|
| 76 |
+
|
| 77 |
+
|