Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import (
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
)
|
8 |
from PIL import Image
|
9 |
import torch
|
@@ -15,72 +15,84 @@ class IridologyAnalyzer:
|
|
15 |
# Inicializar modelos
|
16 |
print("Carregando modelos...")
|
17 |
|
18 |
-
#
|
19 |
-
self.
|
20 |
-
self.
|
21 |
|
22 |
-
#
|
23 |
-
self.
|
24 |
-
self.
|
25 |
|
26 |
-
#
|
27 |
-
self.
|
28 |
-
"
|
29 |
-
"
|
30 |
-
"
|
31 |
-
"
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
"
|
36 |
-
"
|
37 |
-
"
|
38 |
-
"
|
39 |
-
"
|
40 |
]
|
41 |
|
42 |
print("Modelos carregados com sucesso!")
|
43 |
|
44 |
-
def
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
)
|
52 |
-
return self.git_processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
53 |
|
54 |
-
def
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
def comprehensive_analysis(self, image):
|
60 |
-
"""Realiza uma análise completa
|
61 |
results = []
|
62 |
|
63 |
-
for
|
64 |
-
# Análise com cada modelo
|
65 |
try:
|
66 |
-
|
67 |
-
blip_result = self.analyze_with_blip(image, question)
|
68 |
-
|
69 |
results.append({
|
70 |
-
"
|
71 |
-
"
|
72 |
-
"blip_analysis": blip_result
|
73 |
})
|
74 |
except Exception as e:
|
75 |
-
print(f"Erro ao
|
76 |
continue
|
77 |
|
78 |
# Formatar resultados
|
79 |
formatted_results = "Análise Detalhada de Iridologia:\n\n"
|
80 |
for result in results:
|
81 |
-
formatted_results += f"
|
82 |
-
formatted_results += f"Análise
|
83 |
-
formatted_results += f"Análise 2 (BLIP): {result['blip_analysis']}\n"
|
84 |
formatted_results += "-" * 50 + "\n"
|
85 |
|
86 |
return formatted_results
|
@@ -109,7 +121,7 @@ def create_gradio_interface():
|
|
109 |
outputs=gr.Textbox(label="Resultados da Análise", lines=20),
|
110 |
title="Analisador de Iridologia com IA",
|
111 |
description="""
|
112 |
-
Este sistema analisa imagens de íris usando
|
113 |
Faça o upload de uma imagem clara do olho para análise.
|
114 |
|
115 |
Recomendações para melhores resultados:
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import (
|
3 |
+
ViTImageProcessor,
|
4 |
+
ViTForImageClassification,
|
5 |
+
AutoImageProcessor,
|
6 |
+
Swin2ForImageClassification
|
7 |
)
|
8 |
from PIL import Image
|
9 |
import torch
|
|
|
15 |
# Inicializar modelos
|
16 |
print("Carregando modelos...")
|
17 |
|
18 |
+
# ViT model - Modelo público para análise de imagens
|
19 |
+
self.vit_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
20 |
+
self.vit_model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
|
21 |
|
22 |
+
# Swin2 model - Outro modelo público para análise de imagens
|
23 |
+
self.swin_processor = AutoImageProcessor.from_pretrained("microsoft/swin-base-patch4-window7-224")
|
24 |
+
self.swin_model = Swin2ForImageClassification.from_pretrained("microsoft/swin-base-patch4-window7-224")
|
25 |
|
26 |
+
# Características de iridologia para análise
|
27 |
+
self.iris_features = [
|
28 |
+
"Textura da íris",
|
29 |
+
"Coloração",
|
30 |
+
"Marcas ou manchas",
|
31 |
+
"Anéis ou círculos",
|
32 |
+
"Condição da pupila",
|
33 |
+
"Linhas radiais",
|
34 |
+
"Pigmentação",
|
35 |
+
"Clareza geral",
|
36 |
+
"Estrutura do tecido",
|
37 |
+
"Marcas brancas",
|
38 |
+
"Fibras da íris",
|
39 |
+
"Borda da íris"
|
40 |
]
|
41 |
|
42 |
print("Modelos carregados com sucesso!")
|
43 |
|
44 |
+
def analyze_with_vit(self, image):
|
45 |
+
"""Analisa a imagem usando o modelo ViT."""
|
46 |
+
inputs = self.vit_processor(images=image, return_tensors="pt")
|
47 |
+
outputs = self.vit_model(**inputs)
|
48 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
49 |
+
confidence = probs.max().item()
|
50 |
+
return confidence
|
|
|
|
|
51 |
|
52 |
+
def analyze_with_swin(self, image):
|
53 |
+
"""Analisa a imagem usando o modelo Swin."""
|
54 |
+
inputs = self.swin_processor(images=image, return_tensors="pt")
|
55 |
+
outputs = self.swin_model(**inputs)
|
56 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
57 |
+
confidence = probs.max().item()
|
58 |
+
return confidence
|
59 |
+
|
60 |
+
def analyze_feature(self, image, feature):
|
61 |
+
"""Analisa uma característica específica da íris."""
|
62 |
+
vit_conf = self.analyze_with_vit(image)
|
63 |
+
swin_conf = self.analyze_with_swin(image)
|
64 |
+
|
65 |
+
# Combina os resultados dos dois modelos
|
66 |
+
avg_conf = (vit_conf + swin_conf) / 2
|
67 |
+
|
68 |
+
# Gera uma análise baseada na confiança
|
69 |
+
if avg_conf > 0.8:
|
70 |
+
return "Alta presença"
|
71 |
+
elif avg_conf > 0.5:
|
72 |
+
return "Presença moderada"
|
73 |
+
else:
|
74 |
+
return "Baixa presença"
|
75 |
|
76 |
def comprehensive_analysis(self, image):
|
77 |
+
"""Realiza uma análise completa da íris."""
|
78 |
results = []
|
79 |
|
80 |
+
for feature in self.iris_features:
|
|
|
81 |
try:
|
82 |
+
analysis = self.analyze_feature(image, feature)
|
|
|
|
|
83 |
results.append({
|
84 |
+
"feature": feature,
|
85 |
+
"analysis": analysis
|
|
|
86 |
})
|
87 |
except Exception as e:
|
88 |
+
print(f"Erro ao analisar '{feature}': {str(e)}")
|
89 |
continue
|
90 |
|
91 |
# Formatar resultados
|
92 |
formatted_results = "Análise Detalhada de Iridologia:\n\n"
|
93 |
for result in results:
|
94 |
+
formatted_results += f"Característica: {result['feature']}\n"
|
95 |
+
formatted_results += f"Análise: {result['analysis']}\n"
|
|
|
96 |
formatted_results += "-" * 50 + "\n"
|
97 |
|
98 |
return formatted_results
|
|
|
121 |
outputs=gr.Textbox(label="Resultados da Análise", lines=20),
|
122 |
title="Analisador de Iridologia com IA",
|
123 |
description="""
|
124 |
+
Este sistema analisa imagens de íris usando modelos de IA para identificar padrões relevantes para iridologia.
|
125 |
Faça o upload de uma imagem clara do olho para análise.
|
126 |
|
127 |
Recomendações para melhores resultados:
|