Spaces:
Build error
Build error
Upload 3 files
Browse files- backend.py +51 -0
- fronted.py +98 -0
- requirements.txt +4 -0
backend.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Function to get a recipe from Groq LLM
|
| 13 |
+
def get_recipe_from_llm(query):
|
| 14 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 15 |
+
|
| 16 |
+
# Construct message format
|
| 17 |
+
messages = [{"role": "user", "content": [{"type": "text", "text": query}]}]
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
chat_completion = client.chat.completions.create(
|
| 21 |
+
model="llama3-8b-8192",
|
| 22 |
+
messages=messages,
|
| 23 |
+
)
|
| 24 |
+
return chat_completion.choices[0].message.content
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Error: {str(e)}"
|
| 28 |
+
|
| 29 |
+
@app.route("/get_recipe", methods=["POST"])
|
| 30 |
+
def get_recipe():
|
| 31 |
+
try:
|
| 32 |
+
data = request.json
|
| 33 |
+
ingredients = data.get("ingredients", "").strip()
|
| 34 |
+
preference = data.get("preference", "").strip()
|
| 35 |
+
|
| 36 |
+
if not ingredients:
|
| 37 |
+
return jsonify({"error": "Ingredients are required"}), 400
|
| 38 |
+
|
| 39 |
+
# Build query for LLM
|
| 40 |
+
query = f"Give me a recipe using {ingredients}."
|
| 41 |
+
if preference:
|
| 42 |
+
query += f" The recipe should be {preference}."
|
| 43 |
+
|
| 44 |
+
result = get_recipe_from_llm(query)
|
| 45 |
+
return jsonify({"recipe": result})
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return jsonify({"error": str(e)}), 500 # Return error as JSON
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
app.run(debug=True, port=5000)
|
fronted.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Flask API URL
|
| 5 |
+
FLASK_API_URL = "http://127.0.0.1:5000/get_recipe"
|
| 6 |
+
|
| 7 |
+
st.title("π½οΈ RecipeBot - AI Chef Assistant")
|
| 8 |
+
|
| 9 |
+
# Ingredients input
|
| 10 |
+
ingredients = st.text_area("Enter ingredients (comma-separated)", "")
|
| 11 |
+
|
| 12 |
+
# Right-aligned Hover Tooltip using HTML & CSS
|
| 13 |
+
st.markdown(
|
| 14 |
+
"""
|
| 15 |
+
<style>
|
| 16 |
+
.tooltip-container {
|
| 17 |
+
display: flex;
|
| 18 |
+
justify-content: space-between;
|
| 19 |
+
align-items: center;
|
| 20 |
+
}
|
| 21 |
+
.tooltip {
|
| 22 |
+
position: relative;
|
| 23 |
+
display: inline-block;
|
| 24 |
+
cursor: pointer;
|
| 25 |
+
color: blue;
|
| 26 |
+
font-weight: bold;
|
| 27 |
+
}
|
| 28 |
+
.tooltip .tooltip-text {
|
| 29 |
+
visibility: hidden;
|
| 30 |
+
width: 250px;
|
| 31 |
+
background-color: #f1f1f1;
|
| 32 |
+
color: black;
|
| 33 |
+
text-align: left;
|
| 34 |
+
padding: 8px;
|
| 35 |
+
border-radius: 5px;
|
| 36 |
+
position: absolute;
|
| 37 |
+
z-index: 1;
|
| 38 |
+
top: 100%;
|
| 39 |
+
right: 0;
|
| 40 |
+
opacity: 0;
|
| 41 |
+
transition: opacity 0.3s;
|
| 42 |
+
}
|
| 43 |
+
.tooltip:hover .tooltip-text {
|
| 44 |
+
visibility: visible;
|
| 45 |
+
opacity: 1;
|
| 46 |
+
}
|
| 47 |
+
</style>
|
| 48 |
+
|
| 49 |
+
<div class="tooltip-container">
|
| 50 |
+
<label style="font-weight: bold;">Enter your dietary preference/restriction:</label>
|
| 51 |
+
<span class="tooltip">β
|
| 52 |
+
<span class="tooltip-text">
|
| 53 |
+
<b>Dietary Options:</b><br>
|
| 54 |
+
π₯¦ Vegetarian<br>
|
| 55 |
+
π Non-Vegetarian<br>
|
| 56 |
+
π Gluten-Free<br>
|
| 57 |
+
π₯ Dairy-Free<br>
|
| 58 |
+
π₯© Low-Carb<br>
|
| 59 |
+
π³ Low-Fat<br>
|
| 60 |
+
π₯ Keto<br>
|
| 61 |
+
π Paleo<br>
|
| 62 |
+
π₯ Nut-Free<br>
|
| 63 |
+
π Halal<br>
|
| 64 |
+
β‘οΈ Kosher<br>
|
| 65 |
+
β Others (Specify Below)
|
| 66 |
+
</span>
|
| 67 |
+
</span>
|
| 68 |
+
</div>
|
| 69 |
+
""",
|
| 70 |
+
unsafe_allow_html=True
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Dietary preference input
|
| 74 |
+
preference = st.text_input("")
|
| 75 |
+
|
| 76 |
+
if st.button("Get Recipe"):
|
| 77 |
+
if not ingredients:
|
| 78 |
+
st.warning("β οΈ Please enter ingredients.")
|
| 79 |
+
else:
|
| 80 |
+
st.write("β³ Fetching recipe...")
|
| 81 |
+
|
| 82 |
+
response = requests.post(
|
| 83 |
+
FLASK_API_URL,
|
| 84 |
+
json={
|
| 85 |
+
"ingredients": ingredients,
|
| 86 |
+
"preference": preference
|
| 87 |
+
}
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
data = response.json()
|
| 92 |
+
if "recipe" in data:
|
| 93 |
+
st.success("β
Recipe Generated!")
|
| 94 |
+
st.write(data["recipe"])
|
| 95 |
+
else:
|
| 96 |
+
st.error(f"β Error: {data.get('error', 'Unknown error')}")
|
| 97 |
+
except requests.exceptions.JSONDecodeError:
|
| 98 |
+
st.error("β Failed to parse response. Check API logs.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
python-dotenv
|
| 3 |
+
groq
|
| 4 |
+
streamlit
|