jflo commited on
Commit
334dbbc
·
verified ·
1 Parent(s): 5d928b1

Added classification and recipe generator

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -5,12 +5,20 @@ import uvicorn
5
 
6
  from PIL import Image
7
 
 
 
8
  import torch
9
  import torch.nn.functional as F
10
  from torchvision import transforms
11
 
12
  app = FastAPI()
13
 
 
 
 
 
 
 
14
  def transform_img(img: Image.Image) -> torch.tensor:
15
  # Transformations that will be applied
16
  the_transform = transforms.Compose([
@@ -36,16 +44,14 @@ def classify_img(img: Image.Image) -> dict:
36
 
37
  # Converting values to softmax values
38
  result = F.softmax(result,dim=1)
39
- # Grabbing top 3 indices and probabilities for each index
40
- top3_prob, top3_catid = torch.topk(result,3)
41
 
42
  # Dictionary I will display
43
  model_output = {}
44
- for i in range(top3_prob.size(1)):
45
- fast_food_name = class_names[top3_catid[0][i].item()]
46
- probability = round(top3_prob[0][i].item() * 100, 2)
47
-
48
- model_output[f"top{i+1}"] = {"name": fast_food_name, "probability": probability}
49
 
50
  return model_output
51
 
@@ -53,9 +59,12 @@ def classify_img(img: Image.Image) -> dict:
53
  async def upload(file: UploadFile = File(...)) -> JSONResponse:
54
 
55
  pil_img = Image.open(file.file)
56
- result = classify_img(pil_img)
57
 
58
- return JSONResponse(content=result, status_code=201)
 
 
 
59
 
60
  @app.get("/")
61
  def api_home():
 
5
 
6
  from PIL import Image
7
 
8
+ import requests
9
+
10
  import torch
11
  import torch.nn.functional as F
12
  from torchvision import transforms
13
 
14
  app = FastAPI()
15
 
16
+ def grab_recipes(food: str):
17
+ result = requests.get(f'https://api.spoonacular.com/recipes/complexSearch?apiKey={}&query={food}&addRecipeInformation=true&number=2')
18
+ recipes = result.json()
19
+
20
+ return recipes
21
+
22
  def transform_img(img: Image.Image) -> torch.tensor:
23
  # Transformations that will be applied
24
  the_transform = transforms.Compose([
 
44
 
45
  # Converting values to softmax values
46
  result = F.softmax(result,dim=1)
47
+ # Grabbing top 1 index and probability
48
+ top1_prob, top1_catid = torch.topk(result,1)
49
 
50
  # Dictionary I will display
51
  model_output = {}
52
+ fast_food_name = class_names[top1_catid[0][0].item()]
53
+ probability = round(top1_prob[0][0].item() * 100, 2)
54
+ model_output[f"winner"] = {"name": fast_food_name, "probability": probability}
 
 
55
 
56
  return model_output
57
 
 
59
  async def upload(file: UploadFile = File(...)) -> JSONResponse:
60
 
61
  pil_img = Image.open(file.file)
62
+ classifier_result = classify_img(pil_img)
63
 
64
+ recipes = grab_recipes(classifier_result['winner']['name'])
65
+ recipes['fastfoodwinner'] = classifier_result
66
+
67
+ return JSONResponse(content=recipes, status_code=201)
68
 
69
  @app.get("/")
70
  def api_home():