HerbertAIHug commited on
Commit
6c2ff93
1 Parent(s): 52b48e8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
3
+ from scipy.special import softmax
4
+
5
+ # Load your model and tokenizer
6
+ model_path = "HerbertAIHug/Finetuned-Roberta-Base-Sentiment-identifier"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
8
+ config = AutoConfig.from_pretrained(model_path)
9
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
10
+
11
+ # Preprocess text (username and link placeholders)
12
+ #In summary, this preprocessing function helps ensure that usernames and links in the input text do not interfere with the sentiment analysis performed by the model. It replaces them with placeholder tokens to maintain the integrity of the text's structure while anonymizing or standardizing specific elements.
13
+
14
+ def preprocess(text):
15
+ new_text = []
16
+ for t in text.split(" "):
17
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
18
+ t = 'http' if t.startswith('http') else t
19
+ new_text.append(t)
20
+ return " ".join(new_text)
21
+
22
+ def sentiment_analysis(text):
23
+ text = preprocess(text)
24
+
25
+ # PyTorch-based models
26
+ encoded_input = tokenizer(text, return_tensors='pt')
27
+ output = model(**encoded_input)
28
+ scores_ = output[0][0].detach().numpy()
29
+ scores_ = softmax(scores_)
30
+
31
+ # Format output dict of scores
32
+ labels = ['Negative', 'Neutral', 'Positive']
33
+ scores = {l: float(s) for (l, s) in zip(labels, scores_)}
34
+
35
+ return scores
36
+
37
+ # Streamlit app layout with two columns
38
+ st.title("Sentiment Analysis App")
39
+ st.write(" Sentiment analysis, also known as opinion mining, is the process of determining the emotional tone or sentiment expressed in text data, whether it's positive,negative, or neutral")
40
+ # st.image("Assets/sent_emoji.png", caption="Sentiments examples", use_column_width=True)
41
+
42
+ # Input text area for user to enter a tweet in the left column
43
+ input_text = st.text_area("Write your tweet here...")
44
+
45
+ # Output area for displaying sentiment in the right column
46
+ if st.button("Analyze Sentiment"):
47
+ if input_text:
48
+ # Perform sentiment analysis using the loaded model
49
+ scores = sentiment_analysis(input_text)
50
+
51
+ # Display sentiment scores in the right column
52
+ st.text("Sentiment Scores:")
53
+ for label, score in scores.items():
54
+ st.text(f"{label}: {score:.2f}")
55
+
56
+ # Determine the overall sentiment label
57
+ sentiment_label = max(scores, key=scores.get)
58
+
59
+ # Map sentiment labels to human-readable forms
60
+ sentiment_mapping = {
61
+ "Negative": "Negative",
62
+ "Neutral": "Neutral",
63
+ "Positive": "Positive"
64
+ }
65
+ sentiment_readable = sentiment_mapping.get(sentiment_label, "Unknown")
66
+
67
+ # Display the sentiment label in the right column
68
+ st.text(f"Sentiment: {sentiment_readable}")
69
+
70
+ # Button to Clear the input text
71
+ if st.button("Clear Input"):
72
+ input_text = ""
73
+