File size: 4,448 Bytes
ecc47b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# ==============================================================================
# 🚨 МӘЖБҮРЛЕП ОРНАТУ (Қажет болса): 'transformers' табылмаса, оны орнатады.
# ==============================================================================
import os
import subprocess

try:
    from transformers import pipeline
except ImportError:
    print("WARNING: transformers табылмады. Қазір орнатылуда...")
    subprocess.check_call([os.sys.executable, "-m", "pip", "install", "transformers", "accelerate", "torch", "gradio", "Pillow"])
    from transformers import pipeline

# ----------------------------------------------------------------------
# 1. КОНФИГУРАЦИЯЛАР (Қолданушы Атын Қою)
# ----------------------------------------------------------------------

import gradio as gr 

# ⭐ ҚОЛДАНУШЫ АТЫ ⭐
HF_USERNAME = "Nonabzbssbbsbs"

TRASH_MODEL_PATH = f"{HF_USERNAME}/Trash-Net-Fine-Tuned-Demo"
CIFAR_MODEL_PATH = f"{HF_USERNAME}/CIFAR10-Kazakh-Fast-Demo"

# 2. Қазақша Белгілер
CIFAR_KAZAKH_LABELS = {
    0: "Ұшақ", 1: "Автомобиль", 2: "Құс", 3: "Мысық", 4: "Бұғы", 
    5: "Ит", 6: "Бақа", 7: "Жылқы", 8: "Кеме", 9: "Жүк көлігі"
}

# ----------------------------------------------------------------------
# 2. PIPELINES ЖҮКТЕУ (Екі Модельді Де Жүктеу)
# ----------------------------------------------------------------------

print("Модельдер жүктелуде...")

# 1-ші Модель (Trash/Food)
try:
    trash_classifier = pipeline(
        "image-classification", 
        model=TRASH_MODEL_PATH
    )
except Exception as e:
    print(f"Trash-Net моделін жүктеу қатесі: {e}")
    trash_classifier = None 

# 2-ші Модель (CIFAR-10)
try:
    cifar_classifier = pipeline(
        "image-classification", 
        model=CIFAR_MODEL_PATH
    )
except Exception as e:
    print(f"CIFAR-10 моделін жүктеу қатесі: {e}")
    cifar_classifier = None 

print("Модельдерді жүктеу әрекеті аяқталды.")

# ----------------------------------------------------------------------
# 3. ЖІКТЕУ ФУНКЦИЯЛАРЫ
# ----------------------------------------------------------------------

# CIFAR-10 жіктеу
def classify_cifar(image):
    if image is None:
        return "Сурет жүктеңіз."
    if cifar_classifier is None:
        return "CIFAR-10 моделін жүктеу мүмкін болмады."
        
    results = cifar_classifier(image)
    output = {}
    
    for item in results:
        try:
            label_id = int(item['label'])
            kazakh_label = CIFAR_KAZAKH_LABELS.get(label_id, item['label']) 
            output[kazakh_label] = item['score']
        except ValueError:
            output[item['label']] = item['score']

    return output

# Trash/Food жіктеу
def classify_trash(image):
    if image is None:
        return "Сурет жүктеңіз."
    if trash_classifier is None:
        return "Trash/Food моделін жүктеу мүмкін болмады."
        
    results = trash_classifier(image)
    output = {item['label']: item['score'] for item in results}
    return output

# ----------------------------------------------------------------------
# 4. GRADIO ИНТЕРФЕЙСІ
# ----------------------------------------------------------------------

cifar_iface = gr.Interface(
    fn=classify_cifar,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    title="Қазақша CIFAR-10 Жіктеуі",
    description="CIFAR-10 моделі (10 класс). Нәтижелер қазақшаға аударылған.",
    allow_flagging="never"
)

trash_iface = gr.Interface(
    fn=classify_trash,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    title="Trash/Food Жіктеуі",
    description="Trash-Net негізіндегі модель Food101-де оқытылған (101 класс).",
    allow_flagging="never"
)

demo = gr.TabbedInterface(
    [cifar_iface, trash_iface], 
    ["1. CIFAR10 (Қазақша)", "2. Trash/Food (101 класс)"],
    title="Екі Компьютерлік Көру Моделінің Демонстрациясы"
)

if __name__ == "__main__":
    demo.launch()