Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 10 |
+
# Initialize FastAPI app
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Class labels
|
| 14 |
+
class_labels = {
|
| 15 |
+
0: 'Baked Potato', 1: 'Burger', 2: 'Crispy Chicken', 3: 'Donut', 4: 'Fries',
|
| 16 |
+
5: 'Hot Dog', 6: 'Jalapeno', 7: 'Kiwi', 8: 'Lemon', 9: 'Lettuce',
|
| 17 |
+
10: 'Mango', 11: 'Onion', 12: 'Orange', 13: 'Pizza', 14: 'Taquito',
|
| 18 |
+
15: 'Apple', 16: 'Banana', 17: 'Beetroot', 18: 'Bell Pepper', 19: 'Bread',
|
| 19 |
+
20: 'Cabbage', 21: 'Carrot', 22: 'Cauliflower', 23: 'Cheese',
|
| 20 |
+
24: 'Chilli Pepper', 25: 'Corn', 26: 'Crab', 27: 'Cucumber',
|
| 21 |
+
28: 'Eggplant', 29: 'Eggs', 30: 'Garlic', 31: 'Ginger', 32: 'Grapes',
|
| 22 |
+
33: 'Milk', 34: 'Salmon', 35: 'Yogurt'
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Load the trained model
|
| 26 |
+
model = tf.keras.models.load_model("model_unfreezeNewCorrectpredict.keras")
|
| 27 |
+
|
| 28 |
+
# Image preprocessing function
|
| 29 |
+
def load_and_prep_image(file_path, img_shape=224):
|
| 30 |
+
img = tf.io.read_file(file_path)
|
| 31 |
+
img = tf.image.decode_image(img, channels=3)
|
| 32 |
+
img = tf.image.resize(img, size=[img_shape, img_shape])
|
| 33 |
+
img = tf.expand_dims(img, axis=0)
|
| 34 |
+
return img
|
| 35 |
+
|
| 36 |
+
# Predict label function
|
| 37 |
+
def predict_label(model, image_path, class_names):
|
| 38 |
+
img = load_and_prep_image(image_path, img_shape=224)
|
| 39 |
+
pred = model.predict(img)
|
| 40 |
+
pred_class_index = np.argmax(pred, axis=1)[0]
|
| 41 |
+
pred_class_name = class_names[pred_class_index]
|
| 42 |
+
return pred_class_name
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@app.get("/")
|
| 46 |
+
def read_root():
|
| 47 |
+
return {"message": "This is My Nutrionguid App"}
|
| 48 |
+
|
| 49 |
+
# API endpoint for prediction
|
| 50 |
+
@app.post("/predict")
|
| 51 |
+
async def predict_image(file: UploadFile = File(...)):
|
| 52 |
+
try:
|
| 53 |
+
# Save the uploaded file
|
| 54 |
+
file_location = f"./temp_{file.filename}"
|
| 55 |
+
with open(file_location, "wb") as f:
|
| 56 |
+
shutil.copyfileobj(file.file, f)
|
| 57 |
+
|
| 58 |
+
# Predict the label
|
| 59 |
+
prediction = predict_label(model, file_location, class_labels)
|
| 60 |
+
|
| 61 |
+
# Remove the temporary file
|
| 62 |
+
os.remove(file_location)
|
| 63 |
+
|
| 64 |
+
return {"predicted_label": prediction}
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return JSONResponse(
|
| 67 |
+
status_code=500,
|
| 68 |
+
content={"error": f"An error occurred: {str(e)}"}
|
| 69 |
+
)
|
| 70 |
+
@app.post("/predictNUT")
|
| 71 |
+
async def predict_image_and_nutrition(file: UploadFile = File(...)):
|
| 72 |
+
try:
|
| 73 |
+
# Save the uploaded file
|
| 74 |
+
file_location = f"./temp_{file.filename}"
|
| 75 |
+
with open(file_location, "wb") as f:
|
| 76 |
+
shutil.copyfileobj(file.file, f)
|
| 77 |
+
|
| 78 |
+
# Predict the label using the same prediction logic
|
| 79 |
+
prediction = predict_label(model, file_location, class_labels)
|
| 80 |
+
|
| 81 |
+
# Remove the temporary file
|
| 82 |
+
os.remove(file_location)
|
| 83 |
+
|
| 84 |
+
# Define the repository ID and your token
|
| 85 |
+
repo_id = "microsoft/Phi-3-mini-4k-instruct"
|
| 86 |
+
api_token = "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21"
|
| 87 |
+
|
| 88 |
+
# Initialize the InferenceClient with your token
|
| 89 |
+
llm_client = InferenceClient(
|
| 90 |
+
model=repo_id,
|
| 91 |
+
token=api_token[:-2], # Pass the token here
|
| 92 |
+
timeout=120,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Function to call the LLM
|
| 96 |
+
def call_llm(inference_client: InferenceClient, prompt: str):
|
| 97 |
+
response = inference_client.post(
|
| 98 |
+
json={
|
| 99 |
+
"inputs": prompt,
|
| 100 |
+
"parameters": {"max_new_tokens": 500},
|
| 101 |
+
"task": "text-generation",
|
| 102 |
+
},
|
| 103 |
+
)
|
| 104 |
+
return json.loads(response.decode())[0]["generated_text"]
|
| 105 |
+
|
| 106 |
+
# Use the prediction to generate nutrition information
|
| 107 |
+
prompt = f"Nutrition information for {prediction} in formatted list"
|
| 108 |
+
response = call_llm(llm_client, prompt)
|
| 109 |
+
|
| 110 |
+
return {"predicted_label": prediction, "nutrition_info": response}
|
| 111 |
+
except Exception as e:
|
| 112 |
+
return JSONResponse(
|
| 113 |
+
status_code=500,
|
| 114 |
+
content={"error": f"An error occurred: {str(e)}"}
|
| 115 |
+
)
|