kajonation commited on
Commit
faeb6ce
·
verified ·
1 Parent(s): 8ceb8bc

initial commit

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import BertForSequenceClassification, BertTokenizer
3
+ from safetensors.torch import load_file
4
+ import gradio as gr
5
+
6
+ model_path = "/kaggle/input/model_12k/other/default/1/model (5).safetensors"
7
+ state_dict = load_file(model_path)
8
+
9
+ model = BertForSequenceClassification.from_pretrained('indobenchmark/indobert-base-p2', num_labels=3)
10
+ tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-base-p2')
11
+
12
+ model.load_state_dict(state_dict, strict=False)
13
+ model.eval()
14
+
15
+ def detect_stress(input_text):
16
+ inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=128)
17
+
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+
21
+ logits = outputs.logits
22
+ predicted_class = torch.argmax(logits, dim=1).item()
23
+
24
+ labels = {
25
+ 0: ("Not Stress", "#8BC34A", "Currently you are not experiencing stress. Stay on top of your health!"),
26
+ 1: ("Mild Stress", "#FF7F00", "Saat ini anda sedang mengalami stres ringan. Luangkan waktu untuk relaksasi."),
27
+ 2: ("High Stress", "#F44336", "Currently you are experiencing mild stress. Take time to relax.")
28
+ }
29
+
30
+ level, color, message = labels[predicted_class]
31
+ return f"<div style='background-color:{color}; color:white; text-align:center; padding:15px; border-radius:10px; font-size:16px; heigth:200px; width: 500px; margin:auto;'>" \
32
+ f"Level stres Anda: {level}<br>{message}" \
33
+ f"</div>"
34
+
35
+ # Apabila menggunakan model SVM atau ensemble learning
36
+ # pipeline = joblib.load("/kaggle/input/svm_model/other/default/1/svm_hybrid_pipeline.pkl")
37
+
38
+ # def detect_stress(input_text):
39
+ # predicted_class = pipeline.predict([input_text])[0]
40
+ # probs = pipeline.predict_proba([input_text])[0]
41
+ # confidence = max(probs)
42
+
43
+ # labels = {
44
+ # 0: ("Not Stress", "#8BC34A", "Currently you are not experiencing stress. Stay on top of your health!"),
45
+ # 1: ("Mild Stress", "#FF7F00", "Saat ini anda sedang mengalami stres ringan. Luangkan waktu untuk relaksasi."),
46
+ # 2: ("High Stress", "#F44336", "Currently you are experiencing mild stress. Take time to relax.")
47
+ # }
48
+
49
+ # level, color, message = labels[predicted_class]
50
+ # return f"<div style='background-color:{color}; color:white; text-align:center; padding:15px; border-radius:10px; font-size:16px; heigth:200px; width: 500px; margin:auto;'>" \
51
+ # f"Level stress anda : {level}<br>{message}" \
52
+ # f"</div>"
53
+
54
+ custom_css = """
55
+ body {
56
+ margin: 0;
57
+ padding: 0;
58
+ font-family: Arial, sans-serif;
59
+ background-color: var(--background);
60
+ color: var(--text);
61
+ transition: background-color 0.3s, color 0.3s;
62
+ }
63
+
64
+ #title {
65
+ position: fixed;
66
+ top: 0;
67
+ left: 0;
68
+ width: 100vw;
69
+ padding: 20px;
70
+ background-color: #ff7a33;
71
+ color: white;
72
+ font-size: 28px;
73
+ font-weight: bold;
74
+ text-align: center;
75
+ z-index: 1000;
76
+ }
77
+ body {
78
+ padding-top: 80px;
79
+ }
80
+
81
+ #container {
82
+ display: flex;
83
+ flex-direction: column;
84
+ align-items: center;
85
+ justify-content: center;
86
+ min-height: calc(100vh - 80px);
87
+ padding: 20px;
88
+ }
89
+
90
+ textarea {
91
+ background-color: var(--textarea-bg);
92
+ color: var(--textarea-text);
93
+ border: none;
94
+ border-radius: 5px;
95
+ padding: 10px;
96
+ font-size: 16px;
97
+ box-sizing: border-box;
98
+ resize: none;
99
+ }
100
+ textarea:focus {
101
+ outline: 2px solid #ff7a33;
102
+ }
103
+
104
+ .button_detect {
105
+ background-color: #ff7a33;
106
+ color: white;
107
+ border: none;
108
+ border-radius: 5px;
109
+ padding: 15px 30px;
110
+ font-size: 16px;
111
+ cursor: pointer;
112
+ margin-top: 10px;
113
+ width: 200px;
114
+ heigth: 100px;
115
+
116
+ }
117
+ .button_detect:hover {
118
+ background-color: #e5662c;
119
+ }
120
+
121
+ @media (prefers-color-scheme: dark) {
122
+ :root {
123
+ --background: #121212;
124
+ --text: white;
125
+ --textarea-bg: #2c2c2c;
126
+ --textarea-text: white;
127
+ }
128
+ }
129
+ @media (prefers-color-scheme: light) {
130
+ :root {
131
+ --background: #ffffff;
132
+ --text: black;
133
+ --textarea-bg: #f0f0f0;
134
+ --textarea-text: black;
135
+ }
136
+ }
137
+ """
138
+
139
+ # UI Layout
140
+ with gr.Blocks(css=custom_css) as demo:
141
+ gr.HTML("<div id='title'>Stress Detector</div>") # Banner on top
142
+
143
+ with gr.Column(elem_id="container"):
144
+ input_text = gr.Textbox(
145
+ label="Input text",
146
+ placeholder="Tell us your complaint here...",
147
+ lines=5
148
+ )
149
+ btn_submit = gr.Button("Detect", elem_classes=["button_detect"])
150
+ output_label = gr.HTML(label="Detection Results")
151
+
152
+ btn_submit.click(fn=detect_stress, inputs=input_text, outputs=output_label)
153
+
154
+ demo.launch()