Files

1 line
4.4 KiB
JSON

{"repo": "chekkasahitya/recipe-genie", "files": {"app.py": {"5f42e4c9a579ec0552302b3fd7abdc9482d9d246": {"t": 1785674372.7395816, "n": 4143, "h": "d9a8717d0457c466", "c": "# app.py\nfrom flask import Flask, render_template, request, session, redirect, url_for\nimport os\nimport requests\nfrom dotenv import load_dotenv\nfrom PyPDF2 import PdfReader\n\n\napp = Flask(__name__)\napp.secret_key = os.urandom(24)\n\nload_dotenv()\napi_key = os.getenv(\"OPENAI_API_KEY\")\n\n# --- Load Recipe Context from PDFs ---\nRECIPE_PDFS = [\n \"italian_recipes.pdf\",\n \"mexican_recipes.pdf\",\n \"indian_recipes.pdf\"\n]\n\ndef read_pdf(file_path):\n try:\n pdf_reader = PdfReader(file_path)\n text = \"\"\n for page in pdf_reader.pages:\n page_text = page.extract_text()\n if page_text:\n text += page_text + \"\\n\"\n return text\n except Exception as e:\n print(f\"Error reading {file_path}: {str(e)}\")\n return \"\"\n\nrecipe_documents = \"\"\nfor pdf_file in RECIPE_PDFS:\n if os.path.exists(pdf_file):\n recipe_documents += read_pdf(pdf_file) + \"\\n\"\n else:\n print(f\"Warning: {pdf_file} not found!\")\n\nif not recipe_documents.strip():\n recipe_documents = \"No specific recipe examples available.\"\n\n@app.route('/')\ndef landing():\n return render_template('landing.html')\n\n@app.route('/form', methods=['GET', 'POST'])\ndef form():\n if request.method == 'POST':\n ingredients = request.form.get('ingredients')\n cuisine_type = request.form.get('cuisine_type')\n difficulty_level = request.form.get('difficulty_level')\n\n session['ingredients'] = ingredients\n session['cuisine_type'] = cuisine_type\n session['difficulty_level'] = difficulty_level\n\n return generate_recipe_and_redirect(ingredients, cuisine_type, difficulty_level)\n\n return render_template('index.html')\n\ndef generate_recipe_and_redirect(ingredients, cuisine_type, difficulty_level):\n user_prompt = (\n f\"Invent a {difficulty_level.lower()} {cuisine_type.lower()} recipe \"\n f\"using these ingredients: {ingredients}. \"\n \"Provide:\\n\"\n \"- A creative recipe name\\n\"\n \"- List of ingredients with approximate quantities\\n\"\n \"- Step-by-step cooking instructions\\n\"\n \"- Preparation time if possible\\n\"\n \"Return as clean plain text, no Markdown.\"\n )\n\n system_prompt = (\n \"You are DishGenie, a magical chef assistant. \"\n \"Use creativity to invent delicious recipes, while referring to the example recipes below \"\n \"for inspiration and formatting.\\n\\n\"\n f\"Example Recipes:\\n{recipe_documents}\"\n )\n\n try:\n url = \"https://openrouter.ai/api/v1/chat/completions\"\n headers = {\n \"Authorization\": f\"Bearer {api_key}\",\n \"Content-Type\": \"application/json\"\n }\n payload = {\n \"model\": \"openai/gpt-4o-mini\",\n \"messages\": [\n {\"role\": \"system\", \"content\": system_prompt},\n {\"role\": \"user\", \"content\": user_prompt}\n ],\n \"top_p\": 0.8,\n \"temperature\": 0.5\n }\n\n response = requests.post(url, headers=headers, json=payload)\n response.raise_for_status()\n result = response.json()\n recipe = result['choices'][0]['message']['content']\n session['recipe'] = recipe\n session.pop('error', None)\n return redirect(url_for('result'))\n\n except Exception as e:\n session['error'] = str(e)\n return redirect(url_for('result'))\n\n@app.route('/regenerate')\ndef regenerate():\n ingredients = session.get('ingredients')\n cuisine = session.get('cuisine_type')\n difficulty = session.get('difficulty_level')\n\n if ingredients and cuisine and difficulty:\n return generate_recipe_and_redirect(ingredients, cuisine, difficulty)\n else:\n return redirect(url_for('form'))\n\n@app.route('/result')\ndef result():\n recipe = session.get('recipe')\n error = session.get('error')\n return render_template('result.html', recipe=recipe, error=error, background='ingredients_background.jpg')\n\n\nif __name__ == \"__main__\":\n port = int(os.environ.get(\"PORT\", 5001)) # Get the port from the environment\n app.run(host=\"0.0.0.0\", port=port) # Bind to 0.0.0.0 for Render\n\n"}}}}