Skip to content

Commit

Permalink
feat: added v2/recommend
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya062003 committed Nov 12, 2024
1 parent 7885991 commit 5317dd3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.metaflow/
__pycache__/
__pycache__/
.env
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir metaflow pandas scikit-learn sentence-transformers fastapi uvicorn
RUN pip install --no-cache-dir metaflow pandas scikit-learn sentence-transformers fastapi uvicorn groq python-dotenv

EXPOSE 8080

Expand Down
68 changes: 68 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,54 @@
import pandas as pd
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
from groq import Groq
import uvicorn
import os
from dotenv import load_dotenv

load_dotenv()

api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("API key not set. Please set the GROQ_API_KEY in the .env file.")

client = Groq(api_key=api_key)

food_list = [
"Burger", "Cold Coffee", "Hot Coffee", "Veg Pizza", "Pav Bhaji", "Aloo Matar", "Samosa",
"Dum Aloo", "Doodhpak", "Chak Hao Kheer", "Kheer", "Dosa", "Idli", "Aloo Paratha",
"Chole Kulche", "Bhel", "Vada Pav", "Litti Chokha", "Mocktail", "Chocolate Pastries"
]

def get_groq_recommendations(food_item):
try:
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"""I will provide a food item from the following list:
["Burger", "Cold Coffee", "Hot Coffee", "Veg Pizza", "Pav Bhaji", "Aloo Matar", "Samosa",
"Dum Aloo", "Doodhpak", "Chak Hao Kheer", "Kheer", "Dosa", "Idli", "Aloo Paratha",
"Chole Kulche", "Bhel", "Vada Pav", "Litti Chokha", "Mocktail", "Chocolate Pastries"].
The food item I’m providing is "{food_item}".
Based on this item, return an array of other similar items from this list only.
Only include items from the provided list, and ensure they are relevant to "{food_item}".
Only give me the array no other text or anything just array. Only give response as array"""
},
],
model="llama3-8b-8192",
temperature=0.5,
max_tokens=50,
top_p=1,
stream=False,
stop=None,
)
return chat_completion.choices[0].message.content
except Exception as e:
print(f"Error fetching recommendations for {food_item}: {e}")
return []

app = FastAPI()
df = pd.read_csv("indian_food.csv")
Expand Down Expand Up @@ -36,5 +83,26 @@ def recommend(food_item: list[str]):

return {"recommended_food_items": [rec[0] for rec in sorted_recommendations]}

@app.post("/v2/recommend")
def recommendV2(food_items: list[str]):
if len(food_items) >= 3:
food_items = food_items[:3]
all_recommendations = []

for item in food_items:
recommendations = get_groq_recommendations(item)

try:
rec_list = eval(recommendations)
all_recommendations.extend(rec_list)
except:
continue

recommendation_counts = Counter(all_recommendations)
sorted_recommendations = sorted(recommendation_counts.items(), key=lambda x: x[1], reverse=True)

return {"recommended_food_items": [rec[0] for rec in sorted_recommendations]}


if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ sentence-transformers
uvicorn
seaborn
matplotlib
python-dotenv
groq

0 comments on commit 5317dd3

Please sign in to comment.