Ritesh-hf commited on
Commit
7e7a010
·
1 Parent(s): 0bc3fb8

remove sockets

Browse files
Files changed (5) hide show
  1. .DS_Store +0 -0
  2. Dockerfile +9 -4
  3. app.py +308 -113
  4. demo.py +269 -150
  5. demo2.py +410 -100
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
Dockerfile CHANGED
@@ -1,6 +1,4 @@
1
- # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
- # you will also find guides on how best to write your Dockerfile
3
-
4
  FROM python:3.10
5
 
6
  # Install required system dependencies for OpenCV
@@ -27,5 +25,12 @@ RUN pip install --no-cache-dir -r requirements.txt
27
  # Copy the rest of the application code
28
  COPY --chown=user . /app
29
 
30
- # Set the command to start the application
 
 
 
 
 
 
 
31
  CMD ["gunicorn", "-b", "0.0.0.0:7860", "-k", "eventlet", "app:app"]
 
1
+ # Start with a base Python image
 
 
2
  FROM python:3.10
3
 
4
  # Install required system dependencies for OpenCV
 
25
  # Copy the rest of the application code
26
  COPY --chown=user . /app
27
 
28
+ # Set the environment variable for Flask
29
+ ENV FLASK_APP=app.py
30
+ ENV FLASK_ENV=production
31
+
32
+ # Expose the port that the Flask app runs on
33
+ EXPOSE 7860
34
+
35
+ # Set the command to start the application using Gunicorn with Eventlet
36
  CMD ["gunicorn", "-b", "0.0.0.0:7860", "-k", "eventlet", "app:app"]
app.py CHANGED
@@ -27,9 +27,9 @@ from langchain_core.prompts import ChatPromptTemplate
27
  from langchain_groq import ChatGroq
28
 
29
  from dotenv import load_dotenv
30
- from flask import Flask, request, render_template
31
  from flask_cors import CORS
32
- from flask_socketio import SocketIO, emit
33
 
34
  import json
35
  from openai import OpenAI
@@ -51,13 +51,14 @@ os.environ["TOKENIZERS_PARALLELISM"] = 'true'
51
  app = Flask(__name__)
52
  CORS(app)
53
  app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024
54
- socketio = SocketIO(app, cors_allowed_origins="*", logger=True, max_http_buffer_size=1024 * 1024 * 1024)
55
  app.config['SECRET_KEY'] = SECRET_KEY
56
 
57
-
58
  # Initialize LLM
59
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
60
 
 
 
 
61
  # Initialize Router
62
  router = ChatGroq(model="llama-3.2-3b-preview", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
63
 
@@ -77,7 +78,6 @@ class StoppingCriteriaSub(StoppingCriteria):
77
  for stop in self.stops:
78
  if torch.all(input_ids[:, -len(stop):] == stop).item():
79
  return True
80
-
81
  return False
82
 
83
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -128,11 +128,15 @@ model = blip_embs(
128
 
129
  model = model.to(device)
130
  model.eval()
 
 
131
 
132
  transform = transform_test(384)
133
 
 
134
  df = pd.read_json("my_recipes.json")
135
 
 
136
  tar_img_feats = []
137
  for _id in df["id_"].tolist():
138
  tar_img_feats.append(torch.load("./datasets/sidechef/blip-embs-large/{:07d}.pth".format(_id)).unsqueeze(0))
@@ -141,7 +145,7 @@ tar_img_feats = torch.cat(tar_img_feats, dim=0)
141
 
142
  class Chat:
143
 
144
- def __init__(self, model, transform, dataframe, tar_img_feats, device='cuda:0', stopping_criteria=None):
145
  self.device = device
146
  self.model = model
147
  self.transform = transform
@@ -179,30 +183,209 @@ class Chat:
179
 
180
 
181
  chat = Chat(model,transform,df,tar_img_feats, device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
 
184
  def answer_generator(formated_input, session_id):
185
  # QA system prompt and chain
186
  qa_system_prompt = """
187
- You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on provide context ans user query.
188
- Operational Guidelines:
189
- 1. Input Structure:
190
- - Context: You may receive contextual information related to recipes, such as specific data sets, user preferences, dietary restrictions, or previously selected dishes.
191
- - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient substitutions, cooking instructions, and more.
192
- 2. Response Strategy:
193
- - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance.
194
- - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, kindly state that you do not have require information.
195
- Core Functionalities:
196
- - Nutritional Information: Accurately provide nutritional values for each recipe, including calories, macronutrients (proteins, fats, carbohydrates), and essential vitamins and minerals, using contextual data when available.
197
- - Ingredient Details: List all ingredients required for recipes, including substitute options for dietary restrictions or ingredient availability, utilizing context when relevant.
198
- - Step-by-Step Cooking Instructions: Deliver clear, easy-to-follow instructions for preparing and cooking meals, informed by any provided contextual data.
199
- - Recipe Recommendations: Suggest dishes based on user preferences, dietary restrictions, available ingredients, and contextual data if provided.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  Additional Instructions:
201
  - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
202
- - Clarity and Coherence: Ensure that all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
203
- - Substitute Suggestions: When suggesting ingredient substitutes, consider user preferences and dietary restrictions outlined in the context or user query.
204
  - Dynamic Adaptation: Adapt your responses dynamically based on whether the context is relevant to the user's current request, ensuring optimal use of available information.
205
- Don't mention about context in the response, format the answer in a natural and friendly way.
 
206
  Context:
207
  {context}
208
  """
@@ -238,8 +421,8 @@ def router_node(query):
238
  # Prompt
239
  router_instructions = """You are an expert at determining the appropriate task for a user’s question based on chat history and the current query context. You have two available tasks:
240
 
241
- 1. Retrieval: Fetch information based on user's chat history and current query.
242
- 2. Recommendation/Suggestion: Recommend recipes to users based on the query.
243
 
244
  Return a JSON response with a single key named “task” indicating either “retrieval” or “recommendation” based on your decision.
245
  """
@@ -264,14 +447,14 @@ def recommendation_node(query):
264
  "recipe_time": integer,
265
  "recipe_yields": string,
266
  "recipe_ingredients": list of ingredients,
267
- "recipe_instructions": list of instruections,
268
  "recipe_image": string,
269
  "blogger": string,
270
- "recipe_nutrients": JSON object with key value pairs such as "protein: 10g",
271
- "tags": list of tags related to recipe
272
  } \n
273
 
274
- Here is the example of an recipe json object from the JSON data: \n
275
  {
276
  "recipe_name": "Asian Potato Salad with Seven Minute Egg",
277
  "recipe_time": 0,
@@ -321,19 +504,20 @@ def recommendation_node(query):
321
  ]
322
  } \n
323
 
324
- Based on the user query, provide a Python function to filter the JSON data. The output of the function should be a list of json objects. \n
325
 
326
  Recipe filtering instructions:
327
- - If a user asked for the highest nutrient recipe such as "high protein or high calories" then filtered recipes should be the top highest recipes from all the recipes with high nutrient.
328
- - sort or rearrange recipes based which recipes are more appropriate for the user.
 
329
 
330
  Your output instructions:
331
- - The function name should be filter_recipes. The input to the function should be file name.
332
  - The length of output recipes should not be more than 6.
333
- - Only give me output function. Do not call the function.
334
- - Give the python function as a key named "code" in a json format.
335
- - Do not include any other text with the output, only give python code.
336
- - If you do not follow the above given instructions, the chat may be terminated.
337
  """
338
  max_tries = 3
339
  while True:
@@ -391,59 +575,7 @@ def answer_formatter_node(question, context):
391
  return res
392
 
393
  CURR_CONTEXT = ''
394
-
395
- # @spaces.GPU
396
- def get_answer(image=[], message='', sessionID='abc123'):
397
- global CURR_CONTEXT
398
- if len(image) > 0:
399
- try:
400
- # Process the image and message here
401
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
402
- chat = Chat(model,transform,df,tar_img_feats, device)
403
- chat.encode_image(image)
404
- data = chat.ask()
405
- CURR_CONTEXT = data
406
- formated_input = {
407
- 'input': message,
408
- 'context': data
409
- }
410
- response = answer_generator(formated_input, session_id=sessionID)
411
- except Exception as e:
412
- print(e)
413
- response = {'content':"An error occurred while processing your request."}
414
- elif len(image) == 0 and message is not None:
415
- print("I am here")
416
- task = router_node(message)
417
- if task == 'retrieval':
418
- recipes = recommendation_node(message)
419
- print(recipes)
420
- if not recipes:
421
- response = {'content':"An error occurred while processing your request."}
422
- response = answer_formatter_node(message, recipes)
423
- else:
424
- formated_input = {
425
- 'input': message,
426
- 'context': CURR_CONTEXT
427
- }
428
- response = answer_generator(formated_input, session_id=sessionID)
429
-
430
- return response
431
-
432
- # Function to handle WebSocket connection
433
- @socketio.on('ping')
434
- def handle_connect():
435
- emit('Ping-return', {'message': 'Connected'}, room=request.sid)
436
-
437
-
438
- # Function to handle WebSocket connection
439
- @socketio.on('connect')
440
- def handle_connect():
441
- print(f"Client connected: {request.sid}")
442
-
443
- # Function to handle WebSocket disconnection
444
- @socketio.on('disconnect')
445
- def handle_disconnect():
446
- print(f"Client disconnected: {request.sid}")
447
 
448
  import json
449
  import base64
@@ -454,12 +586,12 @@ import torchvision.transforms as transforms
454
  # Dictionary to store incomplete image data by session
455
  session_store = {}
456
 
457
- @socketio.on('message')
458
  def handle_message(data):
459
  global session_store
460
  global CURR_CONTEXT
 
 
461
  context = "No data available"
462
- session_id = request.sid
463
  if session_id not in session_store:
464
  session_store[session_id] = {'image_data': b"", 'message': None, 'image_received': False}
465
 
@@ -474,8 +606,7 @@ def handle_message(data):
474
 
475
  except Exception as e:
476
  print(f"Error processing image chunk: {str(e)}")
477
- emit('response', "An error occurred while receiving the image chunk.", room=session_id)
478
- return
479
 
480
  if session_store[session_id]['image_data'] or session_store[session_id]['message']:
481
  try:
@@ -497,12 +628,11 @@ def handle_message(data):
497
  }
498
  # Invoke question_answer_chain and stream the response
499
  response = answer_generator(formated_input, session_id=session_id)
500
- emit('response', response, room=session_id)
501
 
502
  except Exception as e:
503
  print(f"Error processing image or message: {str(e)}")
504
- emit('response', "An error occurred while processing your request.", room=session_id)
505
- return
506
  finally:
507
  # Clear session data after processing
508
  session_store.pop(session_id, None)
@@ -516,18 +646,15 @@ def handle_message(data):
516
  'context': json.dumps(CURR_CONTEXT)
517
  }
518
  response = answer_generator(formated_input, session_id=session_id)
519
- emit('response', response, room=session_id)
 
520
  else:
521
  response = recommendation_node(message)
522
- print(response)
523
  # response = answer_formatter_node(message, recipes)
524
  if response is None:
525
  response = {'content':"An error occurred while processing your request."}
526
-
527
- emit('json_response', response, room=session_id)
528
- session_store.pop(session_id, None)
529
-
530
-
531
 
532
  import requests
533
  from PIL import Image
@@ -535,7 +662,6 @@ import numpy as np
535
  from io import BytesIO
536
 
537
  def download_image_to_numpy(url):
538
- print("Image URL: ", url)
539
  # Send a GET request to the URL to download the image
540
  response = requests.get(url)
541
 
@@ -551,21 +677,90 @@ def download_image_to_numpy(url):
551
  else:
552
  raise Exception(f"Failed to download image. Status code: {response.status_code}")
553
 
554
- @socketio.on('example')
555
  def handle_message(data):
 
 
556
  img_url = data['img_url']
557
  message = data['message']
558
- session_id = request.sid
559
  image_array = download_image_to_numpy(img_url)
560
- response = get_answer(image=image_array, message=message, sessionID=request.sid)
561
- emit('response', response, room=session_id)
562
  return response
563
 
564
- # Home route
565
- @app.route("/")
566
- def index_view():
567
- return render_template('chat.html')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
 
569
  # Main function to run the app
570
  if __name__ == '__main__':
571
- socketio.run(app, debug=False)
 
27
  from langchain_groq import ChatGroq
28
 
29
  from dotenv import load_dotenv
30
+ from flask import Flask, request, render_template, jsonify
31
  from flask_cors import CORS
32
+
33
 
34
  import json
35
  from openai import OpenAI
 
51
  app = Flask(__name__)
52
  CORS(app)
53
  app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024
 
54
  app.config['SECRET_KEY'] = SECRET_KEY
55
 
 
56
  # Initialize LLM
57
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
58
 
59
+ # JSON response LLM
60
+ json_llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
61
+
62
  # Initialize Router
63
  router = ChatGroq(model="llama-3.2-3b-preview", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
64
 
 
78
  for stop in self.stops:
79
  if torch.all(input_ids[:, -len(stop):] == stop).item():
80
  return True
 
81
  return False
82
 
83
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
128
 
129
  model = model.to(device)
130
  model.eval()
131
+ print("Model Loaded !")
132
+ print("="*50)
133
 
134
  transform = transform_test(384)
135
 
136
+ print("Loading Data")
137
  df = pd.read_json("my_recipes.json")
138
 
139
+ print("Loading Target Embedding")
140
  tar_img_feats = []
141
  for _id in df["id_"].tolist():
142
  tar_img_feats.append(torch.load("./datasets/sidechef/blip-embs-large/{:07d}.pth".format(_id)).unsqueeze(0))
 
145
 
146
  class Chat:
147
 
148
+ def __init__(self, model, transform, dataframe, tar_img_feats, device='cuda', stopping_criteria=None):
149
  self.device = device
150
  self.model = model
151
  self.transform = transform
 
183
 
184
 
185
  chat = Chat(model,transform,df,tar_img_feats, device)
186
+ print("Chat Initialized !")
187
+
188
+ import secrets
189
+ import string
190
+
191
+ def generate_session_key():
192
+ characters = string.ascii_letters + string.digits
193
+ session_key = ''.join(secrets.choice(characters) for _ in range(8))
194
+ return session_key
195
+
196
+
197
+ def json_answer_generator(user_query, context):
198
+ system_prompt = """
199
+ Given a recipe context in JSON format, respond to user queries by extracting and returning the requested information in JSON format with an additional `"header"` key containing a response starter. Use the following rules:
200
+
201
+ 1. **Recipe Information Extraction**:
202
+ - If the user query explicitly requests specific recipe data (e.g., ingredients, nutrients, or instructions), return only those JSON objects from the provided recipe context.
203
+ - For example, if the user asks, “What are the ingredients?” or “Show me the nutrient details,” your output should be limited to only the requested JSON objects (e.g., `recipe_ingredients`, `recipe_nutrients`).
204
+ - Include `"header": "Here is the information you requested:"` at the start of each response.
205
+
206
+ 2. **Multiple Information Points**:
207
+ - If a user query asks for more than one piece of information, return each requested JSON object from the recipe context in a combined JSON response.
208
+ - For example, if the query is “Give me the ingredients and instructions,” the output should include both `recipe_ingredients` and `recipe_instructions` objects.
209
+ - Include `"header": "Here is the information you requested:"` at the start of each response.
210
+
211
+ 3. **Non-Specific Recipe Information**:
212
+ - If the query does not directly refer to recipe data but instead asks for a general response based on the context, return a JSON object with a single key `"content"` and a descriptive response as its value.
213
+ - Include `"header": "Here is a suggestion based on the recipe:"` as the response starter.
214
+ - For example, if the query is “How can I use this recipe for a healthy lunch?” return a response like:
215
+ ```json
216
+ {
217
+ "header": "Here is a suggestion based on the recipe:",
218
+ "content": "This Asian Potato Salad with Seven Minute Egg is a nutritious and light option, ideal for a balanced lunch. It provides protein and essential nutrients with low calories."
219
+ }
220
+ ```
221
+
222
+ **Example Context**:
223
+ ```json
224
+ {
225
+ "recipe_name": "Asian Potato Salad with Seven Minute Egg",
226
+ "recipe_time": 0,
227
+ "recipe_yields": "4 servings",
228
+ "recipe_ingredients": [
229
+ "2 1/2 cup Multi-Colored Fingerling Potato",
230
+ "3/4 cup Celery",
231
+ "1/4 cup Red Onion",
232
+ "2 tablespoon Fresh Parsley",
233
+ "1/3 cup Mayonnaise",
234
+ "1 tablespoon Chili Garlic Sauce",
235
+ "1 teaspoon Hoisin Sauce",
236
+ "1 splash Soy Sauce",
237
+ "to taste Salt",
238
+ "to taste Ground Black Pepper",
239
+ "4 Egg"
240
+ ],
241
+ "recipe_instructions": "Fill a large stock pot with water. Add the Multi-Colored Fingerling Potato...",
242
+ "recipe_image": "https://www.sidechef.com/recipe/eeeeeceb-493e-493d-8273-66c800821b13.jpg?d=1408x1120",
243
+ "blogger": "sidechef.com",
244
+ "recipe_nutrients": {
245
+ "calories": "80 calories",
246
+ "proteinContent": "2.1 g",
247
+ "fatContent": "6.2 g",
248
+ "carbohydrateContent": "3.9 g",
249
+ "fiberContent": "0.5 g",
250
+ "sugarContent": "0.4 g",
251
+ "sodiumContent": "108.0 mg",
252
+ "saturatedFatContent": "1.2 g",
253
+ "transFatContent": "0.0 g",
254
+ "cholesterolContent": "47.4 mg",
255
+ "unsaturatedFatContent": "3.8 g"
256
+ },
257
+ "tags": [
258
+ "Salad",
259
+ "Lunch",
260
+ "Brunch",
261
+ "Appetizers",
262
+ "Side Dish",
263
+ "Budget-Friendly",
264
+ "Vegetarian",
265
+ "Pescatarian",
266
+ "Eggs",
267
+ "Potatoes",
268
+ "Easy",
269
+ "Dairy-Free",
270
+ "Shellfish-Free",
271
+ "Entertaining",
272
+ "Fish-Free",
273
+ "Peanut-Free",
274
+ "Tree Nut-Free",
275
+ "Sugar-Free",
276
+ "Global",
277
+ "Tomato-Free",
278
+ "Stove",
279
+ ""
280
+ ],
281
+ "id_": "0000001"
282
+ }
283
+
284
+ **Example Query & Output**:
285
+
286
+ **Query**: "What are the ingredients and calories?"
287
+ **Output**:
288
+ ```json
289
+ {
290
+ "header": "Here is the information you requested:",
291
+ "recipe_ingredients": [
292
+ "2 1/2 cup Multi-Colored Fingerling Potato",
293
+ "3/4 cup Celery",
294
+ "1/4 cup Red Onion",
295
+ "2 tablespoon Fresh Parsley",
296
+ "1/3 cup Mayonnaise",
297
+ "1 tablespoon Chili Garlic Sauce",
298
+ "1 teaspoon Hoisin Sauce",
299
+ "1 splash Soy Sauce",
300
+ "to taste Salt",
301
+ "to taste Ground Black Pepper",
302
+ "4 Egg"
303
+ ],
304
+ "recipe_nutrients": {
305
+ "calories": "80 calories"
306
+ }
307
+ }
308
+
309
+ Try to format the output as JSON object with key value pairs.
310
+ """
311
+
312
+ formatted_input = f"""
313
+ User Query: {user_query}
314
+
315
+ Recipe data as Context:
316
+ {context}
317
+ """
318
+ response = router.invoke(
319
+ [SystemMessage(content=system_prompt)]
320
+ + [
321
+ HumanMessage(
322
+ content=formatted_input
323
+ )
324
+ ]
325
+ )
326
+ res = json.loads(response.content)
327
+ return res
328
 
329
 
330
  def answer_generator(formated_input, session_id):
331
  # QA system prompt and chain
332
  qa_system_prompt = """
333
+ You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on context and user queries.
334
+ Operational Guidelines: \n
335
+ 1. Input Structure: \n
336
+ - Context: You may receive contextual information related to recipes, such as specific recipe name, ingredients, nutritional informations, intsructions, recipe tags, or previously selected dishes. \n
337
+ - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient, cooking instructions, and more. \n
338
+ 2. Response Strategy: \n
339
+ - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance. \n
340
+ - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, kindly state that you do not have the required information. \n
341
+ Output Format: \n
342
+ - The output format should be JSON.
343
+ - The output should have a key 'header' with response message header such as "Here is your ....",
344
+ - Then there should be other key with the actual response information. If the user query asks recipe ingredients then the key should be named "ingredients" with
345
+ JSON object as its value. The JSON object should have ingredient and its measurement as key-value pairs. Similarly if user asked for nutritional information then the output should have 'header' key with header text and 'nutrients' key
346
+ with a JSON object og nutrient and its content as key-value pairs. Similarly if the user query asks for recipe instructions then JSON output should include 'header key with header text and
347
+ 'instructions' key with a list of instructions as its value.
348
+
349
+ Following are the output formats for some cases:
350
+ 1. if user query asks for all recipe information, then output should be of following format:
351
+ {
352
+ header: header text,
353
+ recipe_name: Recipe Name,
354
+ recipe_instructions: List of recipe instructions,
355
+ recipe_nutrients: key-value pairs of nutrients name and its content,
356
+ recipe_ingredients: key-value pairs of ingredients name and its content,
357
+ recipe_tags: List of tags related to recipe,
358
+ .
359
+ .
360
+ .
361
+ }
362
+
363
+ 2. if user query asks for recipe nutrients information, then output should be of following format:
364
+ {
365
+ header: header text,
366
+ recipe_nutrients: key-value pairs of nutrients name and its content.
367
+ }
368
+
369
+ 3. if user query asks for recipe instructions information, then output should be of following format:
370
+ {
371
+ header: header text,
372
+ recipe_instructions: List of recipe instructions,
373
+ }
374
+
375
+ 4. if user query asks for recipe instructions information, then output should be of following format:
376
+ {
377
+ header: header text,
378
+ recipe_instructions: List of recipe instructions,
379
+ }
380
+
381
+
382
  Additional Instructions:
383
  - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
384
+ - Clarity and Coherence: Ensure all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
385
+ - Substitute Suggestions: Consider user preferences and dietary restrictions outlined in the context or user query when suggesting ingredient substitutes.
386
  - Dynamic Adaptation: Adapt your responses dynamically based on whether the context is relevant to the user's current request, ensuring optimal use of available information.
387
+ - Don't mention about the context in the response, format the answer in a natural and friendly way.
388
+
389
  Context:
390
  {context}
391
  """
 
421
  # Prompt
422
  router_instructions = """You are an expert at determining the appropriate task for a user’s question based on chat history and the current query context. You have two available tasks:
423
 
424
+ 1. Retrieval: Fetch information based on the user's chat history and current query.
425
+ 2. Recommendation/Suggestion: Recommend user recipes based on the query.
426
 
427
  Return a JSON response with a single key named “task” indicating either “retrieval” or “recommendation” based on your decision.
428
  """
 
447
  "recipe_time": integer,
448
  "recipe_yields": string,
449
  "recipe_ingredients": list of ingredients,
450
+ "recipe_instructions": list of instructions,
451
  "recipe_image": string,
452
  "blogger": string,
453
+ "recipe_nutrients": JSON object with key-value pairs such as "protein: 10g",
454
+ "tags": list of tags related to a recipe
455
  } \n
456
 
457
+ Here is the example of a recipe JSON object from the JSON data: \n
458
  {
459
  "recipe_name": "Asian Potato Salad with Seven Minute Egg",
460
  "recipe_time": 0,
 
504
  ]
505
  } \n
506
 
507
+ Based on the user query, provide a Python function to filter the JSON data. The output of the function should be a list of JSON objects. \n
508
 
509
  Recipe filtering instructions:
510
+ - If a user asked for the highest nutrient recipe such as "high protein or high calories" then filtered recipes should be the top highest recipes from all the recipes with high nutrients.
511
+ - sort or rearrange recipes based on which recipes are more appropriate for the user.
512
+ - Suggest dishes based on user preferences, dietary restrictions, available ingredients if specified by user.
513
 
514
  Your output instructions:
515
+ - The function name should be filter_recipes. The input to the function should be the file name.
516
  - The length of output recipes should not be more than 6.
517
+ - Only give me the output function. Do not call the function.
518
+ - Give the Python function as a key named "code" in a JSON format.
519
+ - Do not include any other text with the output, only give Python code.
520
+ - If you do not follow the above-given instructions, the chat may be terminated.
521
  """
522
  max_tries = 3
523
  while True:
 
575
  return res
576
 
577
  CURR_CONTEXT = ''
578
+ CURR_SESSION_KEY = generate_session_key()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579
 
580
  import json
581
  import base64
 
586
  # Dictionary to store incomplete image data by session
587
  session_store = {}
588
 
 
589
  def handle_message(data):
590
  global session_store
591
  global CURR_CONTEXT
592
+ global CURR_SESSION_KEY
593
+ session_id = CURR_SESSION_KEY
594
  context = "No data available"
 
595
  if session_id not in session_store:
596
  session_store[session_id] = {'image_data': b"", 'message': None, 'image_received': False}
597
 
 
606
 
607
  except Exception as e:
608
  print(f"Error processing image chunk: {str(e)}")
609
+ return "An error occurred while receiving the image chunk."
 
610
 
611
  if session_store[session_id]['image_data'] or session_store[session_id]['message']:
612
  try:
 
628
  }
629
  # Invoke question_answer_chain and stream the response
630
  response = answer_generator(formated_input, session_id=session_id)
631
+ return response
632
 
633
  except Exception as e:
634
  print(f"Error processing image or message: {str(e)}")
635
+ return "An error occurred while processing your request."
 
636
  finally:
637
  # Clear session data after processing
638
  session_store.pop(session_id, None)
 
646
  'context': json.dumps(CURR_CONTEXT)
647
  }
648
  response = answer_generator(formated_input, session_id=session_id)
649
+ session_store.pop(session_id, None)
650
+ return response
651
  else:
652
  response = recommendation_node(message)
 
653
  # response = answer_formatter_node(message, recipes)
654
  if response is None:
655
  response = {'content':"An error occurred while processing your request."}
656
+ session_store.pop(session_id, None)
657
+ return response
 
 
 
658
 
659
  import requests
660
  from PIL import Image
 
662
  from io import BytesIO
663
 
664
  def download_image_to_numpy(url):
 
665
  # Send a GET request to the URL to download the image
666
  response = requests.get(url)
667
 
 
677
  else:
678
  raise Exception(f"Failed to download image. Status code: {response.status_code}")
679
 
 
680
  def handle_message(data):
681
+ global CURR_SESSION_KEY
682
+ session_id = CURR_SESSION_KEY
683
  img_url = data['img_url']
684
  message = data['message']
 
685
  image_array = download_image_to_numpy(img_url)
686
+ response = get_answer(image=image_array, message=message, sessionID=session_id)
 
687
  return response
688
 
689
+
690
+ # @spaces.GPU
691
+ def respond_to_user(image, message):
692
+ # Process the image and message here
693
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
694
+ chat = Chat(model,transform,df,tar_img_feats, device)
695
+ chat.encode_image(image)
696
+ data = chat.ask()
697
+ formated_input = {
698
+ 'input': message,
699
+ 'context': data
700
+ }
701
+ try:
702
+ response = answer_generator(formated_input, session_id="123cnedc")
703
+ except Exception as e:
704
+ response = {'content':"An error occurred while processing your request."}
705
+ return response
706
+
707
+ from PIL import Image
708
+ import numpy as np
709
+
710
+ @app.route("/", methods=["POST"])
711
+ def get_answer():
712
+ global CURR_CONTEXT
713
+ global CURR_SESSION_KEY
714
+ sessionID = CURR_SESSION_KEY
715
+
716
+ image = request.files.get('image', "")
717
+ message = request.form.get('message', "")
718
+
719
+ if image:
720
+ # Open the image using PIL
721
+ img = Image.open(image.stream) # Use image.stream for file-like object
722
+ img = img.convert('RGB') # Convert to RGB if needed
723
+
724
+ # Convert the PIL image to a NumPy array
725
+ image = np.array(img)
726
+
727
+ if image is not None:
728
+ try:
729
+ # Process the image and message here
730
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
731
+ chat = Chat(model,transform,df,tar_img_feats, device)
732
+ chat.encode_image(image)
733
+ data = chat.ask()
734
+ CURR_CONTEXT = data
735
+ formated_input = {
736
+ 'input': message,
737
+ 'context': data
738
+ }
739
+ # response = answer_generator(formated_input, session_id=sessionID)
740
+ response = json_answer_generator(message, data)
741
+ except Exception as e:
742
+ print(e)
743
+ response = {'content':"An error occurred while processing your request."}
744
+ elif (image is None) and (message is not None):
745
+ task = router_node(message)
746
+ if task == 'recommendation':
747
+ recipes = recommendation_node(message)
748
+ if not recipes:
749
+ response = {'content': "An error occurred while processing your request."}
750
+ else:
751
+ # response = answer_formatter_node(message, recipes)
752
+ response = recipes
753
+ else:
754
+ formated_input = {
755
+ 'input': message,
756
+ 'context': CURR_CONTEXT
757
+ }
758
+ # response = answer_generator(formated_input, session_id=sessionID)
759
+ response = json_answer_generator(message, CURR_CONTEXT)
760
+ return jsonify(response)
761
+
762
+
763
 
764
  # Main function to run the app
765
  if __name__ == '__main__':
766
+ Flask.run(app, debug=False)
demo.py CHANGED
@@ -1,10 +1,7 @@
1
  import pandas as pd
2
- import json
3
  from PIL import Image
4
  import numpy as np
5
-
6
  import os
7
- from pathlib import Path
8
 
9
  import torch
10
  import torch.nn.functional as F
@@ -15,7 +12,7 @@ from src.data.transforms import transform_test
15
 
16
  from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
17
  import gradio as gr
18
- # import spaces
19
 
20
  from langchain.chains import ConversationChain
21
  from langchain_community.chat_message_histories import ChatMessageHistory
@@ -25,61 +22,6 @@ from langchain_core.prompts import ChatPromptTemplate
25
  from langchain_groq import ChatGroq
26
 
27
  from dotenv import load_dotenv
28
- from flask import Flask, request, render_template
29
- from flask_cors import CORS
30
- from flask_socketio import SocketIO, emit
31
-
32
-
33
- # GROQ_API_KEY = os.getenv("GROQ_API_KEY")
34
- GROQ_API_KEY = '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'
35
- # load_dotenv(".env")
36
- USER_AGENT = os.getenv("USER_AGENT")
37
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
38
- SECRET_KEY = os.getenv("SECRET_KEY")
39
-
40
-
41
- # Set environment variables
42
- os.environ['USER_AGENT'] = USER_AGENT
43
- os.environ["GROQ_API_KEY"] = GROQ_API_KEY
44
- os.environ["TOKENIZERS_PARALLELISM"] = 'true'
45
-
46
- # Initialize Flask app and SocketIO with CORS
47
- app = Flask(__name__)
48
- CORS(app)
49
- app.config['MAX_CONTENT_LENGTH'] = 1e7
50
- app.config['SESSION_COOKIE_SECURE'] = True # Use HTTPS
51
- app.config['SESSION_COOKIE_HTTPONLY'] = True
52
- app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
53
- socketio = SocketIO(app, cors_allowed_origins="*", logger=True, max_http_buffer_size=1e7)
54
- app.config['SECRET_KEY'] = SECRET_KEY
55
-
56
- import pandas as pd
57
- from PIL import Image
58
- import numpy as np
59
- import os
60
-
61
- import torch
62
- import torch.nn.functional as F
63
-
64
- # from src.data.embs import ImageDataset
65
- from src.model.blip_embs import blip_embs
66
- from src.data.transforms import transform_test
67
-
68
- from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
69
- import gradio as gr
70
- # import spaces
71
-
72
- from langchain.chains import ConversationChain
73
- from langchain_community.chat_message_histories import ChatMessageHistory
74
- from langchain_core.runnables import RunnableWithMessageHistory
75
- from langchain_core.output_parsers import StrOutputParser
76
- from langchain_core.prompts import ChatPromptTemplate
77
- from langchain_groq import ChatGroq
78
-
79
- from dotenv import load_dotenv
80
- from flask import Flask, request, render_template
81
- from flask_cors import CORS
82
- from flask_socketio import SocketIO, emit
83
 
84
  import json
85
  from openai import OpenAI
@@ -97,18 +39,12 @@ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
97
  os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY
98
  os.environ["TOKENIZERS_PARALLELISM"] = 'true'
99
 
100
- # Initialize Flask app and SocketIO with CORS
101
- app = Flask(__name__)
102
- CORS(app)
103
- socketio = SocketIO(app, cors_allowed_origins="*", logger=True)
104
- app.config['SESSION_COOKIE_SECURE'] = True # Use HTTPS
105
- app.config['SESSION_COOKIE_HTTPONLY'] = True
106
- app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
107
- app.config['SECRET_KEY'] = SECRET_KEY
108
-
109
  # Initialize LLM
110
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
111
 
 
 
 
112
  # Initialize Router
113
  router = ChatGroq(model="llama-3.2-3b-preview", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
114
 
@@ -128,7 +64,7 @@ class StoppingCriteriaSub(StoppingCriteria):
128
  for stop in self.stops:
129
  if torch.all(input_ids[:, -len(stop):] == stop).item():
130
  return True
131
-
132
  return False
133
 
134
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -196,7 +132,7 @@ tar_img_feats = torch.cat(tar_img_feats, dim=0)
196
 
197
  class Chat:
198
 
199
- def __init__(self, model, transform, dataframe, tar_img_feats, device='cuda:0', stopping_criteria=None):
200
  self.device = device
201
  self.model = model
202
  self.transform = transform
@@ -237,28 +173,208 @@ chat = Chat(model,transform,df,tar_img_feats, device)
237
  print("Chat Initialized !")
238
 
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  def answer_generator(formated_input, session_id):
241
  # QA system prompt and chain
242
  qa_system_prompt = """
243
- You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on provide context ans user query.
244
- Operational Guidelines:
245
- 1. Input Structure:
246
- - Context: You may receive contextual information related to recipes, such as specific data sets, user preferences, dietary restrictions, or previously selected dishes.
247
- - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient substitutions, cooking instructions, and more.
248
- 2. Response Strategy:
249
- - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance.
250
- - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, kindly state that you do not have require information.
251
- Core Functionalities:
252
- - Nutritional Information: Accurately provide nutritional values for each recipe, including calories, macronutrients (proteins, fats, carbohydrates), and essential vitamins and minerals, using contextual data when available.
253
- - Ingredient Details: List all ingredients required for recipes, including substitute options for dietary restrictions or ingredient availability, utilizing context when relevant.
254
- - Step-by-Step Cooking Instructions: Deliver clear, easy-to-follow instructions for preparing and cooking meals, informed by any provided contextual data.
255
- - Recipe Recommendations: Suggest dishes based on user preferences, dietary restrictions, available ingredients, and contextual data if provided.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  Additional Instructions:
257
  - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
258
- - Clarity and Coherence: Ensure that all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
259
- - Substitute Suggestions: When suggesting ingredient substitutes, consider user preferences and dietary restrictions outlined in the context or user query.
260
  - Dynamic Adaptation: Adapt your responses dynamically based on whether the context is relevant to the user's current request, ensuring optimal use of available information.
261
- Don't mention about context in the response, format the answer in a natural and friendly way.
 
262
  Context:
263
  {context}
264
  """
@@ -294,8 +410,8 @@ def router_node(query):
294
  # Prompt
295
  router_instructions = """You are an expert at determining the appropriate task for a user’s question based on chat history and the current query context. You have two available tasks:
296
 
297
- 1. Retrieval: Fetch information based on user's chat history and current query.
298
- 2. Recommendation/Suggestion: Recommend recipes to users based on the query.
299
 
300
  Return a JSON response with a single key named “task” indicating either “retrieval” or “recommendation” based on your decision.
301
  """
@@ -320,14 +436,14 @@ def recommendation_node(query):
320
  "recipe_time": integer,
321
  "recipe_yields": string,
322
  "recipe_ingredients": list of ingredients,
323
- "recipe_instructions": list of instruections,
324
  "recipe_image": string,
325
  "blogger": string,
326
- "recipe_nutrients": JSON object with key value pairs such as "protein: 10g",
327
- "tags": list of tags related to recipe
328
  } \n
329
 
330
- Here is the example of an recipe json object from the JSON data: \n
331
  {
332
  "recipe_name": "Asian Potato Salad with Seven Minute Egg",
333
  "recipe_time": 0,
@@ -377,19 +493,20 @@ def recommendation_node(query):
377
  ]
378
  } \n
379
 
380
- Based on the user query, provide a Python function to filter the JSON data. The output of the function should be a list of json objects. \n
381
 
382
  Recipe filtering instructions:
383
- - If a user asked for the highest nutrient recipe such as "high protein or high calories" then filtered recipes should be the top highest recipes from all the recipes with high nutrient.
384
- - sort or rearrange recipes based which recipes are more appropriate for the user.
 
385
 
386
  Your output instructions:
387
- - The function name should be filter_recipes. The input to the function should be file name.
388
  - The length of output recipes should not be more than 6.
389
- - Only give me output function. Do not call the function.
390
- - Give the python function as a key named "code" in a json format.
391
- - Do not include any other text with the output, only give python code.
392
- - If you do not follow the above given instructions, the chat may be terminated.
393
  """
394
  max_tries = 3
395
  while True:
@@ -447,11 +564,14 @@ def answer_formatter_node(question, context):
447
  return res
448
 
449
  CURR_CONTEXT = ''
 
450
 
451
- # @spaces.GPU
452
  def get_answer(image=[], message='', sessionID='abc123'):
453
  global CURR_CONTEXT
454
- if len(image) > 0:
 
 
455
  try:
456
  # Process the image and message here
457
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -463,43 +583,29 @@ def get_answer(image=[], message='', sessionID='abc123'):
463
  'input': message,
464
  'context': data
465
  }
466
- response = answer_generator(formated_input, session_id=sessionID)
 
467
  except Exception as e:
468
  print(e)
469
  response = {'content':"An error occurred while processing your request."}
470
- elif len(image) == 0 and message is not None:
471
- print("I am here")
472
  task = router_node(message)
473
- if task == 'retrieval':
474
  recipes = recommendation_node(message)
475
  if not recipes:
476
- response = {'content':"An error occurred while processing your request."}
477
- response = answer_formatter_node(message, recipes)
 
 
478
  else:
479
  formated_input = {
480
  'input': message,
481
  'context': CURR_CONTEXT
482
  }
483
- response = answer_generator(formated_input, session_id=sessionID)
484
-
485
  return response
486
 
487
- # Function to handle WebSocket connection
488
- @socketio.on('ping')
489
- def handle_connect():
490
- emit('Ping-return', {'message': 'Connected'}, room=request.sid)
491
-
492
-
493
- # Function to handle WebSocket connection
494
- @socketio.on('connect')
495
- def handle_connect():
496
- print(f"Client connected: {request.sid}")
497
-
498
- # Function to handle WebSocket disconnection
499
- @socketio.on('disconnect')
500
- def handle_disconnect():
501
- print(f"Client disconnected: {request.sid}")
502
-
503
  import json
504
  import base64
505
  from PIL import Image
@@ -509,12 +615,12 @@ import torchvision.transforms as transforms
509
  # Dictionary to store incomplete image data by session
510
  session_store = {}
511
 
512
- @socketio.on('message')
513
  def handle_message(data):
514
  global session_store
515
  global CURR_CONTEXT
 
 
516
  context = "No data available"
517
- session_id = request.sid
518
  if session_id not in session_store:
519
  session_store[session_id] = {'image_data': b"", 'message': None, 'image_received': False}
520
 
@@ -529,8 +635,7 @@ def handle_message(data):
529
 
530
  except Exception as e:
531
  print(f"Error processing image chunk: {str(e)}")
532
- emit('response', "An error occurred while receiving the image chunk.", room=session_id)
533
- return
534
 
535
  if session_store[session_id]['image_data'] or session_store[session_id]['message']:
536
  try:
@@ -552,12 +657,11 @@ def handle_message(data):
552
  }
553
  # Invoke question_answer_chain and stream the response
554
  response = answer_generator(formated_input, session_id=session_id)
555
- emit('response', response, room=session_id)
556
 
557
  except Exception as e:
558
  print(f"Error processing image or message: {str(e)}")
559
- emit('response', "An error occurred while processing your request.", room=session_id)
560
- return
561
  finally:
562
  # Clear session data after processing
563
  session_store.pop(session_id, None)
@@ -571,17 +675,15 @@ def handle_message(data):
571
  'context': json.dumps(CURR_CONTEXT)
572
  }
573
  response = answer_generator(formated_input, session_id=session_id)
574
- emit('response', response, room=session_id)
 
575
  else:
576
  response = recommendation_node(message)
577
  # response = answer_formatter_node(message, recipes)
578
  if response is None:
579
  response = {'content':"An error occurred while processing your request."}
580
-
581
- emit('json_response', response, room=session_id)
582
- session_store.pop(session_id, None)
583
-
584
-
585
 
586
  import requests
587
  from PIL import Image
@@ -604,24 +706,41 @@ def download_image_to_numpy(url):
604
  else:
605
  raise Exception(f"Failed to download image. Status code: {response.status_code}")
606
 
607
- @socketio.on('example')
608
  def handle_message(data):
 
 
609
  img_url = data['img_url']
610
  message = data['message']
611
- session_id = request.sid
612
  image_array = download_image_to_numpy(img_url)
613
- response = get_answer(image=image_array, message=message, sessionID=request.sid)
614
- emit('response', response, room=session_id)
615
  return response
616
 
617
 
618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619
 
620
- # Home route
621
- @app.route("/")
622
- def index_view():
623
- return render_template('chat.html')
 
 
 
 
624
 
625
- # Main function to run the app
626
- if __name__ == '__main__':
627
- socketio.run(app, debug=True)
 
1
  import pandas as pd
 
2
  from PIL import Image
3
  import numpy as np
 
4
  import os
 
5
 
6
  import torch
7
  import torch.nn.functional as F
 
12
 
13
  from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
14
  import gradio as gr
15
+ import spaces
16
 
17
  from langchain.chains import ConversationChain
18
  from langchain_community.chat_message_histories import ChatMessageHistory
 
22
  from langchain_groq import ChatGroq
23
 
24
  from dotenv import load_dotenv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  import json
27
  from openai import OpenAI
 
39
  os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY
40
  os.environ["TOKENIZERS_PARALLELISM"] = 'true'
41
 
 
 
 
 
 
 
 
 
 
42
  # Initialize LLM
43
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
44
 
45
+ # JSON response LLM
46
+ json_llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
47
+
48
  # Initialize Router
49
  router = ChatGroq(model="llama-3.2-3b-preview", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
50
 
 
64
  for stop in self.stops:
65
  if torch.all(input_ids[:, -len(stop):] == stop).item():
66
  return True
67
+
68
  return False
69
 
70
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
132
 
133
  class Chat:
134
 
135
+ def __init__(self, model, transform, dataframe, tar_img_feats, device='cuda', stopping_criteria=None):
136
  self.device = device
137
  self.model = model
138
  self.transform = transform
 
173
  print("Chat Initialized !")
174
 
175
 
176
+
177
+ import secrets
178
+ import string
179
+
180
+ def generate_session_key():
181
+ characters = string.ascii_letters + string.digits
182
+ session_key = ''.join(secrets.choice(characters) for _ in range(8))
183
+ return session_key
184
+
185
+
186
+ def json_answer_generator(user_query, context):
187
+ system_prompt = """
188
+ Given a recipe context in JSON format, respond to user queries by extracting and returning the requested information in JSON format with an additional `"header"` key containing a response starter. Use the following rules:
189
+
190
+ 1. **Recipe Information Extraction**:
191
+ - If the user query explicitly requests specific recipe data (e.g., ingredients, nutrients, or instructions), return only those JSON objects from the provided recipe context.
192
+ - For example, if the user asks, “What are the ingredients?” or “Show me the nutrient details,” your output should be limited to only the requested JSON objects (e.g., `recipe_ingredients`, `recipe_nutrients`).
193
+ - Include `"header": "Here is the information you requested:"` at the start of each response.
194
+
195
+ 2. **Multiple Information Points**:
196
+ - If a user query asks for more than one piece of information, return each requested JSON object from the recipe context in a combined JSON response.
197
+ - For example, if the query is “Give me the ingredients and instructions,” the output should include both `recipe_ingredients` and `recipe_instructions` objects.
198
+ - Include `"header": "Here is the information you requested:"` at the start of each response.
199
+
200
+ 3. **Non-Specific Recipe Information**:
201
+ - If the query does not directly refer to recipe data but instead asks for a general response based on the context, return a JSON object with a single key `"content"` and a descriptive response as its value.
202
+ - Include `"header": "Here is a suggestion based on the recipe:"` as the response starter.
203
+ - For example, if the query is “How can I use this recipe for a healthy lunch?” return a response like:
204
+ ```json
205
+ {
206
+ "header": "Here is a suggestion based on the recipe:",
207
+ "content": "This Asian Potato Salad with Seven Minute Egg is a nutritious and light option, ideal for a balanced lunch. It provides protein and essential nutrients with low calories."
208
+ }
209
+ ```
210
+
211
+ **Example Context**:
212
+ ```json
213
+ {
214
+ "recipe_name": "Asian Potato Salad with Seven Minute Egg",
215
+ "recipe_time": 0,
216
+ "recipe_yields": "4 servings",
217
+ "recipe_ingredients": [
218
+ "2 1/2 cup Multi-Colored Fingerling Potato",
219
+ "3/4 cup Celery",
220
+ "1/4 cup Red Onion",
221
+ "2 tablespoon Fresh Parsley",
222
+ "1/3 cup Mayonnaise",
223
+ "1 tablespoon Chili Garlic Sauce",
224
+ "1 teaspoon Hoisin Sauce",
225
+ "1 splash Soy Sauce",
226
+ "to taste Salt",
227
+ "to taste Ground Black Pepper",
228
+ "4 Egg"
229
+ ],
230
+ "recipe_instructions": "Fill a large stock pot with water. Add the Multi-Colored Fingerling Potato...",
231
+ "recipe_image": "https://www.sidechef.com/recipe/eeeeeceb-493e-493d-8273-66c800821b13.jpg?d=1408x1120",
232
+ "blogger": "sidechef.com",
233
+ "recipe_nutrients": {
234
+ "calories": "80 calories",
235
+ "proteinContent": "2.1 g",
236
+ "fatContent": "6.2 g",
237
+ "carbohydrateContent": "3.9 g",
238
+ "fiberContent": "0.5 g",
239
+ "sugarContent": "0.4 g",
240
+ "sodiumContent": "108.0 mg",
241
+ "saturatedFatContent": "1.2 g",
242
+ "transFatContent": "0.0 g",
243
+ "cholesterolContent": "47.4 mg",
244
+ "unsaturatedFatContent": "3.8 g"
245
+ },
246
+ "tags": [
247
+ "Salad",
248
+ "Lunch",
249
+ "Brunch",
250
+ "Appetizers",
251
+ "Side Dish",
252
+ "Budget-Friendly",
253
+ "Vegetarian",
254
+ "Pescatarian",
255
+ "Eggs",
256
+ "Potatoes",
257
+ "Easy",
258
+ "Dairy-Free",
259
+ "Shellfish-Free",
260
+ "Entertaining",
261
+ "Fish-Free",
262
+ "Peanut-Free",
263
+ "Tree Nut-Free",
264
+ "Sugar-Free",
265
+ "Global",
266
+ "Tomato-Free",
267
+ "Stove",
268
+ ""
269
+ ],
270
+ "id_": "0000001"
271
+ }
272
+
273
+ **Example Query & Output**:
274
+
275
+ **Query**: "What are the ingredients and calories?"
276
+ **Output**:
277
+ ```json
278
+ {
279
+ "header": "Here is the information you requested:",
280
+ "recipe_ingredients": [
281
+ "2 1/2 cup Multi-Colored Fingerling Potato",
282
+ "3/4 cup Celery",
283
+ "1/4 cup Red Onion",
284
+ "2 tablespoon Fresh Parsley",
285
+ "1/3 cup Mayonnaise",
286
+ "1 tablespoon Chili Garlic Sauce",
287
+ "1 teaspoon Hoisin Sauce",
288
+ "1 splash Soy Sauce",
289
+ "to taste Salt",
290
+ "to taste Ground Black Pepper",
291
+ "4 Egg"
292
+ ],
293
+ "recipe_nutrients": {
294
+ "calories": "80 calories"
295
+ }
296
+ }
297
+
298
+ Try to format the output as JSON object with key value pairs.
299
+ """
300
+
301
+ formatted_input = f"""
302
+ User Query: {user_query}
303
+
304
+ Recipe data as Context:
305
+ {context}
306
+ """
307
+ response = router.invoke(
308
+ [SystemMessage(content=system_prompt)]
309
+ + [
310
+ HumanMessage(
311
+ content=formatted_input
312
+ )
313
+ ]
314
+ )
315
+ res = json.loads(response.content)
316
+ return res
317
+
318
+
319
  def answer_generator(formated_input, session_id):
320
  # QA system prompt and chain
321
  qa_system_prompt = """
322
+ You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on context and user queries.
323
+ Operational Guidelines: \n
324
+ 1. Input Structure: \n
325
+ - Context: You may receive contextual information related to recipes, such as specific recipe name, ingredients, nutritional informations, intsructions, recipe tags, or previously selected dishes. \n
326
+ - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient, cooking instructions, and more. \n
327
+ 2. Response Strategy: \n
328
+ - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance. \n
329
+ - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, kindly state that you do not have the required information. \n
330
+ Output Format: \n
331
+ - The output format should be JSON.
332
+ - The output should have a key 'header' with response message header such as "Here is your ....",
333
+ - Then there should be other key with the actual response information. If the user query asks recipe ingredients then the key should be named "ingredients" with
334
+ JSON object as its value. The JSON object should have ingredient and its measurement as key-value pairs. Similarly if user asked for nutritional information then the output should have 'header' key with header text and 'nutrients' key
335
+ with a JSON object og nutrient and its content as key-value pairs. Similarly if the user query asks for recipe instructions then JSON output should include 'header key with header text and
336
+ 'instructions' key with a list of instructions as its value.
337
+
338
+ Following are the output formats for some cases:
339
+ 1. if user query asks for all recipe information, then output should be of following format:
340
+ {
341
+ header: header text,
342
+ recipe_name: Recipe Name,
343
+ recipe_instructions: List of recipe instructions,
344
+ recipe_nutrients: key-value pairs of nutrients name and its content,
345
+ recipe_ingredients: key-value pairs of ingredients name and its content,
346
+ recipe_tags: List of tags related to recipe,
347
+ .
348
+ .
349
+ .
350
+ }
351
+
352
+ 2. if user query asks for recipe nutrients information, then output should be of following format:
353
+ {
354
+ header: header text,
355
+ recipe_nutrients: key-value pairs of nutrients name and its content.
356
+ }
357
+
358
+ 3. if user query asks for recipe instructions information, then output should be of following format:
359
+ {
360
+ header: header text,
361
+ recipe_instructions: List of recipe instructions,
362
+ }
363
+
364
+ 4. if user query asks for recipe instructions information, then output should be of following format:
365
+ {
366
+ header: header text,
367
+ recipe_instructions: List of recipe instructions,
368
+ }
369
+
370
+
371
  Additional Instructions:
372
  - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
373
+ - Clarity and Coherence: Ensure all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
374
+ - Substitute Suggestions: Consider user preferences and dietary restrictions outlined in the context or user query when suggesting ingredient substitutes.
375
  - Dynamic Adaptation: Adapt your responses dynamically based on whether the context is relevant to the user's current request, ensuring optimal use of available information.
376
+ - Don't mention about the context in the response, format the answer in a natural and friendly way.
377
+
378
  Context:
379
  {context}
380
  """
 
410
  # Prompt
411
  router_instructions = """You are an expert at determining the appropriate task for a user’s question based on chat history and the current query context. You have two available tasks:
412
 
413
+ 1. Retrieval: Fetch information based on the user's chat history and current query.
414
+ 2. Recommendation/Suggestion: Recommend user recipes based on the query.
415
 
416
  Return a JSON response with a single key named “task” indicating either “retrieval” or “recommendation” based on your decision.
417
  """
 
436
  "recipe_time": integer,
437
  "recipe_yields": string,
438
  "recipe_ingredients": list of ingredients,
439
+ "recipe_instructions": list of instructions,
440
  "recipe_image": string,
441
  "blogger": string,
442
+ "recipe_nutrients": JSON object with key-value pairs such as "protein: 10g",
443
+ "tags": list of tags related to a recipe
444
  } \n
445
 
446
+ Here is the example of a recipe JSON object from the JSON data: \n
447
  {
448
  "recipe_name": "Asian Potato Salad with Seven Minute Egg",
449
  "recipe_time": 0,
 
493
  ]
494
  } \n
495
 
496
+ Based on the user query, provide a Python function to filter the JSON data. The output of the function should be a list of JSON objects. \n
497
 
498
  Recipe filtering instructions:
499
+ - If a user asked for the highest nutrient recipe such as "high protein or high calories" then filtered recipes should be the top highest recipes from all the recipes with high nutrients.
500
+ - sort or rearrange recipes based on which recipes are more appropriate for the user.
501
+ - Suggest dishes based on user preferences, dietary restrictions, available ingredients if specified by user.
502
 
503
  Your output instructions:
504
+ - The function name should be filter_recipes. The input to the function should be the file name.
505
  - The length of output recipes should not be more than 6.
506
+ - Only give me the output function. Do not call the function.
507
+ - Give the Python function as a key named "code" in a JSON format.
508
+ - Do not include any other text with the output, only give Python code.
509
+ - If you do not follow the above-given instructions, the chat may be terminated.
510
  """
511
  max_tries = 3
512
  while True:
 
564
  return res
565
 
566
  CURR_CONTEXT = ''
567
+ CURR_SESSION_KEY = generate_session_key()
568
 
569
+ @spaces.GPU
570
  def get_answer(image=[], message='', sessionID='abc123'):
571
  global CURR_CONTEXT
572
+ global CURR_SESSION_KEY
573
+ sessionID = CURR_SESSION_KEY
574
+ if image is not None:
575
  try:
576
  # Process the image and message here
577
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
583
  'input': message,
584
  'context': data
585
  }
586
+ # response = answer_generator(formated_input, session_id=sessionID)
587
+ response = json_answer_generator(message, data)
588
  except Exception as e:
589
  print(e)
590
  response = {'content':"An error occurred while processing your request."}
591
+ elif (image is None) and (message is not None):
 
592
  task = router_node(message)
593
+ if task == 'recommendation':
594
  recipes = recommendation_node(message)
595
  if not recipes:
596
+ response = {'content': "An error occurred while processing your request."}
597
+ else:
598
+ # response = answer_formatter_node(message, recipes)
599
+ response = recipes
600
  else:
601
  formated_input = {
602
  'input': message,
603
  'context': CURR_CONTEXT
604
  }
605
+ # response = answer_generator(formated_input, session_id=sessionID)
606
+ response = json_answer_generator(message, CURR_CONTEXT)
607
  return response
608
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609
  import json
610
  import base64
611
  from PIL import Image
 
615
  # Dictionary to store incomplete image data by session
616
  session_store = {}
617
 
 
618
  def handle_message(data):
619
  global session_store
620
  global CURR_CONTEXT
621
+ global CURR_SESSION_KEY
622
+ session_id = CURR_SESSION_KEY
623
  context = "No data available"
 
624
  if session_id not in session_store:
625
  session_store[session_id] = {'image_data': b"", 'message': None, 'image_received': False}
626
 
 
635
 
636
  except Exception as e:
637
  print(f"Error processing image chunk: {str(e)}")
638
+ return "An error occurred while receiving the image chunk."
 
639
 
640
  if session_store[session_id]['image_data'] or session_store[session_id]['message']:
641
  try:
 
657
  }
658
  # Invoke question_answer_chain and stream the response
659
  response = answer_generator(formated_input, session_id=session_id)
660
+ return response
661
 
662
  except Exception as e:
663
  print(f"Error processing image or message: {str(e)}")
664
+ return "An error occurred while processing your request."
 
665
  finally:
666
  # Clear session data after processing
667
  session_store.pop(session_id, None)
 
675
  'context': json.dumps(CURR_CONTEXT)
676
  }
677
  response = answer_generator(formated_input, session_id=session_id)
678
+ session_store.pop(session_id, None)
679
+ return response
680
  else:
681
  response = recommendation_node(message)
682
  # response = answer_formatter_node(message, recipes)
683
  if response is None:
684
  response = {'content':"An error occurred while processing your request."}
685
+ session_store.pop(session_id, None)
686
+ return response
 
 
 
687
 
688
  import requests
689
  from PIL import Image
 
706
  else:
707
  raise Exception(f"Failed to download image. Status code: {response.status_code}")
708
 
 
709
  def handle_message(data):
710
+ global CURR_SESSION_KEY
711
+ session_id = CURR_SESSION_KEY
712
  img_url = data['img_url']
713
  message = data['message']
 
714
  image_array = download_image_to_numpy(img_url)
715
+ response = get_answer(image=image_array, message=message, sessionID=session_id)
 
716
  return response
717
 
718
 
719
 
720
+ # @spaces.GPU
721
+ def respond_to_user(image, message):
722
+ # Process the image and message here
723
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
724
+ chat = Chat(model,transform,df,tar_img_feats, device)
725
+ chat.encode_image(image)
726
+ data = chat.ask()
727
+ formated_input = {
728
+ 'input': message,
729
+ 'context': data
730
+ }
731
+ try:
732
+ response = answer_generator(formated_input, session_id="123cnedc")
733
+ except Exception as e:
734
+ response = {'content':"An error occurred while processing your request."}
735
+ return response
736
 
737
+ iface = gr.Interface(
738
+ fn=get_answer,
739
+ inputs=[gr.Image(), gr.Textbox(label="Ask Query")],
740
+ outputs=[gr.Textbox(label="Nutrition-GPT")],
741
+ title="Nutrition-GPT Demo",
742
+ description="Upload an food image and ask queries!",
743
+ css=".component-12 {background-color: red}",
744
+ )
745
 
746
+ iface.launch()
 
 
demo2.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import pandas as pd
2
  import json
3
  from PIL import Image
@@ -25,83 +27,45 @@ from langchain_core.prompts import ChatPromptTemplate
25
  from langchain_groq import ChatGroq
26
 
27
  from dotenv import load_dotenv
 
 
 
28
 
 
 
29
 
30
  # GROQ_API_KEY = os.getenv("GROQ_API_KEY")
31
- GROQ_API_KEY = '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'
32
  load_dotenv(".env")
33
  USER_AGENT = os.getenv("USER_AGENT")
34
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
35
  SECRET_KEY = os.getenv("SECRET_KEY")
36
 
37
-
38
  # Set environment variables
39
  os.environ['USER_AGENT'] = USER_AGENT
40
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
41
  os.environ["TOKENIZERS_PARALLELISM"] = 'true'
42
 
 
 
 
 
 
 
 
 
43
  # Initialize LLM
44
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
45
 
 
 
46
 
47
- # QA system prompt and chain
48
- qa_system_prompt = """
49
- Prompt:
50
- You are a highly intelligent assistant. Use the following context to answer user questions. Analyze the data carefully and generate a clear, concise, and informative response to the user's question based on this data.
51
-
52
- Response Guidelines:
53
- - Use only the information provided in the data to answer the question.
54
- - Ensure the answer is accurate and directly related to the question.
55
- - If the user question required no related data then give the truthful response to the user question.
56
- - If the data is insufficient to answer the question, politey apologise and tell the user that there is insufficient data available to answer their question.
57
- - Provide the response in a conversational yet friendly tone.
58
-
59
- You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on provide context ans user query.
60
-
61
- Operational Guidelines:
62
- 1. Input Structure:
63
- - Context: You may receive contextual information related to recipes, such as specific data sets, user preferences, dietary restrictions, or previously selected dishes.
64
- - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient substitutions, cooking instructions, and more.
65
-
66
- 2. Response Strategy:
67
- - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance.
68
- - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, generate a response based solely on the user's input and your trained knowledge.
69
-
70
- Core Functionalities:
71
- - Nutritional Information: Accurately provide nutritional values for each recipe, including calories, macronutrients (proteins, fats, carbohydrates), and essential vitamins and minerals, using contextual data when available.
72
- - Ingredient Details: List all ingredients required for recipes, including substitute options for dietary restrictions or ingredient availability, utilizing context when relevant.
73
- - Step-by-Step Cooking Instructions: Deliver clear, easy-to-follow instructions for preparing and cooking meals, informed by any provided contextual data.
74
- - Recipe Recommendations: Suggest dishes based on user preferences, dietary restrictions, available ingredients, and contextual data if provided.
75
-
76
- Additional Instructions:
77
- - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
78
- - Clarity and Coherence: Ensure that all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
79
- - Substitute Suggestions: When suggesting ingredient substitutes, consider user preferences and dietary restrictions outlined in the context or user query.
80
- - Dynamic Adaptation: Adapt your responses dynamically based on whether the context is relevant to the user's current request, ensuring optimal use of available information.
81
-
82
- Don't mention about context in the response, format the answer in a natural friedly way.
83
-
84
- Context:
85
- {context}
86
- """
87
- qa_prompt = ChatPromptTemplate.from_messages(
88
- [
89
- ("system", qa_system_prompt),
90
- ("human", "{input}")
91
- ]
92
- )
93
-
94
- # Create the base chain
95
- base_chain = qa_prompt | llm | StrOutputParser()
96
-
97
- # Wrap the chain with message history
98
- question_answer_chain = RunnableWithMessageHistory(
99
- base_chain,
100
- lambda session_id: ChatMessageHistory(), # This creates a new history for each session
101
- input_messages_key="input",
102
- history_messages_key="chat_history"
103
- )
104
 
 
 
105
 
106
  class StoppingCriteriaSub(StoppingCriteria):
107
 
@@ -149,7 +113,6 @@ def get_blip_config(model="base"):
149
 
150
  return config
151
 
152
-
153
  print("Creating model")
154
  config = get_blip_config("large")
155
 
@@ -165,22 +128,17 @@ model = blip_embs(
165
 
166
  model = model.to(device)
167
  model.eval()
168
- print("Model Loaded !")
169
- print("="*50)
170
 
171
  transform = transform_test(384)
172
 
173
- print("Loading Data")
174
- df = pd.read_json("datasets/sidechef/my_recipes.json")
175
 
176
- print("Loading Target Embedding")
177
  tar_img_feats = []
178
  for _id in df["id_"].tolist():
179
- tar_img_feats.append(torch.load("datasets/sidechef/blip-embs-large/{:07d}.pth".format(_id)).unsqueeze(0))
180
 
181
  tar_img_feats = torch.cat(tar_img_feats, dim=0)
182
 
183
-
184
  class Chat:
185
 
186
  def __init__(self, model, transform, dataframe, tar_img_feats, device='cuda:0', stopping_criteria=None):
@@ -221,41 +179,393 @@ class Chat:
221
 
222
 
223
  chat = Chat(model,transform,df,tar_img_feats, device)
224
- print("Chat Initialized !")
225
 
226
 
227
- custom_css = """
228
- .primary{
229
- background-color: #4CAF50; /* Green */
230
- }
231
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
 
233
 
234
  # @spaces.GPU
235
- def respond_to_user(image, message):
236
- # Process the image and message here
237
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
238
- chat = Chat(model,transform,df,tar_img_feats, device)
239
- chat.encode_image(image)
240
- data = chat.ask()
241
- formated_input = {
242
- 'input': message,
243
- 'context': data
244
- }
245
- try:
246
- response = question_answer_chain.invoke(formated_input, config={"configurable": {"session_id": 'abc123'}})
247
- except Exception as e:
248
- print(e)
249
- response = {'content':"An error occurred while processing your request."}
250
- return response, data
251
-
252
- iface = gr.Interface(
253
- fn=respond_to_user,
254
- inputs=[gr.Image(), gr.Textbox(label="Ask Query")],
255
- outputs=[gr.Textbox(label="Nutrition-GPT"), gr.JSON(label="context")],
256
- title="Nutrition-GPT Demo",
257
- description="Upload an food image and ask queries!",
258
- css=".component-12 {background-color: red}",
259
- )
260
-
261
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import eventlet
2
+
3
  import pandas as pd
4
  import json
5
  from PIL import Image
 
27
  from langchain_groq import ChatGroq
28
 
29
  from dotenv import load_dotenv
30
+ from flask import Flask, request, render_template
31
+ from flask_cors import CORS
32
+ from flask_socketio import SocketIO, emit
33
 
34
+ import json
35
+ from openai import OpenAI
36
 
37
  # GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
38
  load_dotenv(".env")
39
  USER_AGENT = os.getenv("USER_AGENT")
40
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
41
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
42
  SECRET_KEY = os.getenv("SECRET_KEY")
43
 
 
44
  # Set environment variables
45
  os.environ['USER_AGENT'] = USER_AGENT
46
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
47
+ os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY
48
  os.environ["TOKENIZERS_PARALLELISM"] = 'true'
49
 
50
+ # Initialize Flask app and SocketIO with CORS
51
+ app = Flask(__name__)
52
+ CORS(app)
53
+ app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024
54
+ socketio = SocketIO(app, cors_allowed_origins="*", logger=True, max_http_buffer_size=1024 * 1024 * 1024)
55
+ app.config['SECRET_KEY'] = SECRET_KEY
56
+
57
+
58
  # Initialize LLM
59
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
60
 
61
+ # Initialize Router
62
+ router = ChatGroq(model="llama-3.2-3b-preview", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
63
 
64
+ # Initialize Router
65
+ answer_formatter = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Initialized recommendation LLM
68
+ client = OpenAI()
69
 
70
  class StoppingCriteriaSub(StoppingCriteria):
71
 
 
113
 
114
  return config
115
 
 
116
  print("Creating model")
117
  config = get_blip_config("large")
118
 
 
128
 
129
  model = model.to(device)
130
  model.eval()
 
 
131
 
132
  transform = transform_test(384)
133
 
134
+ df = pd.read_json("my_recipes.json")
 
135
 
 
136
  tar_img_feats = []
137
  for _id in df["id_"].tolist():
138
+ tar_img_feats.append(torch.load("./datasets/sidechef/blip-embs-large/{:07d}.pth".format(_id)).unsqueeze(0))
139
 
140
  tar_img_feats = torch.cat(tar_img_feats, dim=0)
141
 
 
142
  class Chat:
143
 
144
  def __init__(self, model, transform, dataframe, tar_img_feats, device='cuda:0', stopping_criteria=None):
 
179
 
180
 
181
  chat = Chat(model,transform,df,tar_img_feats, device)
 
182
 
183
 
184
+ def answer_generator(formated_input, session_id):
185
+ # QA system prompt and chain
186
+ qa_system_prompt = """
187
+ You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on provide context ans user query.
188
+ Operational Guidelines:
189
+ 1. Input Structure:
190
+ - Context: You may receive contextual information related to recipes, such as specific data sets, user preferences, dietary restrictions, or previously selected dishes.
191
+ - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient substitutions, cooking instructions, and more.
192
+ 2. Response Strategy:
193
+ - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance.
194
+ - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, kindly state that you do not have require information.
195
+ Core Functionalities:
196
+ - Nutritional Information: Accurately provide nutritional values for each recipe, including calories, macronutrients (proteins, fats, carbohydrates), and essential vitamins and minerals, using contextual data when available.
197
+ - Ingredient Details: List all ingredients required for recipes, including substitute options for dietary restrictions or ingredient availability, utilizing context when relevant.
198
+ - Step-by-Step Cooking Instructions: Deliver clear, easy-to-follow instructions for preparing and cooking meals, informed by any provided contextual data.
199
+ - Recipe Recommendations: Suggest dishes based on user preferences, dietary restrictions, available ingredients, and contextual data if provided.
200
+ Additional Instructions:
201
+ - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
202
+ - Clarity and Coherence: Ensure that all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
203
+ - Substitute Suggestions: When suggesting ingredient substitutes, consider user preferences and dietary restrictions outlined in the context or user query.
204
+ - Dynamic Adaptation: Adapt your responses dynamically based on whether the context is relevant to the user's current request, ensuring optimal use of available information.
205
+ Don't mention about context in the response, format the answer in a natural and friendly way.
206
+ Context:
207
+ {context}
208
+ """
209
+ qa_prompt = ChatPromptTemplate.from_messages(
210
+ [
211
+ ("system", qa_system_prompt),
212
+ ("human", "{input}")
213
+ ]
214
+ )
215
+
216
+ # Create the base chain
217
+ base_chain = qa_prompt | llm | StrOutputParser()
218
+
219
+ # Wrap the chain with message history
220
+ question_answer_chain = RunnableWithMessageHistory(
221
+ base_chain,
222
+ lambda session_id: ChatMessageHistory(), # This creates a new history for each session
223
+ input_messages_key="input",
224
+ history_messages_key="chat_history"
225
+ )
226
+
227
+ response = question_answer_chain.invoke(formated_input, config={"configurable": {"session_id": session_id}})
228
+
229
+ return response
230
+
231
+
232
+
233
+ ### Router
234
+ import json
235
+ from langchain_core.messages import HumanMessage, SystemMessage
236
+
237
+ def router_node(query):
238
+ # Prompt
239
+ router_instructions = """You are an expert at determining the appropriate task for a user’s question based on chat history and the current query context. You have two available tasks:
240
+
241
+ 1. Retrieval: Fetch information based on user's chat history and current query.
242
+ 2. Recommendation/Suggestion: Recommend recipes to users based on the query.
243
+
244
+ Return a JSON response with a single key named “task” indicating either “retrieval” or “recommendation” based on your decision.
245
+ """
246
+ response = router.invoke(
247
+ [SystemMessage(content=router_instructions)]
248
+ + [
249
+ HumanMessage(
250
+ content=query
251
+ )
252
+ ]
253
+ )
254
+ res = json.loads(response.content)
255
+ return res['task']
256
+
257
+ def recommendation_node(query):
258
+ prompt = """
259
+ You are a helpful assistant that writes Python code to filter recipes from a JSON filr based o the user query. \n
260
+ JSON file path = 'recipes.json' \n
261
+ The JSON file is a list of recipes with the following structure: \n
262
+ {
263
+ "recipe_name": string,
264
+ "recipe_time": integer,
265
+ "recipe_yields": string,
266
+ "recipe_ingredients": list of ingredients,
267
+ "recipe_instructions": list of instruections,
268
+ "recipe_image": string,
269
+ "blogger": string,
270
+ "recipe_nutrients": JSON object with key value pairs such as "protein: 10g",
271
+ "tags": list of tags related to recipe
272
+ } \n
273
+
274
+ Here is the example of an recipe json object from the JSON data: \n
275
+ {
276
+ "recipe_name": "Asian Potato Salad with Seven Minute Egg",
277
+ "recipe_time": 0,
278
+ "recipe_yields": "4 servings",
279
+ "recipe_ingredients": [
280
+ "2 1/2 cup Multi-Colored Fingerling Potato",
281
+ "3/4 cup Celery",
282
+ "1/4 cup Red Onion",
283
+ "2 tablespoon Fresh Parsley",
284
+ "1/3 cup Mayonnaise",
285
+ "1 tablespoon Chili Garlic Sauce",
286
+ "1 teaspoon Hoisin Sauce",
287
+ "1 splash Soy Sauce",
288
+ "to taste Salt",
289
+ "to taste Ground Black Pepper",
290
+ "4 Egg"
291
+ ],
292
+ "recipe_instructions": "Fill a large stock pot with water.\nAdd the Multi-Colored Fingerling Potato (2 1/2 cup) and bring water to a boil. Boil the potatoes for 20 minutes or until fork tender.\nDrain the potatoes and let them cool completely.\nMeanwhile, mix together in a small bowl Mayonnaise (1/3 cup), Chili Garlic Sauce (1 tablespoon), Hoisin Sauce (1 teaspoon), and Soy Sauce (1 splash).\nTo make the Egg (4), fill a stock pot with water and bring to a boil Gently add the eggs to the water and set a timer for seven minutes.\nThen move the eggs to an ice bath to cool completely. Once cooled, crack the egg slightly and remove the shell. Slice in half when ready to serve.\nNext, halve the cooled potatoes and place into a large serving bowl. Add the Ground Black Pepper (to taste), Celery (3/4 cup), Red Onion (1/4 cup), and mayo mixture. Toss to combine adding Salt (to taste) and Fresh Parsley (2 tablespoon).\nTop with seven minute eggs and serve. Enjoy!",
293
+ "recipe_image": "https://www.sidechef.com/recipe/eeeeeceb-493e-493d-8273-66c800821b13.jpg?d=1408x1120",
294
+ "blogger": "sidechef.com",
295
+ "recipe_nutrients": {
296
+ "calories": "80 calories",
297
+ "proteinContent": "2.1 g",
298
+ "fatContent": "6.2 g",
299
+ "carbohydrateContent": "3.9 g",
300
+ "fiberContent": "0.5 g",
301
+ "sugarContent": "0.4 g",
302
+ "sodiumContent": "108.0 mg",
303
+ "saturatedFatContent": "1.2 g",
304
+ "transFatContent": "0.0 g",
305
+ "cholesterolContent": "47.4 mg",
306
+ "unsaturatedFatContent": "3.8 g"
307
+ },
308
+ "tags": [
309
+ "Salad",
310
+ "Lunch",
311
+ "Brunch",
312
+ "Appetizers",
313
+ "Side Dish",
314
+ "Budget-Friendly",
315
+ "Vegetarian",
316
+ "Pescatarian",
317
+ "Eggs",
318
+ "Potatoes",
319
+ "Dairy-Free",
320
+ "Shellfish-Free"
321
+ ]
322
+ } \n
323
+
324
+ Based on the user query, provide a Python function to filter the JSON data. The output of the function should be a list of json objects. \n
325
+
326
+ Recipe filtering instructions:
327
+ - If a user asked for the highest nutrient recipe such as "high protein or high calories" then filtered recipes should be the top highest recipes from all the recipes with high nutrient.
328
+ - sort or rearrange recipes based which recipes are more appropriate for the user.
329
+
330
+ Your output instructions:
331
+ - The function name should be filter_recipes. The input to the function should be file name.
332
+ - The length of output recipes should not be more than 6.
333
+ - Only give me output function. Do not call the function.
334
+ - Give the python function as a key named "code" in a json format.
335
+ - Do not include any other text with the output, only give python code.
336
+ - If you do not follow the above given instructions, the chat may be terminated.
337
+ """
338
+ max_tries = 3
339
+ while True:
340
+ try:
341
+ # llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
342
+ response = client.chat.completions.create(
343
+ model="gpt-4o-mini",
344
+ messages=[
345
+ {"role": "system", "content": prompt},
346
+ {
347
+ "role": "user",
348
+ "content": query
349
+ }
350
+ ]
351
+ )
352
+
353
+ content = response.choices[0].message.content
354
+
355
+ res = json.loads(content)
356
+ script = res['code']
357
+ exec(script, globals())
358
+ filtered_recipes = filter_recipes('recipes.json')
359
+ if len(filtered_recipes) > 0:
360
+ return filtered_recipes
361
+ except Exception as e:
362
+ print(e)
363
+ if max_tries <= 0:
364
+ return []
365
+ else:
366
+ max_tries -= 1
367
+ return filtered_recipes
368
+
369
+
370
+ def answer_formatter_node(question, context):
371
+ prompt = f""" You are an highly clever question-answering assistant trained to provide clear and concise answers based on the user query and provided context.
372
+ Your task is to generated answers for the user query based on the context provided.
373
+ Instructions for your response:
374
+ 1. Directly answer the user query using only the information provided in the context.
375
+ 2. Ensure your response is clear and concise.
376
+ 3. Mention only details related to the recipe, including the recipe name, instructions, nutrients, yield, ingredients, and image.
377
+ 4. Do not include any information that is not related to the recipe context.
378
+
379
+ Please format an answer based on the following user question and context provided:
380
+
381
+ User Question:
382
+ {question}
383
+
384
+ Context:
385
+ {context}
386
+ """
387
+ response = answer_formatter.invoke(
388
+ [SystemMessage(content=prompt)]
389
+ )
390
+ res = response.content
391
+ return res
392
 
393
+ CURR_CONTEXT = ''
394
 
395
  # @spaces.GPU
396
+ def get_answer(image=[], message='', sessionID='abc123'):
397
+ global CURR_CONTEXT
398
+ if len(image) > 0:
399
+ try:
400
+ # Process the image and message here
401
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
402
+ chat = Chat(model,transform,df,tar_img_feats, device)
403
+ chat.encode_image(image)
404
+ data = chat.ask()
405
+ CURR_CONTEXT = data
406
+ formated_input = {
407
+ 'input': message,
408
+ 'context': data
409
+ }
410
+ response = answer_generator(formated_input, session_id=sessionID)
411
+ except Exception as e:
412
+ print(e)
413
+ response = {'content':"An error occurred while processing your request."}
414
+ elif len(image) == 0 and message is not None:
415
+ print("I am here")
416
+ task = router_node(message)
417
+ if task == 'retrieval':
418
+ recipes = recommendation_node(message)
419
+ print(recipes)
420
+ if not recipes:
421
+ response = {'content':"An error occurred while processing your request."}
422
+ response = answer_formatter_node(message, recipes)
423
+ else:
424
+ formated_input = {
425
+ 'input': message,
426
+ 'context': CURR_CONTEXT
427
+ }
428
+ response = answer_generator(formated_input, session_id=sessionID)
429
+
430
+ return response
431
+
432
+ # Function to handle WebSocket connection
433
+ @socketio.on('ping')
434
+ def handle_connect():
435
+ emit('Ping-return', {'message': 'Connected'}, room=request.sid)
436
+
437
+
438
+ # Function to handle WebSocket connection
439
+ @socketio.on('connect')
440
+ def handle_connect():
441
+ print(f"Client connected: {request.sid}")
442
+
443
+ # Function to handle WebSocket disconnection
444
+ @socketio.on('disconnect')
445
+ def handle_disconnect():
446
+ print(f"Client disconnected: {request.sid}")
447
+
448
+ import json
449
+ import base64
450
+ from PIL import Image
451
+ from io import BytesIO
452
+ import torchvision.transforms as transforms
453
+
454
+ # Dictionary to store incomplete image data by session
455
+ session_store = {}
456
+
457
+ @socketio.on('message')
458
+ def handle_message(data):
459
+ global session_store
460
+ global CURR_CONTEXT
461
+ context = "No data available"
462
+ session_id = request.sid
463
+ if session_id not in session_store:
464
+ session_store[session_id] = {'image_data': b"", 'message': None, 'image_received': False}
465
+
466
+ if 'message' in data:
467
+ session_store[session_id]['message'] = data['message']
468
+
469
+ # Handle image chunk data
470
+ if 'image' in data:
471
+ try:
472
+ # Append the incoming image chunk
473
+ session_store[session_id]['image_data'] += data['image']
474
+
475
+ except Exception as e:
476
+ print(f"Error processing image chunk: {str(e)}")
477
+ emit('response', "An error occurred while receiving the image chunk.", room=session_id)
478
+ return
479
+
480
+ if session_store[session_id]['image_data'] or session_store[session_id]['message']:
481
+ try:
482
+ image_bytes = session_store[session_id]['image_data']
483
+ # print("checkpoint 2")
484
+ if isinstance(image_bytes, str):
485
+ image_bytes = base64.b64decode(image_bytes)
486
+ image = Image.open(BytesIO(image_bytes))
487
+ image_array = np.array(image)
488
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
489
+ chat = Chat(model, transform, df, tar_img_feats, device)
490
+ chat.encode_image(image_array)
491
+ context = chat.ask()
492
+ CURR_CONTEXT = context
493
+ message = data['message']
494
+ formated_input = {
495
+ 'input': message,
496
+ 'context': json.dumps(context)
497
+ }
498
+ # Invoke question_answer_chain and stream the response
499
+ response = answer_generator(formated_input, session_id=session_id)
500
+ emit('response', response, room=session_id)
501
+
502
+ except Exception as e:
503
+ print(f"Error processing image or message: {str(e)}")
504
+ emit('response', "An error occurred while processing your request.", room=session_id)
505
+ return
506
+ finally:
507
+ # Clear session data after processing
508
+ session_store.pop(session_id, None)
509
+ else:
510
+ message = data['message']
511
+ task = router_node(message)
512
+ print(task)
513
+ if task == 'retrieval':
514
+ formated_input = {
515
+ 'input': message,
516
+ 'context': json.dumps(CURR_CONTEXT)
517
+ }
518
+ response = answer_generator(formated_input, session_id=session_id)
519
+ emit('response', response, room=session_id)
520
+ else:
521
+ response = recommendation_node(message)
522
+ print(response)
523
+ # response = answer_formatter_node(message, recipes)
524
+ if response is None:
525
+ response = {'content':"An error occurred while processing your request."}
526
+
527
+ emit('json_response', response, room=session_id)
528
+ session_store.pop(session_id, None)
529
+
530
+
531
+
532
+ import requests
533
+ from PIL import Image
534
+ import numpy as np
535
+ from io import BytesIO
536
+
537
+ def download_image_to_numpy(url):
538
+ print("Image URL: ", url)
539
+ # Send a GET request to the URL to download the image
540
+ response = requests.get(url)
541
+
542
+ # Check if the request was successful
543
+ if response.status_code == 200:
544
+ # Open the image using PIL and convert it to RGB format
545
+ image = Image.open(BytesIO(response.content)).convert('RGB')
546
+
547
+ # Convert the image to a NumPy array
548
+ image_array = np.array(image)
549
+
550
+ return image_array
551
+ else:
552
+ raise Exception(f"Failed to download image. Status code: {response.status_code}")
553
+
554
+ @socketio.on('example')
555
+ def handle_message(data):
556
+ img_url = data['img_url']
557
+ message = data['message']
558
+ session_id = request.sid
559
+ image_array = download_image_to_numpy(img_url)
560
+ response = get_answer(image=image_array, message=message, sessionID=request.sid)
561
+ emit('response', response, room=session_id)
562
+ return response
563
+
564
+ # Home route
565
+ @app.route("/")
566
+ def index_view():
567
+ return render_template('chat.html')
568
+
569
+ # Main function to run the app
570
+ if __name__ == '__main__':
571
+ socketio.run(app, debug=False)