Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +3 -3
- app.py +44 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.41.0
|
|
|
1 |
---
|
2 |
+
title: Gradio2
|
3 |
+
emoji: 👁
|
4 |
+
colorFrom: gray
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.41.0
|
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Hugging Face tokeninizi çevresel değişkenden alın
|
7 |
+
hf_token = os.getenv("HF_TOKEN")
|
8 |
+
if not hf_token:
|
9 |
+
raise ValueError("HF_TOKEN çevresel değişkeni ayarlanmamış. Lütfen Hugging Face token'ınızı ayarlayın.")
|
10 |
+
|
11 |
+
# Model ve işlemciyi yükleyin
|
12 |
+
model_name = "cantuncok/autotrain1-model"
|
13 |
+
processor = AutoImageProcessor.from_pretrained(model_name, token=hf_token)
|
14 |
+
model = AutoModelForImageClassification.from_pretrained(model_name, token=hf_token)
|
15 |
+
|
16 |
+
# Görsel sınıflandırma fonksiyonu
|
17 |
+
def classify_image(img):
|
18 |
+
if img is None:
|
19 |
+
return {"Hata": 1.0}
|
20 |
+
|
21 |
+
inputs = processor(images=img, return_tensors="pt")
|
22 |
+
with torch.no_grad():
|
23 |
+
outputs = model(**inputs)
|
24 |
+
|
25 |
+
logits = outputs.logits
|
26 |
+
predicted_class_idx = logits.argmax(-1).item()
|
27 |
+
|
28 |
+
labels = model.config.id2label
|
29 |
+
predicted_label = labels[predicted_class_idx]
|
30 |
+
probabilities = torch.nn.functional.softmax(logits, dim=-1)
|
31 |
+
return {label: float(probabilities[0, idx]) for idx, label in labels.items()}
|
32 |
+
|
33 |
+
# Gradio arayüzü
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=classify_image,
|
36 |
+
inputs=gr.Image(type="pil"),
|
37 |
+
outputs=gr.Label(num_top_classes=3),
|
38 |
+
title="Görüntü Sınıflandırma Uygulaması",
|
39 |
+
description="Bu uygulama, 'cantuncok/autotrain1-model' modeli kullanılarak görüntü sınıflandırması yapar."
|
40 |
+
)
|
41 |
+
|
42 |
+
# Uygulamayı başlatma
|
43 |
+
if __name__ == "__main__":
|
44 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
Pillow
|
4 |
+
gradio
|