Santhosh54321 commited on
Commit
7d1bfcb
·
verified ·
1 Parent(s): dae48dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -5
app.py CHANGED
@@ -2,10 +2,13 @@ import streamlit as st
2
  import base64
3
  from huggingface_hub import InferenceClient
4
  import os
 
5
 
6
- # Initialize Hugging Face Inference client using token from environment variables
7
  client = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DISH"))
8
  client1 = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DIET"))
 
 
9
  # 1. Function to identify dish from image
10
  def identify_dish(image_bytes):
11
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
@@ -15,7 +18,7 @@ def identify_dish(image_bytes):
15
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
16
  messages=[
17
  {
18
- "role": "You are a highly specialized food identification AI with extensive knowledge of global cuisines. Your sole task is to accurately identify dishes from images. Adhere strictly to these guidelines:\n1. Analyze the image thoroughly, focusing on ingredients, presentation, and cultural context.\n2. Provide ONLY the name of the main dish or dishes visible. Do not list individual ingredients or components.\n3. Use the most specific and widely recognized name for the dish.\n4. If multiple distinct dishes are present, list them separated by commas.\n5. If you cannot identify a dish with high confidence (>90%), respond with 'Unidentified dish'.\n6. Do not provide any explanations, descriptions, or additional commentary.\n7. Respond in a concise, list-like format.\nYour response should contain nothing but the dish name(s) or 'Unidentified dish'.",
19
  "content": [
20
  {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}" }},
21
  {"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."},
@@ -30,7 +33,7 @@ def identify_dish(image_bytes):
30
 
31
  return dish_name.strip()
32
 
33
- # 2. Function to get user inputs and calculate daily caloric needs
34
  def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months):
35
  bmi = weight_kg / ((height_cm / 100) ** 2)
36
 
@@ -78,13 +81,12 @@ def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_l
78
  # 3. Function to generate diet plan
79
  def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
80
  user_input = f"""
81
- You are a certified Dietitian with 20 years of experience. Based on the following input, create an Indian diet plan that fits within the calculated calorie intake and assesses if the given dish is suitable for the user's goal.
82
 
83
  Input:
84
  - Dish Name: {dish_name}
85
  - Caloric Intake per Day: {calorie_intake_per_day} calories
86
  - Goal: {goal}
87
-
88
  """
89
  response = client1.chat_completion(
90
  model="meta-llama/Meta-Llama-3-8B-Instruct",
@@ -94,6 +96,30 @@ def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
94
 
95
  return response.choices[0].message.content
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  # Streamlit App Title
98
  st.title("AI Diet Planner")
99
 
@@ -143,6 +169,16 @@ if submit:
143
  st.write("#### Diet Plan Based on Dish & Goal:")
144
  st.markdown(f"<div style='background-color: #d1ecf1; color: #0c5460; padding: 10px; border-radius: 10px;'>{diet_plan}</div>", unsafe_allow_html=True)
145
 
 
 
 
 
 
 
 
 
 
 
146
  else:
147
  st.error("Please upload a valid image in JPEG or PNG format.")
148
 
 
2
  import base64
3
  from huggingface_hub import InferenceClient
4
  import os
5
+ import requests
6
 
7
+ # Initialize Hugging Face Inference client using tokens from environment variables
8
  client = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DISH"))
9
  client1 = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DIET"))
10
+ groq_api_key = os.getenv("API_Groq")
11
+
12
  # 1. Function to identify dish from image
13
  def identify_dish(image_bytes):
14
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
 
18
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
19
  messages=[
20
  {
21
+ "role": "You are a highly specialized food identification AI...",
22
  "content": [
23
  {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}" }},
24
  {"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."},
 
33
 
34
  return dish_name.strip()
35
 
36
+ # 2. Function to calculate daily caloric needs
37
  def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months):
38
  bmi = weight_kg / ((height_cm / 100) ** 2)
39
 
 
81
  # 3. Function to generate diet plan
82
  def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
83
  user_input = f"""
84
+ You are a certified Dietitian with 20 years of experience. Based on the following input, create an Indian diet plan...
85
 
86
  Input:
87
  - Dish Name: {dish_name}
88
  - Caloric Intake per Day: {calorie_intake_per_day} calories
89
  - Goal: {goal}
 
90
  """
91
  response = client1.chat_completion(
92
  model="meta-llama/Meta-Llama-3-8B-Instruct",
 
96
 
97
  return response.choices[0].message.content
98
 
99
+ # 4. Function to generate a recipe using Groq API
100
+ def generate_recipe(recipe_name):
101
+ url = "https://api.groq.com/openai/v1/chat/completions"
102
+ headers = {
103
+ "Authorization": f"Bearer {groq_api_key}",
104
+ "Content-Type": "application/json"
105
+ }
106
+ data = {
107
+ "model": "gpt-3.5-turbo",
108
+ "messages": [
109
+ {"role": "system", "content": "You are a skilled chef."},
110
+ {"role": "user", "content": f"Create a detailed recipe for {recipe_name}"}
111
+ ]
112
+ }
113
+
114
+ response = requests.post(url, headers=headers, json=data)
115
+
116
+ if response.status_code == 200:
117
+ result = response.json()
118
+ generated_recipe = result['choices'][0]['message']['content']
119
+ return generated_recipe
120
+ else:
121
+ return f"Error: {response.status_code}, {response.text}"
122
+
123
  # Streamlit App Title
124
  st.title("AI Diet Planner")
125
 
 
169
  st.write("#### Diet Plan Based on Dish & Goal:")
170
  st.markdown(f"<div style='background-color: #d1ecf1; color: #0c5460; padding: 10px; border-radius: 10px;'>{diet_plan}</div>", unsafe_allow_html=True)
171
 
172
+ # Step 4: Ask for recipe generation
173
+ st.markdown("<hr>", unsafe_allow_html=True)
174
+ recipe_option = st.radio("Would you like a recipe for a dish?", ["No", "Yes"])
175
+
176
+ if recipe_option == "Yes":
177
+ recipe_name = st.text_input("Enter the dish name for the recipe:")
178
+ if st.button("Generate Recipe"):
179
+ recipe = generate_recipe(recipe_name)
180
+ st.write("### Generated Recipe:")
181
+ st.markdown(f"<div style='background-color: #fff3cd; color: #856404; padding: 10px; border-radius: 10px;'>{recipe}</div>", unsafe_allow_html=True)
182
  else:
183
  st.error("Please upload a valid image in JPEG or PNG format.")
184