File size: 12,418 Bytes
5c0c96a
538b4a9
a91cbb3
 
f631343
aace76b
 
a91cbb3
 
 
15646ab
 
a7603f9
a91cbb3
 
aace76b
 
a91cbb3
 
 
15646ab
a91cbb3
 
15646ab
a91cbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15646ab
 
a91cbb3
 
 
15646ab
a91cbb3
 
15646ab
a91cbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15646ab
aace76b
2092c30
aace76b
 
 
a91cbb3
ec914f9
a91cbb3
 
aace76b
 
a91cbb3
aace76b
 
 
 
 
 
 
 
 
 
 
 
 
 
15646ab
aace76b
a91cbb3
aace76b
 
a91cbb3
 
 
 
 
 
aace76b
a91cbb3
 
 
 
 
 
 
 
 
 
 
aace76b
a91cbb3
aace76b
 
 
 
 
a91cbb3
aace76b
 
a91cbb3
 
2092c30
15646ab
a91cbb3
15646ab
a91cbb3
aace76b
2092c30
a91cbb3
 
 
2092c30
a91cbb3
15646ab
a91cbb3
 
 
 
 
 
 
 
 
 
15646ab
a91cbb3
 
 
 
aace76b
 
a91cbb3
15646ab
a91cbb3
 
15646ab
 
 
a91cbb3
 
15646ab
a91cbb3
2092c30
a91cbb3
15646ab
 
a91cbb3
 
15646ab
 
 
a91cbb3
15646ab
 
 
a91cbb3
 
15646ab
 
 
a91cbb3
aace76b
15646ab
 
2092c30
 
a91cbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30e029e
aace76b
a91cbb3
aace76b
 
 
 
 
 
 
 
a91cbb3
 
aace76b
 
 
 
 
 
 
 
 
 
 
 
97ab7f8
aace76b
a91cbb3
aace76b
0f757cc
aace76b
 
 
a91cbb3
aace76b
 
 
 
 
a91cbb3
aace76b
 
 
2092c30
aace76b
 
 
 
 
2092c30
aace76b
 
 
a91cbb3
aace76b
2092c30
aace76b
 
a91cbb3
aace76b
2092c30
aace76b
 
0f757cc
a91cbb3
aace76b
a91cbb3
 
 
 
 
 
0f757cc
a91cbb3
 
 
aace76b
a91cbb3
aace76b
 
 
 
 
 
 
0f757cc
aace76b
5c0c96a
 
aace76b
a91cbb3
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import gradio as gr
import torch
from transformers import AutoProcessor, AutoModelForVision2Seq
from PIL import Image
import numpy as np
import os
import logging
import cv2
import shutil
import subprocess
from pathlib import Path
import tempfile

# Configuração de logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

class ForceImageProcessor:
    """Processador agressivo de imagens com múltiplos fallbacks"""
    
    @staticmethod
    def force_convert_image(input_path):
        """Converte imagem usando múltiplos métodos até funcionar"""
        try:
            # Cria diretório temporário
            with tempfile.TemporaryDirectory() as temp_dir:
                temp_path = Path(temp_dir) / "converted_image.jpg"
                
                # Tenta diferentes métodos de conversão
                methods = [
                    ForceImageProcessor._try_pillow,
                    ForceImageProcessor._try_opencv,
                    ForceImageProcessor._try_imagemagick,
                    ForceImageProcessor._try_ffmpeg
                ]
                
                for method in methods:
                    try:
                        result = method(input_path, temp_path)
                        if result:
                            return Image.open(temp_path)
                    except Exception as e:
                        logger.debug(f"Método falhou: {str(e)}")
                        continue
                
                raise ValueError("Todos os métodos de conversão falharam")
                
        except Exception as e:
            logger.error(f"Erro na conversão: {str(e)}")
            raise
    
    @staticmethod
    def _try_pillow(input_path, output_path):
        """Tenta converter usando Pillow"""
        try:
            img = Image.open(input_path)
            img = img.convert('RGB')
            img.save(output_path, 'JPEG')
            return True
        except:
            return False
    
    @staticmethod
    def _try_opencv(input_path, output_path):
        """Tenta converter usando OpenCV"""
        try:
            img = cv2.imread(str(input_path))
            if img is None:
                return False
            cv2.imwrite(str(output_path), img)
            return True
        except:
            return False
    
    @staticmethod
    def _try_imagemagick(input_path, output_path):
        """Tenta converter usando ImageMagick"""
        try:
            result = subprocess.run(
                ['convert', str(input_path), str(output_path)],
                capture_output=True,
                text=True
            )
            return result.returncode == 0
        except:
            return False
    
    @staticmethod
    def _try_ffmpeg(input_path, output_path):
        """Tenta converter usando FFmpeg"""
        try:
            result = subprocess.run(
                ['ffmpeg', '-i', str(input_path), '-y', str(output_path)],
                capture_output=True,
                text=True
            )
            return result.returncode == 0
        except:
            return False

class NutritionalAnalyzer:
    def __init__(self):
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.models = {}
        self.processors = {}
        self.image_processor = ForceImageProcessor()
        
    async def initialize_model(self, model_name):
        """Inicializa modelo com tratamento de erros melhorado"""
        try:
            if model_name not in self.models:
                logger.info(f"Inicializando {model_name}...")
                
                model_configs = {
                    "llava": {
                        "repo": "llava-hf/llava-1.5-7b-hf",
                        "local_cache": "models/llava"
                    },
                    "git": {
                        "repo": "microsoft/git-base-coco",
                        "local_cache": "models/git"
                    }
                }
                
                config = model_configs.get(model_name)
                if not config:
                    raise ValueError(f"Modelo não suportado: {model_name}")
                
                # Garante que o diretório de cache existe
                os.makedirs(config["local_cache"], exist_ok=True)
                
                # Carrega processador e modelo
                try:
                    self.processors[model_name] = await gr.asyncio.asyncio.to_thread(
                        AutoProcessor.from_pretrained,
                        config["repo"],
                        trust_remote_code=True
                    )
                    
                    self.models[model_name] = await gr.asyncio.asyncio.to_thread(
                        AutoModelForVision2Seq.from_pretrained,
                        config["repo"],
                        torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
                        device_map="auto",
                        trust_remote_code=True
                    )
                except Exception as e:
                    logger.error(f"Erro ao carregar modelo: {str(e)}")
                    raise
                
                logger.info(f"{model_name} inicializado com sucesso")
                return True
            
            return True
            
        except Exception as e:
            logger.error(f"Erro na inicialização do {model_name}: {str(e)}")
            return False

    async def analyze_image(self, image, question, model_choice):
        """Analisa imagem com foco nutricional"""
        try:
            if image is None:
                return "Por favor, envie uma imagem para análise."

            # Converte escolha do modelo
            model_name = model_choice.lower().replace("-", "")
            
            # Inicializa modelo
            if not await self.initialize_model(model_name):
                return "Erro: Falha ao inicializar o modelo. Tente novamente."
            
            # Processa imagem com conversão forçada
            try:
                with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
                    if isinstance(image, np.ndarray):
                        cv2.imwrite(temp_file.name, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
                    else:
                        shutil.copy2(image, temp_file.name)
                    
                    processed_image = await gr.asyncio.asyncio.to_thread(
                        self.image_processor.force_convert_image,
                        temp_file.name
                    )
            except Exception as e:
                logger.error(f"Erro no processamento da imagem: {str(e)}")
                return f"Erro ao processar imagem: {str(e)}"
            
            # Gera prompt
            nutritional_prompt = self.generate_nutritional_prompt(question)
            
            # Processa input
            try:
                inputs = await gr.asyncio.asyncio.to_thread(
                    self.processors[model_name],
                    images=processed_image,
                    text=nutritional_prompt,
                    return_tensors="pt"
                )
                inputs = {k: v.to(self.device) for k, v in inputs.items()}
            except Exception as e:
                return f"Erro no processamento: {str(e)}"
            
            # Gera resposta
            try:
                with torch.no_grad():
                    outputs = await gr.asyncio.asyncio.to_thread(
                        self.models[model_name].generate,
                        **inputs,
                        max_new_tokens=300,
                        num_beams=5,
                        do_sample=True,  # Habilitado para usar temperature e top_p
                        temperature=0.7,
                        top_p=0.9,
                        repetition_penalty=1.2,
                        no_repeat_ngram_size=3,  # Evita repetições de frases
                        early_stopping=True  # Para quando a geração estiver completa
                    )
                
                response = self.processors[model_name].decode(outputs[0], skip_special_tokens=True)
                return self.format_response(response)
            
            except Exception as e:
                return f"Erro na geração da análise: {str(e)}"
            
        except Exception as e:
            logger.error(f"Erro na análise: {str(e)}")
            return f"Erro: {str(e)}\nPor favor, tente novamente."
        
    def generate_nutritional_prompt(self, question):
        """Gera prompt para análise nutricional"""
        return f"""Como nutricionista especializado, analise esta refeição detalhadamente:

1. Composição do Prato:
   - Ingredientes principais
   - Proporções aproximadas
   - Método de preparo aparente

2. Análise Nutricional:
   - Estimativa calórica
   - Macronutrientes (proteínas, carboidratos, gorduras)
   - Principais micronutrientes

3. Recomendações:
   - Sugestões para versão mais saudável
   - Porção recomendada
   - Adequação para dietas específicas

Pergunta específica do usuário: {question}

Por favor, forneça uma análise detalhada em português."""

    def format_response(self, response):
        """Formata a resposta para melhor legibilidade"""
        sections = [
            "Composição do Prato",
            "Análise Nutricional",
            "Recomendações"
        ]
        
        formatted = "# 📊 Análise Nutricional\n\n"
        current_section = ""
        
        for paragraph in response.split("\n"):
            for section in sections:
                if section.lower() in paragraph.lower():
                    current_section = f"\n## {section}\n"
                    formatted += current_section
                    break
            
            if paragraph.strip() and current_section:
                formatted += f"- {paragraph.strip()}\n"
            elif paragraph.strip():
                formatted += f"{paragraph.strip()}\n"
        
        return formatted

def create_interface():
    """Cria interface Gradio"""
    analyzer = NutritionalAnalyzer()
    
    with gr.Blocks(theme=gr.themes.Soft()) as iface:
        gr.Markdown("""
        # 🥗 Análise Nutricional Inteligente
        Upload da foto do seu prato para análise nutricional detalhada.
        """)
        
        with gr.Row():
            with gr.Column(scale=2):
                image_input = gr.Image(
                    type="filepath",  # Mudado para filepath para melhor compatibilidade
                    label="📸 Foto do Prato",
                    height=400
                )
                
                question_input = gr.Textbox(
                    label="💭 Sua Pergunta",
                    placeholder="Ex: Quais são os nutrientes principais deste prato?",
                    lines=2
                )
                
                model_choice = gr.Radio(
                    choices=["LLaVA", "GIT"],
                    value="LLaVA",
                    label="🤖 Escolha o Modelo"
                )
                
                analyze_btn = gr.Button(
                    "🔍 Analisar Prato",
                    variant="primary"
                )
            
            with gr.Column(scale=3):
                output = gr.Markdown(label="Resultado da Análise")
        
        with gr.Accordion("💡 Dicas", open=False):
            gr.Markdown("""
            ### Formatos Suportados:
            - JPG/JPEG
            - PNG
            - WEBP
            - AVIF
            - Outros formatos de imagem comuns
            
            ### Para Melhores Resultados:
            1. Boa iluminação na foto
            2. Capture todo o prato
            3. Evite ângulos muito inclinados
            4. Perguntas específicas ajudam
            """)
        
        analyze_btn.click(
            fn=analyzer.analyze_image,
            inputs=[image_input, question_input, model_choice],
            outputs=output
        )
    
    return iface

if __name__ == "__main__":
    iface = create_interface()
    iface.launch(
        share=False,
        debug=True,
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True
    )