Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
classifier = pipeline(task="text-classification", model="SamLowe/roberta-base-go_emotions", top_k=None)
|
5 |
+
|
6 |
+
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="Emotion Detection",
|
9 |
+
page_icon=":bar_chart:",
|
10 |
+
layout="centered",
|
11 |
+
)
|
12 |
+
|
13 |
+
|
14 |
+
st.markdown(
|
15 |
+
"""
|
16 |
+
<style>
|
17 |
+
.stButton > button {
|
18 |
+
background-color: #4CAF50;
|
19 |
+
color: white;
|
20 |
+
font-size: 18px;
|
21 |
+
padding: 10px 20px;
|
22 |
+
border: none;
|
23 |
+
cursor: pointer;
|
24 |
+
}
|
25 |
+
.stButton > button:hover {
|
26 |
+
background-color: #86D8DB;
|
27 |
+
}
|
28 |
+
.stApp {
|
29 |
+
background-color: #73D9C8; /* Background color */
|
30 |
+
}
|
31 |
+
</style>
|
32 |
+
""",
|
33 |
+
unsafe_allow_html=True,
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
st.title("🎭 Emotion Detection")
|
38 |
+
st.markdown("Choose the input type and enter a sentence or upload an image to classify emotions.")
|
39 |
+
|
40 |
+
|
41 |
+
input_type = st.radio("Select Input Type", ("Text", "Image"))
|
42 |
+
|
43 |
+
|
44 |
+
if input_type == "Text":
|
45 |
+
user_input = st.text_area("Enter a sentence:")
|
46 |
+
uploaded_image = None
|
47 |
+
else:
|
48 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
49 |
+
user_input = ""
|
50 |
+
|
51 |
+
|
52 |
+
if st.button("Analyze"):
|
53 |
+
with st.spinner("Analyzing..."):
|
54 |
+
if input_type == "Text" and user_input:
|
55 |
+
|
56 |
+
model_outputs = classifier(user_input)
|
57 |
+
st.subheader("Emotion Classification Results (Text):")
|
58 |
+
elif input_type == "Image" and uploaded_image is not None:
|
59 |
+
|
60 |
+
st.image(uploaded_image, use_column_width=True, caption="Uploaded Image")
|
61 |
+
model_outputs = classifier("Analyze this image.")
|
62 |
+
st.subheader("Emotion Classification Results (Image):")
|
63 |
+
else:
|
64 |
+
st.warning("Please enter a sentence or upload an image to analyze.")
|
65 |
+
|
66 |
+
for label_info in model_outputs[0]:
|
67 |
+
label = label_info["label"]
|
68 |
+
score = label_info["score"]
|
69 |
+
st.write(f"- {label}: {score:.4f}")
|
70 |
+
|
71 |
+
|
72 |
+
if st.button("Clear"):
|
73 |
+
user_input = ""
|
74 |
+
uploaded_image = None
|