Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108from fastapi import FastAPI, File, UploadFilefrom fastapi.responses import JSONResponseimport tensorflow as tfimport numpy as npimport shutilimport osfrom huggingface_hub import InferenceClientimport jsonimport requestsfrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.runnables import chainfrom langchain_huggingface import HuggingFaceEndpoint,ChatHuggingFacefrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables import RunnableParallelfrom PIL import Imageimport jsonimport requests
# Initialize FastAPI appapp = FastAPI()
chat_nutrition_prompt = ChatPromptTemplate.from_template( ''' 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. ''')
chat_health_benefits_prompt = ChatPromptTemplate.from_template( ''' 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. ''')chat_recipes_prompt = ChatPromptTemplate.from_template( ''' Tell me about the two most famous recipes for {prediction}. Include the ingredients only. ''')
def load_and_prep_image(uploaded_file, img_shape=224): img = Image.open(uploaded_file) # Open uploaded image img = img.resize((img_shape, img_shape)) # Resize image img = tf.convert_to_tensor(img, dtype=tf.float32) # Convert to tensor return img@chaindef predict_label(uploaded_file): img = load_and_prep_image(uploaded_file, img_shape=224) # Preprocess image img = tf.expand_dims(img, axis=0) # Add batch dimension pred = model.predict(img) # Model prediction pred_class_index = np.argmax(pred, axis=1)[0] # Get highest probability index pred_class_name = class_labels[pred_class_index] # Convert index to class name return pred_class_name model = tf.keras.models.load_model("NewVersionModelOptimized40V2.keras")class_labels = {0: 'Baked Potato',1: 'Burger',2: 'Cake',3: 'Chips',4: 'Crispy Chicken',5: 'Croissant', 6: 'Dount',7: 'Dragon Fruit',8: 'Frise',9: 'Hot Dog',10: 'Jalapeno',11: 'Kiwi',12: 'Lemon',13: 'Lettuce', 14: 'Mango',15: 'Onion',16: 'Orange',17: 'Pizza',18: 'Taquito',19: 'apple',20: 'banana',21: 'beetroot', 22: 'bell pepper',23: 'bread',24: 'cabbage',25: 'carrot',26: 'cauliflower',27: 'cheese',28: 'chilli pepper', 29: 'corn',30: 'crab',31: 'cucumber',32: 'eggplant',33: 'eggs',34: 'garlic',36: 'grapes',37: 'milk', 38: 'salamon',39: 'yogurt'}
api_key='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1'llm = HuggingFaceEndpoint( repo_id="Qwen/Qwen2.5-72B-Instruct", max_new_tokens=512, client=api_key[:-1])chat = ChatHuggingFace(llm=llm, verbose=True)
str_output_parser = StrOutputParser()
chain_label = predict_labelchain1= chat_nutrition_prompt | chat | str_output_parserchain2= chat_health_benefits_prompt | chat | str_output_parserchain3= chat_recipes_prompt | chat | str_output_parser
chain_parallel = RunnableParallel({'chat_nutrition_prompt':chain1, 'chat_health_benefits_prompt':chain2, 'chat_recipes_prompt':chain3})
@app.post("/predictNUT")async def predict_image_and_nutrition(file: UploadFile = File(...)): try: # Save the uploaded file file_location = f"./temp_{file.filename}" with open(file_location, "wb") as f: shutil.copyfileobj(file.file, f) # Predict the label using the same prediction logic with open(file_location, "rb") as image_file: prediction = predict_label.invoke(image_file) # Remove the temporary file # os.remove(file_location)
result = chain_parallel.invoke(prediction)
return { "Predicted_label": prediction, "Nutrition_info": result['chat_nutrition_prompt'], "Information": result['chat_health_benefits_prompt'], "Recipes":result['chat_recipes_prompt'] } except Exception as e: return JSONResponse( status_code=500, content={"error": f"An error occurred: {str(e)}"} )app.pyAdded+108