Spaces:
Sleeping
Sleeping
Added application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
bg_img = Path("faces.jpeg")
|
| 6 |
+
|
| 7 |
+
model_id = 'user-anto/bert-emotion-classifier'
|
| 8 |
+
classifier = pipeline('text-classification', model=model_id)
|
| 9 |
+
|
| 10 |
+
label_map = {
|
| 11 |
+
"label_0": "neutral", "label_1": "anger", "label_2": "love",
|
| 12 |
+
"label_3": "fear", "label_4": "hate", "label_5": "happiness",
|
| 13 |
+
"label_6": "sadness", "label_7": "surprise"
|
| 14 |
+
}
|
| 15 |
+
emoji_map = {
|
| 16 |
+
"neutral": "π", "anger": "π ", "love": "β€",
|
| 17 |
+
"fear": "π¨", "hate": "π€¬", "happiness": "π",
|
| 18 |
+
"sadness": "π’", "surprise": "π²"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def predict_emotion(text):
|
| 22 |
+
if not text:
|
| 23 |
+
return ""
|
| 24 |
+
pred = classifier(text)[0]
|
| 25 |
+
label_raw = pred["label"].lower()
|
| 26 |
+
label = label_map[label_raw]
|
| 27 |
+
score = pred["score"] * 100
|
| 28 |
+
emoji = emoji_map.get(label, "")
|
| 29 |
+
return f"{label.capitalize()} {emoji} ({score:.1f}%)"
|
| 30 |
+
|
| 31 |
+
with gr.Blocks(
|
| 32 |
+
css=f"""
|
| 33 |
+
/* Ensure the body and html take full height */
|
| 34 |
+
html, body {{
|
| 35 |
+
height: 100%;
|
| 36 |
+
margin: 0;
|
| 37 |
+
padding: 0;
|
| 38 |
+
overflow: hidden; /* Hide scrollbars if content is within view */
|
| 39 |
+
}}
|
| 40 |
+
|
| 41 |
+
/* Apply background image to the body or the main Gradio container */
|
| 42 |
+
body {{
|
| 43 |
+
background-image: url("file/{bg_img}"); /* Use the background image */
|
| 44 |
+
background-size: cover; /* Cover the entire area */
|
| 45 |
+
background-position: center; /* Center the image */
|
| 46 |
+
background-repeat: no-repeat; /* Do not repeat the image */
|
| 47 |
+
background-attachment: fixed; /* Fix the background image during scroll */
|
| 48 |
+
}}
|
| 49 |
+
|
| 50 |
+
/* Main Gradio container styling */
|
| 51 |
+
.gradio-container {{
|
| 52 |
+
padding: 20px; /* Add some padding around the content */
|
| 53 |
+
color:white; /* Sets text color to white */
|
| 54 |
+
text-shadow:1px 1px 2px black; /* Adds text shadow for better readability */
|
| 55 |
+
display: flex; /* Use flexbox for centering */
|
| 56 |
+
flex-direction: column; /* Stack items vertically */
|
| 57 |
+
justify-content: center; /* Center content vertically */
|
| 58 |
+
align-items: center; /* Center content horizontally */
|
| 59 |
+
min-height: 100vh; /* Ensure container takes full viewport height */
|
| 60 |
+
box-sizing: border-box; /* Include padding in element's total width and height */
|
| 61 |
+
background: rgba(0,0,0,0.5); /* Semi-transparent overlay to make text more readable */
|
| 62 |
+
}}
|
| 63 |
+
|
| 64 |
+
/* Target specific Gradio components to refine their appearance over the background */
|
| 65 |
+
.gr-textbox, .gr-markdown {{
|
| 66 |
+
background: rgba(0,0,0,0.5); /* Slightly transparent dark background for readability */
|
| 67 |
+
border-radius: 8px; /* Rounded corners */
|
| 68 |
+
padding: 15px; /* Add padding inside components */
|
| 69 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2); /* Subtle shadow */
|
| 70 |
+
}}
|
| 71 |
+
|
| 72 |
+
input[type='text'], textarea {{
|
| 73 |
+
background: rgba(255,255,255,0.1); /* Slightly transparent white background for input fields */
|
| 74 |
+
color: white; /* White text color */
|
| 75 |
+
border: 1px solid rgba(255,255,255,0.3); /* Light border */
|
| 76 |
+
border-radius: 5px; /* Rounded corners for input fields */
|
| 77 |
+
padding: 10px; /* Padding for input fields */
|
| 78 |
+
}}
|
| 79 |
+
|
| 80 |
+
/* Style for the button */
|
| 81 |
+
.gr-button {{
|
| 82 |
+
background: linear-gradient(145deg, #007bff, #0056b3); /* Blue gradient */
|
| 83 |
+
color: orange;
|
| 84 |
+
border: none;
|
| 85 |
+
border-radius: 8px;
|
| 86 |
+
padding: 10px 20px;
|
| 87 |
+
font-size: 1.1em;
|
| 88 |
+
cursor: pointer;
|
| 89 |
+
transition: all 0.3s ease;
|
| 90 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 91 |
+
}}
|
| 92 |
+
.gr-button:hover {{
|
| 93 |
+
background: linear-gradient(145deg, #0056b3, #007bff);
|
| 94 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.2);
|
| 95 |
+
transform: translateY(-2px);
|
| 96 |
+
}}
|
| 97 |
+
|
| 98 |
+
/* Styling for the Markdown headlines */
|
| 99 |
+
.gradio-container h2 {{
|
| 100 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* Modern font */
|
| 101 |
+
color: #FFD700; /* Gold color for headlines */
|
| 102 |
+
text-align: center;
|
| 103 |
+
padding: 10px 20px;
|
| 104 |
+
border-radius: 10px;
|
| 105 |
+
background: rgba(255, 255, 255, 0.15); /* Slightly transparent background */
|
| 106 |
+
backdrop-filter: blur(5px); /* Frosted glass effect */
|
| 107 |
+
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); /* Deeper shadow */
|
| 108 |
+
margin-bottom: 20px;
|
| 109 |
+
letter-spacing: 1.5px; /* Spacing between letters */
|
| 110 |
+
text-transform: uppercase; /* Uppercase text */
|
| 111 |
+
border: 1px solid rgba(255, 215, 0, 0.5); /* Gold border */
|
| 112 |
+
}}
|
| 113 |
+
.gradio-container h3 {{ /* If you use h3, it will also be styled */
|
| 114 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 115 |
+
color: #ADD8E6; /* Light blue color for sub-headlines */
|
| 116 |
+
text-align: center;
|
| 117 |
+
margin-bottom: 15px;
|
| 118 |
+
text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
|
| 119 |
+
}}
|
| 120 |
+
"""
|
| 121 |
+
,
|
| 122 |
+
) as demo:
|
| 123 |
+
with gr.Column(
|
| 124 |
+
) as main_content_column:
|
| 125 |
+
gr.Markdown("## π§ BERT Emotion Classifier")
|
| 126 |
+
gr.Markdown("### Enter a sentence and see what BERT detects.")
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
text_input = gr.Textbox(label="Input Text", placeholder="Type your sentence here...")
|
| 130 |
+
|
| 131 |
+
predict_button = gr.Button("Predict Emotion")
|
| 132 |
+
output = gr.Textbox(label="Predicted Emotion")
|
| 133 |
+
|
| 134 |
+
predict_button.click(fn=predict_emotion, inputs=text_input, outputs=output)
|
| 135 |
+
|
| 136 |
+
if __name__ == "__main__":
|
| 137 |
+
demo.launch(share=True)
|