kajonation commited on
Commit
ed9fd10
·
verified ·
1 Parent(s): b5e2dd0

initial commit

Browse files
Files changed (1) hide show
  1. app.py +110 -110
app.py CHANGED
@@ -1,110 +1,110 @@
1
- import torch
2
- from transformers import BertForSequenceClassification, BertTokenizer
3
- from safetensors.torch import load_file
4
- import gradio as gr
5
-
6
- # Load model dan tokenizer
7
- model_path = "/kaggle/input/model_prediction/other/default/1/model (2).safetensors"
8
- state_dict = load_file(model_path)
9
-
10
- model = BertForSequenceClassification.from_pretrained('indobenchmark/indobert-base-p2', num_labels=3)
11
- tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-base-p2')
12
-
13
- model.load_state_dict(state_dict, strict=False)
14
- model.eval() # Set model ke mode evaluasi
15
-
16
- # Fungsi deteksi stres dengan model
17
- def detect_stress(input_text):
18
- # Tokenisasi input teks
19
- inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=128)
20
-
21
- # Inference
22
- with torch.no_grad():
23
- outputs = model(**inputs)
24
-
25
- # Mengambil prediksi
26
- logits = outputs.logits
27
- predicted_class = torch.argmax(logits, dim=1).item()
28
-
29
- # Label, warna, dan pesan berdasarkan tingkat stres
30
- labels = {
31
- 0: ("Tidak Stres", "#8BC34A", "Saat ini anda tidak mengalami stres. Tetap jaga kesehatan Anda!"),
32
- 1: ("Stres Ringan", "#FF7F00", "Saat ini anda sedang mengalami stres ringan. Luangkan waktu untuk relaksasi."),
33
- 2: ("Stres Berat", "#F44336", "Saat ini anda sedang mengalami stres berat. Mohon untuk segera melakukan konsultasi.")
34
- }
35
-
36
- level, color, message = labels[predicted_class]
37
- return f"<div style='color: white; background-color: {color}; padding: 10px; border-radius: 5px;'>" \
38
- f"Level stress anda : {level}<br>{message}" \
39
- f"</div>"
40
-
41
- # Komponen Gradio
42
- with gr.Blocks(css="""
43
- body {
44
- background-color: black;
45
- color: white;
46
- font-family: Arial, sans-serif;
47
- }
48
- .gradio-container {
49
- width: 100%;
50
- max-width: 600px;
51
- margin: 0 auto;
52
- text-align: center;
53
- }
54
- #title {
55
- background-color: #ff7a33;
56
- padding: 20px;
57
- font-size: 24px;
58
- font-weight: bold;
59
- }
60
- textarea {
61
- background-color: #3a3a3a;
62
- color: white;
63
- border: none;
64
- border-radius: 5px;
65
- padding: 5px;
66
- font-size: 14px;
67
- }
68
-
69
- textarea:focus {
70
- border-color: #ff7a33 !important;
71
- }
72
- .button_detect {
73
- background-color: #ff7a33;
74
- color: white;
75
- border: none;
76
- border-radius: 5px;
77
- width: 20px;
78
- height: 50px;
79
- font-size: 14px;
80
- cursor: pointer;
81
- }
82
- .button_detect:hover {
83
- background-color: #e5662c;
84
- }
85
- """) as demo:
86
- with gr.Row():
87
- gr.Markdown("<h1 id='title'>Stress Detector</h1>")
88
-
89
- with gr.Row():
90
- input_text = gr.Textbox(label="Masukkan teks", placeholder="Ketik sesuatu di sini...", lines=3)
91
-
92
- # Tombol submit
93
- with gr.Row():
94
- btn_submit = gr.Button("Deteksi", elem_classes ="button_detect")
95
-
96
- with gr.Row():
97
- output_label = gr.HTML(label="Hasil Deteksi")
98
-
99
- with gr.Row():
100
- gr.Markdown(
101
- "<p style='text-align: center;'>"
102
- "0 untuk <b>Tidak Stress</b>, 1 untuk <b>Stress</b>, dan 2 untuk <b>Stress Berat</b>."
103
- "</p>"
104
- )
105
-
106
- # Interaksi Gradio
107
- btn_submit.click(fn=detect_stress, inputs=input_text, outputs=output_label)
108
-
109
- # Jalankan demo
110
- demo.launch()
 
1
+ import torch
2
+ from transformers import BertForSequenceClassification, BertTokenizer
3
+ from safetensors.torch import load_file
4
+ import gradio as gr
5
+
6
+ # Load model dan tokenizer
7
+ model_path = "model (5).safetensors"
8
+ state_dict = load_file(model_path)
9
+
10
+ model = BertForSequenceClassification.from_pretrained('indobenchmark/indobert-base-p2', num_labels=3)
11
+ tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-base-p2')
12
+
13
+ model.load_state_dict(state_dict, strict=False)
14
+ model.eval() # Set model ke mode evaluasi
15
+
16
+ # Fungsi deteksi stres dengan model
17
+ def detect_stress(input_text):
18
+ # Tokenisasi input teks
19
+ inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=128)
20
+
21
+ # Inference
22
+ with torch.no_grad():
23
+ outputs = model(**inputs)
24
+
25
+ # Mengambil prediksi
26
+ logits = outputs.logits
27
+ predicted_class = torch.argmax(logits, dim=1).item()
28
+
29
+ # Label, warna, dan pesan berdasarkan tingkat stres
30
+ labels = {
31
+ 0: ("Tidak Stres", "#8BC34A", "Saat ini anda tidak mengalami stres. Tetap jaga kesehatan Anda!"),
32
+ 1: ("Stres Ringan", "#FF7F00", "Saat ini anda sedang mengalami stres ringan. Luangkan waktu untuk relaksasi."),
33
+ 2: ("Stres Berat", "#F44336", "Saat ini anda sedang mengalami stres berat. Mohon untuk segera melakukan konsultasi.")
34
+ }
35
+
36
+ level, color, message = labels[predicted_class]
37
+ return f"<div style='color: white; background-color: {color}; padding: 10px; border-radius: 5px;'>" \
38
+ f"Level stress anda : {level}<br>{message}" \
39
+ f"</div>"
40
+
41
+ # Komponen Gradio
42
+ with gr.Blocks(css="""
43
+ body {
44
+ background-color: black;
45
+ color: white;
46
+ font-family: Arial, sans-serif;
47
+ }
48
+ .gradio-container {
49
+ width: 100%;
50
+ max-width: 600px;
51
+ margin: 0 auto;
52
+ text-align: center;
53
+ }
54
+ #title {
55
+ background-color: #ff7a33;
56
+ padding: 20px;
57
+ font-size: 24px;
58
+ font-weight: bold;
59
+ }
60
+ textarea {
61
+ background-color: #3a3a3a;
62
+ color: white;
63
+ border: none;
64
+ border-radius: 5px;
65
+ padding: 5px;
66
+ font-size: 14px;
67
+ }
68
+
69
+ textarea:focus {
70
+ border-color: #ff7a33 !important;
71
+ }
72
+ .button_detect {
73
+ background-color: #ff7a33;
74
+ color: white;
75
+ border: none;
76
+ border-radius: 5px;
77
+ width: 20px;
78
+ height: 50px;
79
+ font-size: 14px;
80
+ cursor: pointer;
81
+ }
82
+ .button_detect:hover {
83
+ background-color: #e5662c;
84
+ }
85
+ """) as demo:
86
+ with gr.Row():
87
+ gr.Markdown("<h1 id='title'>Stress Detector</h1>")
88
+
89
+ with gr.Row():
90
+ input_text = gr.Textbox(label="Masukkan teks", placeholder="Ketik sesuatu di sini...", lines=3)
91
+
92
+ # Tombol submit
93
+ with gr.Row():
94
+ btn_submit = gr.Button("Deteksi", elem_classes ="button_detect")
95
+
96
+ with gr.Row():
97
+ output_label = gr.HTML(label="Hasil Deteksi")
98
+
99
+ with gr.Row():
100
+ gr.Markdown(
101
+ "<p style='text-align: center;'>"
102
+ "0 untuk <b>Tidak Stress</b>, 1 untuk <b>Stress</b>, dan 2 untuk <b>Stress Berat</b>."
103
+ "</p>"
104
+ )
105
+
106
+ # Interaksi Gradio
107
+ btn_submit.click(fn=detect_stress, inputs=input_text, outputs=output_label)
108
+
109
+ # Jalankan demo
110
+ demo.launch()