Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from dotenv import load_dotenv
|
|
5 |
import pandas as pd
|
6 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
7 |
import torch
|
|
|
8 |
|
9 |
# Carrega variáveis de ambiente
|
10 |
load_dotenv()
|
@@ -31,13 +32,22 @@ def identify_foods(image):
|
|
31 |
|
32 |
return processor.decode(outputs[0], skip_special_tokens=True)
|
33 |
|
34 |
-
def analyze_nutrition(image):
|
35 |
"""Análise nutricional da imagem"""
|
36 |
try:
|
37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
identified_foods = identify_foods(image)
|
|
|
|
|
39 |
|
40 |
-
#
|
|
|
41 |
nutrients = {
|
42 |
"calorias": 450,
|
43 |
"proteinas": 25,
|
@@ -61,7 +71,18 @@ def analyze_nutrition(image):
|
|
61 |
["Fibras", f"{nutrients['fibras']:.1f}g"]
|
62 |
]
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
analysis = []
|
66 |
if nutrients['calorias'] > 800:
|
67 |
analysis.append("⚠️ Alto valor calórico")
|
@@ -72,7 +93,8 @@ def analyze_nutrition(image):
|
|
72 |
|
73 |
analysis_text = "\n".join(analysis) if analysis else "✅ Valores nutricionais adequados"
|
74 |
|
75 |
-
|
|
|
76 |
recommendations = []
|
77 |
if "Alto valor calórico" in analysis_text:
|
78 |
recommendations.append("• Considere reduzir as porções")
|
@@ -83,32 +105,30 @@ def analyze_nutrition(image):
|
|
83 |
|
84 |
recommendations_text = "\n".join(recommendations) if recommendations else "• Continue mantendo uma alimentação equilibrada!"
|
85 |
|
86 |
-
|
87 |
-
|
|
|
|
|
88 |
table_data,
|
89 |
plot_data,
|
90 |
-
f"### Análise\n{analysis_text}",
|
91 |
-
f"### Recomendações\n{recommendations_text}",
|
92 |
-
"success"
|
93 |
)
|
94 |
|
95 |
except Exception as e:
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
100 |
)
|
101 |
|
102 |
css = """
|
103 |
-
.container {
|
104 |
-
|
105 |
-
margin: auto;
|
106 |
-
padding: 20px;
|
107 |
-
}
|
108 |
-
.header {
|
109 |
-
text-align: center;
|
110 |
-
margin-bottom: 30px;
|
111 |
-
}
|
112 |
.result-box {
|
113 |
border: 1px solid #ddd;
|
114 |
border-radius: 8px;
|
@@ -116,6 +136,24 @@ css = """
|
|
116 |
margin: 10px 0;
|
117 |
background-color: #fff;
|
118 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
"""
|
120 |
|
121 |
# Interface Gradio
|
@@ -153,9 +191,9 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as iface:
|
|
153 |
# Coluna da Direita - Resultados
|
154 |
with gr.Column(scale=3):
|
155 |
# Status
|
156 |
-
status = gr.Markdown(visible=False)
|
157 |
|
158 |
-
with gr.Tabs():
|
159 |
# Tab de Identificação
|
160 |
with gr.Tab("🔍 Identificação"):
|
161 |
with gr.Group(elem_classes="result-box"):
|
|
|
5 |
import pandas as pd
|
6 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
7 |
import torch
|
8 |
+
import time
|
9 |
|
10 |
# Carrega variáveis de ambiente
|
11 |
load_dotenv()
|
|
|
32 |
|
33 |
return processor.decode(outputs[0], skip_special_tokens=True)
|
34 |
|
35 |
+
def analyze_nutrition(image, progress=gr.Progress()):
|
36 |
"""Análise nutricional da imagem"""
|
37 |
try:
|
38 |
+
# Status inicial
|
39 |
+
progress(0, desc="Iniciando análise...")
|
40 |
+
yield "Iniciando análise...", None, None, None, None, "processing"
|
41 |
+
time.sleep(1) # Simula processamento
|
42 |
+
|
43 |
+
# Identificação dos alimentos
|
44 |
+
progress(0.2, desc="Identificando alimentos...")
|
45 |
identified_foods = identify_foods(image)
|
46 |
+
yield f"🔄 Identificando alimentos...\n\n{identified_foods}", None, None, None, None, "processing"
|
47 |
+
time.sleep(1)
|
48 |
|
49 |
+
# Análise nutricional
|
50 |
+
progress(0.4, desc="Calculando valores nutricionais...")
|
51 |
nutrients = {
|
52 |
"calorias": 450,
|
53 |
"proteinas": 25,
|
|
|
71 |
["Fibras", f"{nutrients['fibras']:.1f}g"]
|
72 |
]
|
73 |
|
74 |
+
progress(0.6, desc="Gerando análise...")
|
75 |
+
yield (
|
76 |
+
f"🔄 Gerando análise...\n\n{identified_foods}",
|
77 |
+
table_data,
|
78 |
+
plot_data,
|
79 |
+
"🔄 Analisando valores...",
|
80 |
+
"🔄 Preparando recomendações...",
|
81 |
+
"processing"
|
82 |
+
)
|
83 |
+
time.sleep(1)
|
84 |
+
|
85 |
+
# Análise
|
86 |
analysis = []
|
87 |
if nutrients['calorias'] > 800:
|
88 |
analysis.append("⚠️ Alto valor calórico")
|
|
|
93 |
|
94 |
analysis_text = "\n".join(analysis) if analysis else "✅ Valores nutricionais adequados"
|
95 |
|
96 |
+
progress(0.8, desc="Gerando recomendações...")
|
97 |
+
# Recomendações
|
98 |
recommendations = []
|
99 |
if "Alto valor calórico" in analysis_text:
|
100 |
recommendations.append("• Considere reduzir as porções")
|
|
|
105 |
|
106 |
recommendations_text = "\n".join(recommendations) if recommendations else "• Continue mantendo uma alimentação equilibrada!"
|
107 |
|
108 |
+
progress(1.0, desc="Concluído!")
|
109 |
+
# Resultado final
|
110 |
+
yield (
|
111 |
+
f"### 🍽️ Alimentos Identificados\n{identified_foods}",
|
112 |
table_data,
|
113 |
plot_data,
|
114 |
+
f"### 📊 Análise\n{analysis_text}",
|
115 |
+
f"### 💡 Recomendações\n{recommendations_text}",
|
116 |
+
"success"
|
117 |
)
|
118 |
|
119 |
except Exception as e:
|
120 |
+
yield (
|
121 |
+
"❌ Erro na identificação",
|
122 |
+
None,
|
123 |
+
None,
|
124 |
+
"❌ Erro na análise",
|
125 |
+
f"Erro: {str(e)}",
|
126 |
+
"error"
|
127 |
)
|
128 |
|
129 |
css = """
|
130 |
+
.container { max-width: 1200px; margin: auto; padding: 20px; }
|
131 |
+
.header { text-align: center; margin-bottom: 30px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
.result-box {
|
133 |
border: 1px solid #ddd;
|
134 |
border-radius: 8px;
|
|
|
136 |
margin: 10px 0;
|
137 |
background-color: #fff;
|
138 |
}
|
139 |
+
.processing {
|
140 |
+
display: flex;
|
141 |
+
justify-content: center;
|
142 |
+
align-items: center;
|
143 |
+
padding: 20px;
|
144 |
+
background: #f7f7f7;
|
145 |
+
border-radius: 8px;
|
146 |
+
margin: 10px 0;
|
147 |
+
}
|
148 |
+
.status-progress {
|
149 |
+
padding: 10px;
|
150 |
+
margin: 10px 0;
|
151 |
+
border-radius: 4px;
|
152 |
+
font-weight: bold;
|
153 |
+
}
|
154 |
+
.status-error { background: #fee; color: #c00; }
|
155 |
+
.status-success { background: #efe; color: #0c0; }
|
156 |
+
.status-processing { background: #eef; color: #00c; }
|
157 |
"""
|
158 |
|
159 |
# Interface Gradio
|
|
|
191 |
# Coluna da Direita - Resultados
|
192 |
with gr.Column(scale=3):
|
193 |
# Status
|
194 |
+
status = gr.Markdown(elem_classes="status-progress", visible=False)
|
195 |
|
196 |
+
with gr.Tabs() as tabs:
|
197 |
# Tab de Identificação
|
198 |
with gr.Tab("🔍 Identificação"):
|
199 |
with gr.Group(elem_classes="result-box"):
|