AbrorBalxiyev commited on
Commit
5a9c799
·
verified ·
1 Parent(s): 2ca7850

add app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -23
app.py CHANGED
@@ -1,23 +1,122 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
-
4
- # Pipeline-ni o'rnatish
5
- pipe = pipeline("text-classification", model="AbrorBalxiyev/my_awesome_model")
6
-
7
- # Klassifikatsiya funksiyasi
8
- def classify_text(text):
9
- results = pipe(text)
10
- output = {result['label']: f"{result['score'] * 100:.2f}%" for result in results}
11
- return output
12
-
13
- # Gradio interfeysini yaratish
14
- with gr.Blocks() as demo:
15
- gr.Markdown("## Text Classification Pipeline")
16
- text_input = gr.Textbox(label="Enter Text", placeholder="Type something here...")
17
- output_label = gr.Label(label="Classification Results")
18
- classify_button = gr.Button("Classify")
19
-
20
- classify_button.click(classify_text, inputs=text_input, outputs=output_label)
21
-
22
- # Interfeysni ishga tushirish
23
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+ # Initialize the pipeline
5
+ pipe = pipeline("text-classification", model="AbrorBalxiyev/text-classification", return_all_scores=True)
6
+
7
+ def get_html_for_results(results):
8
+ # Sort results by score in descending order
9
+ sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
10
+
11
+ html = """
12
+ <style>
13
+ .result-container {
14
+ font-family: Arial, sans-serif;
15
+ max-width: 600px;
16
+ margin: 20px auto;
17
+ }
18
+ .category-row {
19
+ margin: 10px 0;
20
+ }
21
+ .category-name {
22
+ display: inline-block;
23
+ width: 120px;
24
+ font-size: 14px;
25
+ color: #333;
26
+ }
27
+ .progress-bar {
28
+ display: inline-block;
29
+ width: calc(100% - 200px);
30
+ height: 20px;
31
+ background-color: #f0f0f0;
32
+ border-radius: 10px;
33
+ overflow: hidden;
34
+ margin-right: 10px;
35
+ }
36
+ .progress {
37
+ height: 100%;
38
+ background-color: #ff6b33;
39
+ border-radius: 10px;
40
+ transition: width 0.5s ease-in-out;
41
+ }
42
+ .percentage {
43
+ display: inline-block;
44
+ width: 50px;
45
+ text-align: right;
46
+ color: #666;
47
+ }
48
+ </style>
49
+ <div class="result-container">
50
+ """
51
+
52
+ for item in sorted_results:
53
+ percentage = item['score'] * 100
54
+ html += f"""
55
+ <div class="category-row">
56
+ <span class="category-name">{item['label']}</span>
57
+ <div class="progress-bar">
58
+ <div class="progress" style="width: {percentage}%;"></div>
59
+ </div>
60
+ <span class="percentage">{percentage:.0f}%</span>
61
+ </div>
62
+ """
63
+
64
+ html += "</div>"
65
+ return html
66
+
67
+ def classify_text(text):
68
+ if not text.strip():
69
+ return "Please enter some text to classify."
70
+
71
+ # Get predictions
72
+ pred = pipe(text)
73
+
74
+ # Decode predictions
75
+ decoded_data = [
76
+ {"label": label_mapping[int(item["label"].split("_")[1])],
77
+ "score": item["score"]} for item in pred[0]
78
+ ]
79
+
80
+ return get_html_for_results(decoded_data)
81
+
82
+ # Create Gradio interface
83
+ iface = gr.Interface(
84
+ fn=classify_text,
85
+ inputs=[
86
+ gr.Textbox(
87
+ placeholder="Enter text to classify...",
88
+ label=None,
89
+ lines=3
90
+ )
91
+ ],
92
+ outputs=gr.HTML(),
93
+ title="Text Category Classification",
94
+ css="""
95
+ .gradio-container {
96
+ font-family: Arial, sans-serif;
97
+ }
98
+ .gradio-interface {
99
+ max-width: 800px !important;
100
+ }
101
+ #component-0 {
102
+ border-radius: 8px;
103
+ border: 1px solid #ddd;
104
+ }
105
+ .submit-button {
106
+ background-color: #ff6b33 !important;
107
+ }
108
+ .clear-button {
109
+ background-color: #f0f0f0 !important;
110
+ color: #333 !important;
111
+ }
112
+ """,
113
+ examples=[
114
+ ["Messi jahon chempioni bo'ldi"],
115
+ ["Yangi iPhone 15 Pro Max sotuvga chiqdi"],
116
+ ["Kitob o'qish foydali"],
117
+ ["Toshkentda ob-havo issiq"]
118
+ ]
119
+ )
120
+
121
+ # Launch the interface
122
+ iface.launch(share=True)