Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import (
|
3 |
+
AutoProcessor,
|
4 |
+
AutoModelForVision2Seq,
|
5 |
+
BlipProcessor,
|
6 |
+
BlipForQuestionAnswering,
|
7 |
+
OFATokenizer,
|
8 |
+
OFAModel
|
9 |
+
)
|
10 |
+
from PIL import Image
|
11 |
+
import torch
|
12 |
+
import warnings
|
13 |
+
warnings.filterwarnings("ignore")
|
14 |
+
|
15 |
+
class IridologyAnalyzer:
|
16 |
+
def __init__(self):
|
17 |
+
# Inicializar modelos
|
18 |
+
print("Carregando modelos...")
|
19 |
+
|
20 |
+
# GIT model
|
21 |
+
self.git_processor = AutoProcessor.from_pretrained("microsoft/git-base-vqa")
|
22 |
+
self.git_model = AutoModelForVision2Seq.from_pretrained("microsoft/git-base-vqa")
|
23 |
+
|
24 |
+
# BLIP model
|
25 |
+
self.blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
26 |
+
self.blip_model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
27 |
+
|
28 |
+
# OFA model
|
29 |
+
self.ofa_tokenizer = OFATokenizer.from_pretrained("OFA-Sys/ofa-base")
|
30 |
+
self.ofa_model = OFAModel.from_pretrained("OFA-Sys/ofa-base")
|
31 |
+
|
32 |
+
# Perguntas predefinidas para análise de iridologia
|
33 |
+
self.iridology_questions = [
|
34 |
+
"What is the color pattern of the iris?",
|
35 |
+
"Are there any dark spots or marks in the iris?",
|
36 |
+
"What is the texture of the iris?",
|
37 |
+
"Are there any rings or circles in the iris?",
|
38 |
+
"What is the condition of the pupil?",
|
39 |
+
"Are there any lines radiating from the pupil?",
|
40 |
+
"Is there any discoloration in specific areas?",
|
41 |
+
"What is the overall clarity of the iris?"
|
42 |
+
]
|
43 |
+
|
44 |
+
print("Modelos carregados com sucesso!")
|
45 |
+
|
46 |
+
def analyze_with_git(self, image, question):
|
47 |
+
inputs = self.git_processor(images=image, text=question, return_tensors="pt")
|
48 |
+
outputs = self.git_model.generate(
|
49 |
+
**inputs,
|
50 |
+
max_length=50,
|
51 |
+
num_beams=4,
|
52 |
+
early_stopping=True
|
53 |
+
)
|
54 |
+
return self.git_processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
55 |
+
|
56 |
+
def analyze_with_blip(self, image, question):
|
57 |
+
inputs = self.blip_processor(image, question, return_tensors="pt")
|
58 |
+
outputs = self.blip_model.generate(**inputs)
|
59 |
+
return self.blip_processor.decode(outputs[0], skip_special_tokens=True)
|
60 |
+
|
61 |
+
def analyze_with_ofa(self, image, question):
|
62 |
+
inputs = self.ofa_tokenizer([question], return_tensors="pt")
|
63 |
+
img = self.ofa_tokenizer(images=image, return_tensors="pt").pixel_values
|
64 |
+
|
65 |
+
outputs = self.ofa_model.generate(
|
66 |
+
input_ids=inputs.input_ids,
|
67 |
+
pixel_values=img,
|
68 |
+
max_length=50,
|
69 |
+
num_beams=4
|
70 |
+
)
|
71 |
+
return self.ofa_tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
72 |
+
|
73 |
+
def comprehensive_analysis(self, image):
|
74 |
+
"""Realiza uma análise completa usando todos os modelos e questões predefinidas."""
|
75 |
+
results = []
|
76 |
+
|
77 |
+
for question in self.iridology_questions:
|
78 |
+
# Análise com cada modelo
|
79 |
+
git_result = self.analyze_with_git(image, question)
|
80 |
+
blip_result = self.analyze_with_blip(image, question)
|
81 |
+
ofa_result = self.analyze_with_ofa(image, question)
|
82 |
+
|
83 |
+
results.append({
|
84 |
+
"question": question,
|
85 |
+
"git_analysis": git_result,
|
86 |
+
"blip_analysis": blip_result,
|
87 |
+
"ofa_analysis": ofa_result
|
88 |
+
})
|
89 |
+
|
90 |
+
# Formatar resultados
|
91 |
+
formatted_results = "Análise Detalhada de Iridologia:\n\n"
|
92 |
+
for result in results:
|
93 |
+
formatted_results += f"Pergunta: {result['question']}\n"
|
94 |
+
formatted_results += f"GIT: {result['git_analysis']}\n"
|
95 |
+
formatted_results += f"BLIP: {result['blip_analysis']}\n"
|
96 |
+
formatted_results += f"OFA: {result['ofa_analysis']}\n"
|
97 |
+
formatted_results += "-" * 50 + "\n"
|
98 |
+
|
99 |
+
return formatted_results
|
100 |
+
|
101 |
+
def create_gradio_interface():
|
102 |
+
analyzer = IridologyAnalyzer()
|
103 |
+
|
104 |
+
def process_image(image):
|
105 |
+
if image is None:
|
106 |
+
return "Por favor, faça o upload de uma imagem."
|
107 |
+
|
108 |
+
# Converter para PIL Image se necessário
|
109 |
+
if not isinstance(image, Image.Image):
|
110 |
+
image = Image.fromarray(image)
|
111 |
+
|
112 |
+
# Realizar análise
|
113 |
+
try:
|
114 |
+
return analyzer.comprehensive_analysis(image)
|
115 |
+
except Exception as e:
|
116 |
+
return f"Erro durante a análise: {str(e)}"
|
117 |
+
|
118 |
+
# Interface Gradio
|
119 |
+
iface = gr.Interface(
|
120 |
+
fn=process_image,
|
121 |
+
inputs=gr.Image(type="pil", label="Upload da Imagem do Olho"),
|
122 |
+
outputs=gr.Textbox(label="Resultados da Análise", lines=20),
|
123 |
+
title="Analisador de Iridologia com IA",
|
124 |
+
description="""
|
125 |
+
Este sistema analisa imagens de íris usando múltiplos modelos de IA para identificar padrões relevantes para iridologia.
|
126 |
+
Faça o upload de uma imagem clara do olho para análise.
|
127 |
+
""",
|
128 |
+
examples=[],
|
129 |
+
cache_examples=True
|
130 |
+
)
|
131 |
+
|
132 |
+
return iface
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
iface = create_gradio_interface()
|
136 |
+
iface.launch(share=True)
|