File size: 996 Bytes
e42a40a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import gradio as gr
import requests
import json
import os

API_URL = "https://api-inference.huggingface.co/models/davidaf3/ReverseNutrition_FCNutr"
headers = {"Authorization": f"Bearer {os.environ['API_TOKEN']}"}

def predict(image_file):
    with open(image_file, "rb") as f:
      data = f.read()
    response = requests.request("POST", API_URL, headers=headers, data=data)
    predictions = json.loads(response.content.decode("utf-8"))
    return [[element["label"], element["score"]] for element in predictions]

app = gr.Interface(
    fn=predict, 
    inputs=gr.Image(type="filepath"), 
    outputs=gr.Dataframe(headers=["name", "amount per 100g (in kcal or g)"]), 
    allow_flagging="never",
    description=
      "Upload food images and get an estimation about their nutrition facts.\
      The model used is [ReverseNutrition_FCNutr](https://huggingface.co/davidaf3/ReverseNutrition_FCNutr).\
      If the output table shows an error, wait until the model is loaded."
  )

app.launch()