sumonta056
commited on
Commit
β’
a4d1f5c
1
Parent(s):
9b77418
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import BertForSequenceClassification, BertTokenizerFast, pipeline
|
3 |
+
|
4 |
+
# Load the BERT model and tokenizer
|
5 |
+
model_path = "./"
|
6 |
+
model = BertForSequenceClassification.from_pretrained(model_path)
|
7 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_path)
|
8 |
+
nlp = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
9 |
+
|
10 |
+
# Function to update sentiment analysis
|
11 |
+
def analyze_sentiment(text):
|
12 |
+
if text.strip():
|
13 |
+
result = nlp(text)
|
14 |
+
label = result[0]['label']
|
15 |
+
score = result[0]['score']
|
16 |
+
return label, score
|
17 |
+
else:
|
18 |
+
return None, None
|
19 |
+
|
20 |
+
# Function to get emoji based on emotion
|
21 |
+
def get_emoji(label):
|
22 |
+
if label == "Anger":
|
23 |
+
return "π "
|
24 |
+
elif label == "Astonished":
|
25 |
+
return "π²"
|
26 |
+
elif label == "Optimistic":
|
27 |
+
return "π"
|
28 |
+
elif label == "Sadness":
|
29 |
+
return "π’"
|
30 |
+
else:
|
31 |
+
return "π"
|
32 |
+
|
33 |
+
# Streamlit app
|
34 |
+
st.set_page_config(page_title="G-Bert: Emotion Analysis", page_icon="π", layout="centered")
|
35 |
+
|
36 |
+
# Title
|
37 |
+
st.title("G-Bert: Emotion Analysis")
|
38 |
+
|
39 |
+
# Text input
|
40 |
+
st.write("Enter some text below, and G-Bert will analyze its emotion!")
|
41 |
+
text = st.text_area("Input Text", height=200)
|
42 |
+
|
43 |
+
# Analyze button
|
44 |
+
if st.button("Analyze Emotion"):
|
45 |
+
if text.strip():
|
46 |
+
label, score = analyze_sentiment(text)
|
47 |
+
if label and score:
|
48 |
+
emoji = get_emoji(label)
|
49 |
+
st.subheader(f"Emotion: {label}")
|
50 |
+
st.markdown(f"**Confidence Score:** {score:.2f}")
|
51 |
+
st.markdown(f"{emoji} **{label}**")
|
52 |
+
else:
|
53 |
+
st.error("Something went wrong with the analysis.")
|
54 |
+
else:
|
55 |
+
st.warning("Please enter some text to analyze.")
|
56 |
+
|
57 |
+
# Footer
|
58 |
+
st.markdown("---")
|
59 |
+
st.markdown("Built with β€οΈ using Hugging Face and Streamlit")
|