Spaces:
Build error
Build error
Added app file
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
|
6 |
+
API_URL = "https://api-inference.huggingface.co/models/davidaf3/ReverseNutrition_TFIngPort"
|
7 |
+
headers = {"Authorization": f"Bearer {os.environ['API_TOKEN']}"}
|
8 |
+
|
9 |
+
def predict(image_file):
|
10 |
+
with open(image_file, "rb") as f:
|
11 |
+
data = f.read()
|
12 |
+
response = requests.request("POST", API_URL, headers=headers, data=data)
|
13 |
+
predictions = json.loads(response.content.decode("utf-8"))
|
14 |
+
return [[element["label"], element["score"]] for element in predictions if element["score"] > 0]
|
15 |
+
|
16 |
+
app = gr.Interface(
|
17 |
+
fn=predict,
|
18 |
+
inputs=gr.Image(type="filepath"),
|
19 |
+
outputs=gr.Dataframe(headers=["ingredient", "amount per 100g (in g)"]),
|
20 |
+
allow_flagging="never",
|
21 |
+
description=
|
22 |
+
"Upload food images and get an estimation about their ingredients and the ingredient proportions.\
|
23 |
+
The model used is [ReverseNutrition_TFIngPort](https://huggingface.co/davidaf3/ReverseNutrition_TFIngPort).\
|
24 |
+
If the output table shows an error, wait until the model is loaded."
|
25 |
+
)
|
26 |
+
|
27 |
+
app.launch()
|