-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
83 lines (69 loc) · 2.31 KB
/
app.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import warnings
import gradio as gr
import lightning as L
from lightning.app.components.serve import ServeGradio
from PIL import Image
from src.llava_wrapper import LLaVA
from src import prompt_handler
warnings.filterwarnings("ignore")
class LitGradio(ServeGradio):
inputs = [
gr.Image(type="filepath", label='Image'),
gr.Textbox(label="Hints", placeholder="Menu name, brocolli 3 pieces, steak 150g..."),
]
outputs = [
gr.outputs.Textbox(label="Menu Name"),
gr.outputs.Textbox(label="Description"),
gr.outputs.JSON(label="Nutrients"),
]
examples = [
[
os.path.join(os.path.dirname(__file__), "assets", "fruits.jpg"),
None,
],
[
os.path.join(os.path.dirname(__file__), "assets", "pasta.jpeg"),
None,
],
[
os.path.join(os.path.dirname(__file__), "assets", "pizza.jpeg"),
None,
],
[
os.path.join(os.path.dirname(__file__), "assets", "salad.jpeg"),
None,
],
[
os.path.join(os.path.dirname(__file__), "assets", "waffle.jpeg"),
None,
],
]
def __init__(self):
super().__init__()
self.ready = False
def predict(self, image_path, hints):
print("Start prediction")
image_pil = Image.open(image_path).convert("RGB")
output = self.model.generate(image_pil, hints)
dict_out = prompt_handler.parse_dictionary_string(output)
isfood = dict_out.get('imageofFood', 'False')
if not isfood:
return 'Not food', 'Not food', {}
menu_name = dict_out.get('menuName', 'Not found')
description = dict_out.get('description', 'Not found')
nutrients = dict_out.get('nutrients', 'Not found')
nutrients_dict = {}
if type(nutrients) is list:
for nutrient in nutrients:
name = nutrient['nutrientName']
value = nutrient['nutritionalValue']
unit = nutrient['unit']
nutrients_dict[name] = f"{value} {unit}"
print('Done')
return menu_name, description, nutrients_dict
def build_model(self):
model = LLaVA()
self.ready = True
return model
app = L.LightningApp(LitGradio())