import gradio as gr from transformers import AutoProcessor, PaliGemmaForConditionalGeneration import torch from PIL import Image from huggingface_hub import login import os # Token’ı çevreden al HF_TOKEN = os.getenv("HF_TOKEN") if not HF_TOKEN: raise ValueError("HF_TOKEN bulunamadı. Lütfen Space ayarlarından token’ı ekleyin.") login(HF_TOKEN) # Hugging Face'e oturum aç # PaliGemma modelini ve işlemcisini yükle model_id = "google/paligemma-3b-mix-224" model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16) processor = AutoProcessor.from_pretrained(model_id) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) def predict(image, question): inputs = processor(images=image, text=question, return_tensors="pt").to(device) with torch.no_grad(): output = model.generate(**inputs, max_new_tokens=50) answer = processor.decode(output[0], skip_special_tokens=True)[len(question):].strip() return answer interface = gr.Interface( fn=predict, inputs=[ gr.Image(type="pil", label="Görüntüyü Yükle"), gr.Textbox(label="Sorunuzu Girin", placeholder="Bu görüntüde ne var?") ], outputs=gr.Textbox(label="Cevap"), title="PaliGemma ile Görsel Soru Yanıtlama", description="Bir görüntü yükleyin ve ona dair bir soru sorun. PaliGemma sizin için cevaplayacak!" ) interface.launch()