1MR commited on
Commit
b5c8ca8
·
verified ·
1 Parent(s): 180632d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import JSONResponse
3
+ import tensorflow as tf
4
+ import numpy as np
5
+ import shutil
6
+ import os
7
+ from huggingface_hub import InferenceClient
8
+ import json
9
+ import requests
10
+ from langchain_core.output_parsers import StrOutputParser
11
+ from langchain_core.runnables import chain
12
+ from langchain_huggingface import HuggingFaceEndpoint,ChatHuggingFace
13
+ from langchain_core.prompts import ChatPromptTemplate
14
+ from langchain_core.runnables import RunnableParallel
15
+ from PIL import Image
16
+ import json
17
+ import requests
18
+
19
+ # Initialize FastAPI app
20
+ app = FastAPI()
21
+
22
+ chat_nutrition_prompt = ChatPromptTemplate.from_template(
23
+ '''
24
+ Provide the nutrition information (Calories, Protein, Carbohydrates, Dietary Fiber, Sugars, Fat, Sodium, Potassium, Vitamin C, Vitamin B6) for {prediction} per 100 grams, Output the information as a concise, formatted list without repetition.
25
+ '''
26
+ )
27
+
28
+ chat_health_benefits_prompt = ChatPromptTemplate.from_template(
29
+ '''
30
+ Provide the health benefits and considerations for {prediction}. Additionally, include practical tips for making {prediction} healthier. Keep the response focused on these two aspects only.
31
+ '''
32
+ )
33
+ chat_recipes_prompt = ChatPromptTemplate.from_template(
34
+ '''
35
+ Tell me about the two most famous recipes for {prediction}. Include the ingredients only.
36
+ '''
37
+ )
38
+
39
+ def load_and_prep_image(uploaded_file, img_shape=224):
40
+ img = Image.open(uploaded_file) # Open uploaded image
41
+ img = img.resize((img_shape, img_shape)) # Resize image
42
+ img = tf.convert_to_tensor(img, dtype=tf.float32) # Convert to tensor
43
+ return img
44
+ @chain
45
+ def predict_label(uploaded_file):
46
+ img = load_and_prep_image(uploaded_file, img_shape=224) # Preprocess image
47
+ img = tf.expand_dims(img, axis=0) # Add batch dimension
48
+
49
+ pred = model.predict(img) # Model prediction
50
+ pred_class_index = np.argmax(pred, axis=1)[0] # Get highest probability index
51
+ pred_class_name = class_labels[pred_class_index] # Convert index to class name
52
+ return pred_class_name
53
+
54
+ model = tf.keras.models.load_model("NewVersionModelOptimized40V2.keras")
55
+ class_labels = {0: 'Baked Potato',1: 'Burger',2: 'Cake',3: 'Chips',4: 'Crispy Chicken',5: 'Croissant',
56
+ 6: 'Dount',7: 'Dragon Fruit',8: 'Frise',9: 'Hot Dog',10: 'Jalapeno',11: 'Kiwi',12: 'Lemon',13: 'Lettuce',
57
+ 14: 'Mango',15: 'Onion',16: 'Orange',17: 'Pizza',18: 'Taquito',19: 'apple',20: 'banana',21: 'beetroot',
58
+ 22: 'bell pepper',23: 'bread',24: 'cabbage',25: 'carrot',26: 'cauliflower',27: 'cheese',28: 'chilli pepper',
59
+ 29: 'corn',30: 'crab',31: 'cucumber',32: 'eggplant',33: 'eggs',34: 'garlic',36: 'grapes',37: 'milk',
60
+ 38: 'salamon',39: 'yogurt'}
61
+
62
+ api_key='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1'
63
+ llm = HuggingFaceEndpoint(
64
+ repo_id="Qwen/Qwen2.5-72B-Instruct",
65
+ max_new_tokens=512,
66
+ client=api_key[:-1]
67
+ )
68
+ chat = ChatHuggingFace(llm=llm, verbose=True)
69
+
70
+ str_output_parser = StrOutputParser()
71
+
72
+ chain_label = predict_label
73
+ chain1= chat_nutrition_prompt | chat | str_output_parser
74
+ chain2= chat_health_benefits_prompt | chat | str_output_parser
75
+ chain3= chat_recipes_prompt | chat | str_output_parser
76
+
77
+ chain_parallel = RunnableParallel({'chat_nutrition_prompt':chain1,
78
+ 'chat_health_benefits_prompt':chain2,
79
+ 'chat_recipes_prompt':chain3})
80
+
81
+ @app.post("/predictNUT")
82
+ async def predict_image_and_nutrition(file: UploadFile = File(...)):
83
+ try:
84
+ # Save the uploaded file
85
+ file_location = f"./temp_{file.filename}"
86
+ with open(file_location, "wb") as f:
87
+ shutil.copyfileobj(file.file, f)
88
+
89
+ # Predict the label using the same prediction logic
90
+ with open(file_location, "rb") as image_file:
91
+ prediction = predict_label.invoke(image_file)
92
+
93
+ # Remove the temporary file
94
+ # os.remove(file_location)
95
+
96
+ result = chain_parallel.invoke(prediction)
97
+
98
+ return {
99
+ "Predicted_label": prediction,
100
+ "Nutrition_info": result['chat_nutrition_prompt'],
101
+ "Information": result['chat_health_benefits_prompt'],
102
+ "Recipes":result['chat_recipes_prompt']
103
+ }
104
+ except Exception as e:
105
+ return JSONResponse(
106
+ status_code=500,
107
+ content={"error": f"An error occurred: {str(e)}"}
108
+ )