Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
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 |
-
|
33 |
|
34 |
import json
|
35 |
from openai import OpenAI
|
@@ -51,14 +51,13 @@ os.environ["TOKENIZERS_PARALLELISM"] = 'true'
|
|
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,6 +77,7 @@ class StoppingCriteriaSub(StoppingCriteria):
|
|
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,15 +128,11 @@ model = blip_embs(
|
|
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,7 +141,7 @@ tar_img_feats = torch.cat(tar_img_feats, dim=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,209 +179,30 @@ class Chat:
|
|
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
|
334 |
-
Operational Guidelines:
|
335 |
-
1. Input Structure:
|
336 |
-
- Context: You may receive contextual information related to recipes, such as specific
|
337 |
-
- User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient, cooking instructions, and more.
|
338 |
-
2. Response Strategy:
|
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.
|
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
|
341 |
-
|
342 |
-
-
|
343 |
-
-
|
344 |
-
-
|
345 |
-
|
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:
|
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 |
-
|
388 |
-
|
389 |
Context:
|
390 |
{context}
|
391 |
"""
|
@@ -421,8 +238,8 @@ def router_node(query):
|
|
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
|
425 |
-
2. Recommendation/Suggestion: Recommend
|
426 |
|
427 |
Return a JSON response with a single key named “task” indicating either “retrieval” or “recommendation” based on your decision.
|
428 |
"""
|
@@ -447,14 +264,14 @@ def recommendation_node(query):
|
|
447 |
"recipe_time": integer,
|
448 |
"recipe_yields": string,
|
449 |
"recipe_ingredients": list of ingredients,
|
450 |
-
"recipe_instructions": list of
|
451 |
"recipe_image": string,
|
452 |
"blogger": string,
|
453 |
-
"recipe_nutrients": JSON object with key
|
454 |
-
"tags": list of tags related to
|
455 |
} \n
|
456 |
|
457 |
-
Here is the example of
|
458 |
{
|
459 |
"recipe_name": "Asian Potato Salad with Seven Minute Egg",
|
460 |
"recipe_time": 0,
|
@@ -504,20 +321,19 @@ def recommendation_node(query):
|
|
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
|
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
|
511 |
-
- sort or rearrange recipes based
|
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
|
516 |
- The length of output recipes should not be more than 6.
|
517 |
-
- Only give me
|
518 |
-
- Give the
|
519 |
-
- Do not include any other text with the output, only give
|
520 |
-
- If you do not follow the above
|
521 |
"""
|
522 |
max_tries = 3
|
523 |
while True:
|
@@ -575,7 +391,59 @@ def answer_formatter_node(question, context):
|
|
575 |
return res
|
576 |
|
577 |
CURR_CONTEXT = ''
|
578 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
579 |
|
580 |
import json
|
581 |
import base64
|
@@ -586,12 +454,12 @@ import torchvision.transforms as transforms
|
|
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,7 +474,8 @@ def handle_message(data):
|
|
606 |
|
607 |
except Exception as e:
|
608 |
print(f"Error processing image chunk: {str(e)}")
|
609 |
-
|
|
|
610 |
|
611 |
if session_store[session_id]['image_data'] or session_store[session_id]['message']:
|
612 |
try:
|
@@ -628,11 +497,12 @@ def handle_message(data):
|
|
628 |
}
|
629 |
# Invoke question_answer_chain and stream the response
|
630 |
response = answer_generator(formated_input, session_id=session_id)
|
631 |
-
|
632 |
|
633 |
except Exception as e:
|
634 |
print(f"Error processing image or message: {str(e)}")
|
635 |
-
|
|
|
636 |
finally:
|
637 |
# Clear session data after processing
|
638 |
session_store.pop(session_id, None)
|
@@ -646,15 +516,18 @@ def handle_message(data):
|
|
646 |
'context': json.dumps(CURR_CONTEXT)
|
647 |
}
|
648 |
response = answer_generator(formated_input, session_id=session_id)
|
649 |
-
|
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 |
-
|
657 |
-
|
|
|
|
|
|
|
658 |
|
659 |
import requests
|
660 |
from PIL import Image
|
@@ -662,6 +535,7 @@ import numpy as np
|
|
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,96 +551,21 @@ def download_image_to_numpy(url):
|
|
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=
|
|
|
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("/get-answer", 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." + str(e)}
|
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." + str(e)}
|
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 |
# Home route
|
764 |
@app.route("/")
|
765 |
def index_view():
|
766 |
return render_template('chat.html')
|
767 |
|
768 |
-
|
769 |
-
|
770 |
# Main function to run the app
|
771 |
if __name__ == '__main__':
|
772 |
-
|
|
|
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 |
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 |
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 |
|
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 |
|
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 |
|
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 |
# 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 |
"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 |
]
|
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 |
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 |
# 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 |
|
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 |
}
|
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 |
'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 |
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 |
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)
|