Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import ( | |
AutoProcessor, | |
AutoModelForVision2Seq, | |
BlipProcessor, | |
BlipForQuestionAnswering, | |
OFATokenizer, | |
OFAModel | |
) | |
from PIL import Image | |
import torch | |
import warnings | |
warnings.filterwarnings("ignore") | |
class IridologyAnalyzer: | |
def __init__(self): | |
# Inicializar modelos | |
print("Carregando modelos...") | |
# GIT model | |
self.git_processor = AutoProcessor.from_pretrained("microsoft/git-base-vqa") | |
self.git_model = AutoModelForVision2Seq.from_pretrained("microsoft/git-base-vqa") | |
# BLIP model | |
self.blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base") | |
self.blip_model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base") | |
# OFA model | |
self.ofa_tokenizer = OFATokenizer.from_pretrained("OFA-Sys/ofa-base") | |
self.ofa_model = OFAModel.from_pretrained("OFA-Sys/ofa-base") | |
# Perguntas predefinidas para análise de iridologia | |
self.iridology_questions = [ | |
"What is the color pattern of the iris?", | |
"Are there any dark spots or marks in the iris?", | |
"What is the texture of the iris?", | |
"Are there any rings or circles in the iris?", | |
"What is the condition of the pupil?", | |
"Are there any lines radiating from the pupil?", | |
"Is there any discoloration in specific areas?", | |
"What is the overall clarity of the iris?" | |
] | |
print("Modelos carregados com sucesso!") | |
def analyze_with_git(self, image, question): | |
inputs = self.git_processor(images=image, text=question, return_tensors="pt") | |
outputs = self.git_model.generate( | |
**inputs, | |
max_length=50, | |
num_beams=4, | |
early_stopping=True | |
) | |
return self.git_processor.batch_decode(outputs, skip_special_tokens=True)[0] | |
def analyze_with_blip(self, image, question): | |
inputs = self.blip_processor(image, question, return_tensors="pt") | |
outputs = self.blip_model.generate(**inputs) | |
return self.blip_processor.decode(outputs[0], skip_special_tokens=True) | |
def analyze_with_ofa(self, image, question): | |
inputs = self.ofa_tokenizer([question], return_tensors="pt") | |
img = self.ofa_tokenizer(images=image, return_tensors="pt").pixel_values | |
outputs = self.ofa_model.generate( | |
input_ids=inputs.input_ids, | |
pixel_values=img, | |
max_length=50, | |
num_beams=4 | |
) | |
return self.ofa_tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] | |
def comprehensive_analysis(self, image): | |
"""Realiza uma análise completa usando todos os modelos e questões predefinidas.""" | |
results = [] | |
for question in self.iridology_questions: | |
# Análise com cada modelo | |
git_result = self.analyze_with_git(image, question) | |
blip_result = self.analyze_with_blip(image, question) | |
ofa_result = self.analyze_with_ofa(image, question) | |
results.append({ | |
"question": question, | |
"git_analysis": git_result, | |
"blip_analysis": blip_result, | |
"ofa_analysis": ofa_result | |
}) | |
# Formatar resultados | |
formatted_results = "Análise Detalhada de Iridologia:\n\n" | |
for result in results: | |
formatted_results += f"Pergunta: {result['question']}\n" | |
formatted_results += f"GIT: {result['git_analysis']}\n" | |
formatted_results += f"BLIP: {result['blip_analysis']}\n" | |
formatted_results += f"OFA: {result['ofa_analysis']}\n" | |
formatted_results += "-" * 50 + "\n" | |
return formatted_results | |
def create_gradio_interface(): | |
analyzer = IridologyAnalyzer() | |
def process_image(image): | |
if image is None: | |
return "Por favor, faça o upload de uma imagem." | |
# Converter para PIL Image se necessário | |
if not isinstance(image, Image.Image): | |
image = Image.fromarray(image) | |
# Realizar análise | |
try: | |
return analyzer.comprehensive_analysis(image) | |
except Exception as e: | |
return f"Erro durante a análise: {str(e)}" | |
# Interface Gradio | |
iface = gr.Interface( | |
fn=process_image, | |
inputs=gr.Image(type="pil", label="Upload da Imagem do Olho"), | |
outputs=gr.Textbox(label="Resultados da Análise", lines=20), | |
title="Analisador de Iridologia com IA", | |
description=""" | |
Este sistema analisa imagens de íris usando múltiplos modelos de IA para identificar padrões relevantes para iridologia. | |
Faça o upload de uma imagem clara do olho para análise. | |
""", | |
examples=[], | |
cache_examples=True | |
) | |
return iface | |
if __name__ == "__main__": | |
iface = create_gradio_interface() | |
iface.launch(share=True) |