File size: 4,728 Bytes
faeb6ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import torch
from transformers import BertForSequenceClassification, BertTokenizer
from safetensors.torch import load_file
import gradio as gr

model_path = "/kaggle/input/model_12k/other/default/1/model (5).safetensors"
state_dict = load_file(model_path)

model = BertForSequenceClassification.from_pretrained('indobenchmark/indobert-base-p2', num_labels=3)
tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-base-p2')

model.load_state_dict(state_dict, strict=False)
model.eval()

def detect_stress(input_text):
    inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=128)

    with torch.no_grad():
        outputs = model(**inputs)

    logits = outputs.logits
    predicted_class = torch.argmax(logits, dim=1).item()

    labels = {
        0: ("Not Stress", "#8BC34A", "Currently you are not experiencing stress. Stay on top of your health!"),
        1: ("Mild Stress", "#FF7F00", "Saat ini anda sedang mengalami stres ringan. Luangkan waktu untuk relaksasi."),
        2: ("High Stress", "#F44336", "Currently you are experiencing mild stress. Take time to relax.")
    }

    level, color, message = labels[predicted_class]
    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;'>" \
           f"Level stres Anda: {level}<br>{message}" \
           f"</div>"

# Apabila menggunakan model SVM atau ensemble learning
# pipeline = joblib.load("/kaggle/input/svm_model/other/default/1/svm_hybrid_pipeline.pkl")

# def detect_stress(input_text):
#     predicted_class = pipeline.predict([input_text])[0]
#     probs = pipeline.predict_proba([input_text])[0]
#     confidence = max(probs)
    
#     labels = {
#         0: ("Not Stress", "#8BC34A", "Currently you are not experiencing stress. Stay on top of your health!"),
#         1: ("Mild Stress", "#FF7F00", "Saat ini anda sedang mengalami stres ringan. Luangkan waktu untuk relaksasi."),
#         2: ("High Stress", "#F44336", "Currently you are experiencing mild stress. Take time to relax.")
#     }

#     level, color, message = labels[predicted_class]
#     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;'>" \
#            f"Level stress anda : {level}<br>{message}" \
#            f"</div>"

custom_css = """

body {

    margin: 0;

    padding: 0;

    font-family: Arial, sans-serif;

    background-color: var(--background);

    color: var(--text);

    transition: background-color 0.3s, color 0.3s;

}



#title {

    position: fixed;

    top: 0;

    left: 0;

    width: 100vw;

    padding: 20px;

    background-color: #ff7a33;

    color: white;

    font-size: 28px;

    font-weight: bold;

    text-align: center;

    z-index: 1000;

}

body {

    padding-top: 80px;

}



#container {

    display: flex;

    flex-direction: column;

    align-items: center;

    justify-content: center;

    min-height: calc(100vh - 80px);

    padding: 20px;

}



textarea {

    background-color: var(--textarea-bg);

    color: var(--textarea-text);

    border: none;

    border-radius: 5px;

    padding: 10px;

    font-size: 16px;

    box-sizing: border-box;

    resize: none;

}

textarea:focus {

    outline: 2px solid #ff7a33;

}



.button_detect {

    background-color: #ff7a33;

    color: white;

    border: none;

    border-radius: 5px;

    padding: 15px 30px;

    font-size: 16px;

    cursor: pointer;

    margin-top: 10px;

    width: 200px;

    heigth: 100px;

    

}

.button_detect:hover {

    background-color: #e5662c;

}



@media (prefers-color-scheme: dark) {

    :root {

        --background: #121212;

        --text: white;

        --textarea-bg: #2c2c2c;

        --textarea-text: white;

    }

}

@media (prefers-color-scheme: light) {

    :root {

        --background: #ffffff;

        --text: black;

        --textarea-bg: #f0f0f0;

        --textarea-text: black;

    }

}

"""

# UI Layout
with gr.Blocks(css=custom_css) as demo:
    gr.HTML("<div id='title'>Stress Detector</div>")  # Banner on top

    with gr.Column(elem_id="container"):
        input_text = gr.Textbox(
            label="Input text",
            placeholder="Tell us your complaint here...",
            lines=5
        )
        btn_submit = gr.Button("Detect", elem_classes=["button_detect"])
        output_label = gr.HTML(label="Detection Results")

        btn_submit.click(fn=detect_stress, inputs=input_text, outputs=output_label)

demo.launch()