Santhosh54321 commited on
Commit
2acbd91
·
verified ·
1 Parent(s): e906233

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -47
app.py CHANGED
@@ -2,25 +2,23 @@ import streamlit as st
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")
15
  dish_name = ""
16
 
17
- for message in client.chat_completion(
18
- model="meta-llama/Llama-3.2-11B-Vision-Instruct",
19
  messages=[
20
  {
21
  "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'.",
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."},
25
  ],
26
  }
@@ -33,7 +31,7 @@ def identify_dish(image_bytes):
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
 
@@ -88,7 +86,7 @@ def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
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",
93
  messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}],
94
  max_tokens=500
@@ -96,40 +94,16 @@ def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
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": "llama-3.1-70b-versatile",
108
- "messages": [
109
- {"role": "system", "content": "You are a skilled chef with 10 years of experience."},
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
 
126
  # Sidebar for user input
127
  st.sidebar.title("User Input")
128
  image_file = st.sidebar.file_uploader("Upload an image of the dish", type=["jpeg", "png"])
129
- age = st.sidebar.number_input("Enter your age", min_value=1)
130
  gender = st.sidebar.selectbox("Select your gender", ["male", "female"])
131
- height_cm = st.sidebar.number_input("Enter your height (cm)", min_value=1.0)
132
- weight_kg = st.sidebar.number_input("Enter your weight (kg)", min_value=1.0)
133
  weight_goal = st.sidebar.selectbox("Weight goal", ["loss", "gain", "maintain"])
134
  activity_level = st.sidebar.selectbox("Activity level", ["sedentary", "light", "moderate", "active", "very active"])
135
  time_frame = st.sidebar.number_input("Time frame to achieve goal (months)", min_value=1)
@@ -169,16 +143,6 @@ if submit:
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
 
 
2
  import base64
3
  from huggingface_hub import InferenceClient
4
  import os
 
5
 
6
+ # Initialize Hugging Face Inference clients using tokens from environment variables
7
+ client_dish = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DISH"))
8
+ client_diet = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DIET"))
 
9
 
10
  # 1. Function to identify dish from image
11
  def identify_dish(image_bytes):
12
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
13
  dish_name = ""
14
 
15
+ for message in client_dish.chat_completion(
16
+ model="meta-llama/Llama-3-11B-Vision-Instruct",
17
  messages=[
18
  {
19
  "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'.",
20
  "content": [
21
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}},
22
  {"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."},
23
  ],
24
  }
 
31
 
32
  return dish_name.strip()
33
 
34
+ # 2. Function to get user inputs and calculate daily caloric needs
35
  def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months):
36
  bmi = weight_kg / ((height_cm / 100) ** 2)
37
 
 
86
  - Caloric Intake per Day: {calorie_intake_per_day} calories
87
  - Goal: {goal}
88
  """
89
+ response = client_diet.chat_completion(
90
  model="meta-llama/Meta-Llama-3-8B-Instruct",
91
  messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}],
92
  max_tokens=500
 
94
 
95
  return response.choices[0].message.content
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  # Streamlit App Title
98
  st.title("AI Diet Planner")
99
 
100
  # Sidebar for user input
101
  st.sidebar.title("User Input")
102
  image_file = st.sidebar.file_uploader("Upload an image of the dish", type=["jpeg", "png"])
103
+ age = st.sidebar.number_input("Enter your age", min_value=18)
104
  gender = st.sidebar.selectbox("Select your gender", ["male", "female"])
105
+ height_cm = st.sidebar.number_input("Enter your height (cm)", min_value=150.0)
106
+ weight_kg = st.sidebar.number_input("Enter your weight (kg)", min_value=50.0)
107
  weight_goal = st.sidebar.selectbox("Weight goal", ["loss", "gain", "maintain"])
108
  activity_level = st.sidebar.selectbox("Activity level", ["sedentary", "light", "moderate", "active", "very active"])
109
  time_frame = st.sidebar.number_input("Time frame to achieve goal (months)", min_value=1)
 
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