sumonta056 commited on
Commit
1c283bc
β€’
1 Parent(s): 11701c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -13
app.py CHANGED
@@ -1,20 +1,59 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
 
4
- # model_path = "./"
5
- # model = BertForSequenceClassification.from_pretrained(model_path)
6
- # tokenizer = BertTokenizerFast.from_pretrained(model_path)
7
- # nlp = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
 
8
 
9
- # # Streamlit app
10
- # st.title("Simple Emotion Analyzer")
 
 
 
 
 
 
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- pipe = pipeline('sentiment-analysis')
 
14
 
15
- # Input text
16
- text = st.text_area("Enter text to analyze:")
17
 
18
- if text:
19
- result = pipe(text)
20
- st.json(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")