Spaces:
Sleeping
Sleeping
File size: 1,517 Bytes
5ea1eb0 3351272 5ea1eb0 9ba60c5 5ea1eb0 |
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 |
import os
from transformers import AutoModelForImageClassification, AutoImageProcessor
import gradio as gr
import torch
# Hugging Face tokeninizi çevresel değişkenden alın
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise ValueError("HF_TOKEN çevresel değişkeni ayarlanmamış. Lütfen Hugging Face token'ınızı ayarlayın.")
# Model ve işlemciyi yükleyin
model_name = "cantuncokibb/autotrain-new"
processor = AutoImageProcessor.from_pretrained(model_name, token=hf_token)
model = AutoModelForImageClassification.from_pretrained(model_name, token=hf_token)
# Görsel sınıflandırma fonksiyonu
def classify_image(img):
if img is None:
return {"Hata": 1.0}
inputs = processor(images=img, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
labels = model.config.id2label
predicted_label = labels[predicted_class_idx]
probabilities = torch.nn.functional.softmax(logits, dim=-1)
return {label: float(probabilities[0, idx]) for idx, label in labels.items()}
# Gradio arayüzü
iface = gr.Interface(
fn=classify_image,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=3),
title="Görüntü Sınıflandırma Uygulaması",
description="Bu uygulama, 'cantuncokibb/autotrain-new' modeli kullanılarak görüntü sınıflandırması yapar."
)
# Uygulamayı başlatma
if __name__ == "__main__":
iface.launch() |