File size: 3,433 Bytes
5a9c799
9c42db7
5a9c799
 
 
 
 
63d3f41
 
 
 
 
5a9c799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
import gradio as gr
from transformers import pipeline
import numpy as np

# Initialize the pipeline
pipe = pipeline("text-classification", model="AbrorBalxiyev/text-classification", return_all_scores=True)

label_mapping = {
    0: 'Avto', 1: 'Biznes', 2: 'Iqtisodiyot', 3: 'Kino', 4: 'Kitob',
    5: 'Koinot', 6: 'Madaniyat', 7: 'Ob-havo', 8: 'Sayohat', 9: 'Sport', 10: 'Texnologiya'
}

def get_html_for_results(results):
    # Sort results by score in descending order
    sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
    
    html = """
    <style>
        .result-container {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 20px auto;
        }
        .category-row {
            margin: 10px 0;
        }
        .category-name {
            display: inline-block;
            width: 120px;
            font-size: 14px;
            color: #333;
        }
        .progress-bar {
            display: inline-block;
            width: calc(100% - 200px);
            height: 20px;
            background-color: #f0f0f0;
            border-radius: 10px;
            overflow: hidden;
            margin-right: 10px;
        }
        .progress {
            height: 100%;
            background-color: #ff6b33;
            border-radius: 10px;
            transition: width 0.5s ease-in-out;
        }
        .percentage {
            display: inline-block;
            width: 50px;
            text-align: right;
            color: #666;
        }
    </style>
    <div class="result-container">
    """
    
    for item in sorted_results:
        percentage = item['score'] * 100
        html += f"""
        <div class="category-row">
            <span class="category-name">{item['label']}</span>
            <div class="progress-bar">
                <div class="progress" style="width: {percentage}%;"></div>
            </div>
            <span class="percentage">{percentage:.0f}%</span>
        </div>
        """
    
    html += "</div>"
    return html

def classify_text(text):
    if not text.strip():
        return "Please enter some text to classify."
    
    # Get predictions
    pred = pipe(text)
    
    # Decode predictions
    decoded_data = [
        {"label": label_mapping[int(item["label"].split("_")[1])], 
         "score": item["score"]} for item in pred[0]
    ]
    
    return get_html_for_results(decoded_data)

# Create Gradio interface
iface = gr.Interface(
    fn=classify_text,
    inputs=[
        gr.Textbox(
            placeholder="Enter text to classify...",
            label=None,
            lines=3
        )
    ],
    outputs=gr.HTML(),
    title="Text Category Classification",
    css="""
        .gradio-container {
            font-family: Arial, sans-serif;
        }
        .gradio-interface {
            max-width: 800px !important;
        }
        #component-0 {
            border-radius: 8px;
            border: 1px solid #ddd;
        }
        .submit-button {
            background-color: #ff6b33 !important;
        }
        .clear-button {
            background-color: #f0f0f0 !important;
            color: #333 !important;
        }
    """,
    examples=[
        ["Messi jahon chempioni bo'ldi"],
        ["Yangi iPhone 15 Pro Max sotuvga chiqdi"],
        ["Kitob o'qish foydali"],
        ["Toshkentda ob-havo issiq"]
    ]
)

# Launch the interface
iface.launch(share=True)