File size: 5,334 Bytes
baf2af5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import gradio as gr
from transformers import pipeline

bg_img = Path("faces.jpeg") 

model_id = 'user-anto/bert-emotion-classifier'
classifier = pipeline('text-classification', model=model_id)

label_map = {
    "label_0": "neutral", "label_1": "anger", "label_2": "love",
    "label_3": "fear", "label_4": "hate", "label_5": "happiness",
    "label_6": "sadness", "label_7": "surprise"
}
emoji_map = {
    "neutral": "😐", "anger": "😠", "love": "❀",
    "fear": "😨", "hate": "🀬", "happiness": "😊",
    "sadness": "😒", "surprise": "😲"
}

def predict_emotion(text):
    if not text:
        return ""
    pred = classifier(text)[0]
    label_raw = pred["label"].lower()
    label = label_map[label_raw]
    score = pred["score"] * 100
    emoji = emoji_map.get(label, "")
    return f"{label.capitalize()} {emoji} ({score:.1f}%)"

with gr.Blocks(
    css=f"""
    /* Ensure the body and html take full height */
    html, body {{
        height: 100%;
        margin: 0;
        padding: 0;
        overflow: hidden; /* Hide scrollbars if content is within view */
    }}

    /* Apply background image to the body or the main Gradio container */
    body {{
        background-image: url("file/{bg_img}"); /* Use the background image */
        background-size: cover; /* Cover the entire area */
        background-position: center; /* Center the image */
        background-repeat: no-repeat; /* Do not repeat the image */
        background-attachment: fixed; /* Fix the background image during scroll */
    }}

    /* Main Gradio container styling */
    .gradio-container {{ 
        padding: 20px; /* Add some padding around the content */
        color:white; /* Sets text color to white */
        text-shadow:1px 1px 2px black; /* Adds text shadow for better readability */
        display: flex; /* Use flexbox for centering */
        flex-direction: column; /* Stack items vertically */
        justify-content: center; /* Center content vertically */
        align-items: center; /* Center content horizontally */
        min-height: 100vh; /* Ensure container takes full viewport height */
        box-sizing: border-box; /* Include padding in element's total width and height */
        background: rgba(0,0,0,0.5); /* Semi-transparent overlay to make text more readable */
    }}

    /* Target specific Gradio components to refine their appearance over the background */
    .gr-textbox, .gr-markdown {{
        background: rgba(0,0,0,0.5); /* Slightly transparent dark background for readability */
        border-radius: 8px; /* Rounded corners */
        padding: 15px; /* Add padding inside components */
        box-shadow: 0 4px 8px rgba(0,0,0,0.2); /* Subtle shadow */
    }}

    input[type='text'], textarea {{ 
        background: rgba(255,255,255,0.1); /* Slightly transparent white background for input fields */
        color: white; /* White text color */
        border: 1px solid rgba(255,255,255,0.3); /* Light border */
        border-radius: 5px; /* Rounded corners for input fields */
        padding: 10px; /* Padding for input fields */
    }}

    /* Style for the button */
    .gr-button {{
        background: linear-gradient(145deg, #007bff, #0056b3); /* Blue gradient */
        color: orange;
        border: none;
        border-radius: 8px;
        padding: 10px 20px;
        font-size: 1.1em;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }}
    .gr-button:hover {{
        background: linear-gradient(145deg, #0056b3, #007bff);
        box-shadow: 0 6px 10px rgba(0, 0, 0, 0.2);
        transform: translateY(-2px);
    }}

    /* Styling for the Markdown headlines */
    .gradio-container h2 {{
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* Modern font */
        color: #FFD700; /* Gold color for headlines */
        text-align: center;
        padding: 10px 20px;
        border-radius: 10px;
        background: rgba(255, 255, 255, 0.15); /* Slightly transparent background */
        backdrop-filter: blur(5px); /* Frosted glass effect */
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); /* Deeper shadow */
        margin-bottom: 20px;
        letter-spacing: 1.5px; /* Spacing between letters */
        text-transform: uppercase; /* Uppercase text */
        border: 1px solid rgba(255, 215, 0, 0.5); /* Gold border */
    }}
    .gradio-container h3 {{ /* If you use h3, it will also be styled */
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        color: #ADD8E6; /* Light blue color for sub-headlines */
        text-align: center;
        margin-bottom: 15px;
        text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
    }}
    """
    ,
) as demo:
    with gr.Column(
    ) as main_content_column:
        gr.Markdown("## 🧠 BERT Emotion Classifier")
        gr.Markdown("### Enter a sentence and see what BERT detects.")

        with gr.Row():
            text_input = gr.Textbox(label="Input Text", placeholder="Type your sentence here...")
        
        predict_button = gr.Button("Predict Emotion")
        output = gr.Textbox(label="Predicted Emotion")

        predict_button.click(fn=predict_emotion, inputs=text_input, outputs=output)

if __name__ == "__main__":
    demo.launch(share=True)