kedimestan commited on
Commit
1c4953c
·
verified ·
1 Parent(s): ecad1b3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import SwinForImageClassification, SwinImageProcessor
3
+ import torch
4
+ from PIL import Image
5
+
6
+ # Cihaz ayarı
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ # Modeli ve işlemciyi doğrudan Hugging Face'den yükle
10
+ model_name = "kedimestan/swin-base-patch4-window7-224"
11
+
12
+ # Model ve işlemciyi yükle
13
+ model = SwinForImageClassification.from_pretrained(model_name)
14
+ processor = SwinImageProcessor.from_pretrained(model_name)
15
+
16
+ model = model.to(device)
17
+ model.eval()
18
+
19
+ # Tahmin fonksiyonu
20
+ def predict(image: Image.Image):
21
+ # Görüntüyü işlemci ile işleme
22
+ inputs = processor(images=image, return_tensors="pt").to(device)
23
+
24
+ # Modeli kullanarak tahmin yapma
25
+ with torch.no_grad():
26
+ logits = model(**inputs).logits
27
+
28
+ # Tahmin sonucu (maksimum sınıf)
29
+ predicted_class_idx = logits.argmax(-1).item()
30
+ prediction = f"Sınıf {predicted_class_idx}"
31
+ return prediction
32
+
33
+ # Gradio arayüzü
34
+ inputs = gr.Image(type="pil", label="Görsel Yükle")
35
+ outputs = gr.Textbox(label="Tahmin Sonucu")
36
+
37
+ gr.Interface(
38
+ fn=predict,
39
+ inputs=inputs,
40
+ outputs=outputs,
41
+ title="Swin Transformer Görüntü Sınıflandırma",
42
+ theme="default"
43
+ ).launch(debug=True)