import os import openai import gradio import json from pathlib import Path from bs4 import BeautifulSoup openai.api_key = os.getenv("OPENAI_API_KEY") content_input = "Format the recipe, given the format provided. You must return an HTML:" html_template = '''
Mom's World Famous Banana Bread This classic banana bread recipe comes from my mom. 3 or 4 ripe bananas, smashed 3/4 cup of sugar 1 - Preheat the oven to 350 degrees. 2 - Mix in the ingredients in a bowl.
''' content_input+=html_template messages = [{"role": "system", "content": content_input}] # create a static directory to store the static files static_dir = Path('./static') static_dir.mkdir(parents=True, exist_ok=True) def CustomChatGPT(recipe): messages.append({"role": "user", "content": recipe}) response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) ChatGPT_reply = response["choices"][0]["message"]["content"] file_name = "output.html" file_path = static_dir / file_name soup = BeautifulSoup(ChatGPT_reply, 'html.parser') recipe_name = soup.find(attrs={"itemprop": "name"}) recipe_name_str = recipe_name.text ingredients_html = soup.find_all(attrs={"itemprop": "recipeIngredient"}) ingredients = [item.text.strip() for item in ingredients_html] ingredients_str = "\n".join(ingredients) steps_html = soup.find_all(attrs={"itemprop": "recipeInstructions"}) steps = [item.text.strip() for item in steps_html] steps_str = "\n".join(steps) with open(file_path, "w") as file: file.write(ChatGPT_reply) return recipe_name_str, ingredients_str, steps_str, "Import" demo = gradio.Interface( fn=CustomChatGPT, inputs=gradio.Textbox(label="Recipe", lines=2, placeholder="Recipe here..."), outputs=[gradio.Textbox(label="Recipe Name", lines=1, placeholder="Recipe Name..."), gradio.Textbox(label="Ingredients", lines=2, placeholder="Ingredients..."),gradio.Textbox(label="Preparation Steps", lines=2, placeholder="Steps..."),gradio.HTML()], title="Recipe Cleaner" ) # demo.launch() demo.launch(share=True)