Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import json
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
# Carregar o modelo e processador DeepSeek
|
9 |
+
model = AutoModelForVision2Seq.from_pretrained("deepseek-ai/deepseek-vl-7b-base")
|
10 |
+
processor = AutoProcessor.from_pretrained("deepseek-ai/deepseek-vl-7b-base")
|
11 |
+
|
12 |
+
# Base de dados nutricional (exemplo simplificado)
|
13 |
+
NUTRITIONAL_DB = {
|
14 |
+
"arroz": {
|
15 |
+
"calorias": 130,
|
16 |
+
"proteinas": 2.7,
|
17 |
+
"carboidratos": 28,
|
18 |
+
"gorduras": 0.3,
|
19 |
+
"fibras": 0.4,
|
20 |
+
},
|
21 |
+
"feijão": {
|
22 |
+
"calorias": 77,
|
23 |
+
"proteinas": 5.2,
|
24 |
+
"carboidratos": 13.6,
|
25 |
+
"gorduras": 0.5,
|
26 |
+
"fibras": 5.4,
|
27 |
+
},
|
28 |
+
# Adicione mais alimentos conforme necessário
|
29 |
+
}
|
30 |
+
|
31 |
+
def analyze_food(image):
|
32 |
+
"""
|
33 |
+
Analisa a imagem do alimento e retorna um relatório nutricional
|
34 |
+
"""
|
35 |
+
# Processar a imagem
|
36 |
+
inputs = processor(images=image, text="What foods are in this image? List them.", return_tensors="pt")
|
37 |
+
|
38 |
+
# Gerar descrição
|
39 |
+
with torch.no_grad():
|
40 |
+
outputs = model.generate(**inputs, max_length=100)
|
41 |
+
foods = processor.decode(outputs[0], skip_special_tokens=True)
|
42 |
+
|
43 |
+
# Processar lista de alimentos identificados
|
44 |
+
food_list = [food.strip().lower() for food in foods.split(',')]
|
45 |
+
|
46 |
+
# Gerar relatório nutricional
|
47 |
+
total_nutrients = {
|
48 |
+
"calorias": 0,
|
49 |
+
"proteinas": 0,
|
50 |
+
"carboidratos": 0,
|
51 |
+
"gorduras": 0,
|
52 |
+
"fibras": 0,
|
53 |
+
}
|
54 |
+
|
55 |
+
report = "🍽️ Relatório Nutricional\n\n"
|
56 |
+
report += "Alimentos identificados:\n"
|
57 |
+
|
58 |
+
for food in food_list:
|
59 |
+
if food in NUTRITIONAL_DB:
|
60 |
+
report += f"- {food.capitalize()}\n"
|
61 |
+
for nutrient, value in NUTRITIONAL_DB[food].items():
|
62 |
+
total_nutrients[nutrient] += value
|
63 |
+
|
64 |
+
report += "\n📊 Informação Nutricional Total:\n"
|
65 |
+
report += f"Calorias: {total_nutrients['calorias']:.1f} kcal\n"
|
66 |
+
report += f"Proteínas: {total_nutrients['proteinas']:.1f}g\n"
|
67 |
+
report += f"Carboidratos: {total_nutrients['carboidratos']:.1f}g\n"
|
68 |
+
report += f"Gorduras: {total_nutrients['gorduras']:.1f}g\n"
|
69 |
+
report += f"Fibras: {total_nutrients['fibras']:.1f}g\n"
|
70 |
+
|
71 |
+
report += "\n💡 Recomendações:\n"
|
72 |
+
# Adicionar recomendações básicas baseadas nos valores nutricionais
|
73 |
+
if total_nutrients['calorias'] > 800:
|
74 |
+
report += "- Esta refeição tem um alto valor calórico. Considere porções menores.\n"
|
75 |
+
if total_nutrients['proteinas'] < 15:
|
76 |
+
report += "- Considere adicionar mais fontes de proteína à refeição.\n"
|
77 |
+
if total_nutrients['fibras'] < 6:
|
78 |
+
report += "- Adicione mais vegetais para aumentar o teor de fibras.\n"
|
79 |
+
|
80 |
+
return report
|
81 |
+
|
82 |
+
# Interface Gradio
|
83 |
+
iface = gr.Interface(
|
84 |
+
fn=analyze_food,
|
85 |
+
inputs=gr.Image(type="pil"),
|
86 |
+
outputs=gr.Textbox(label="Relatório Nutricional", lines=10),
|
87 |
+
title="Análise Nutricional de Alimentos",
|
88 |
+
description="Faça upload de uma foto do seu prato para receber uma análise nutricional detalhada.",
|
89 |
+
examples=[["example_food1.jpg"], ["example_food2.jpg"]],
|
90 |
+
theme=gr.themes.Soft()
|
91 |
+
)
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
iface.launch(share=True)
|