Ephicho commited on
Commit
0d0ebe5
·
1 Parent(s): ed56e64

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
+
6
+ # Load your model and tokenizer
7
+ model_path = "https://huggingface.co/Ephicho/NLP_Capstone"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
9
+ config = AutoConfig.from_pretrained(model_path)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
11
+
12
+ # Preprocess text (username and link placeholders)
13
+ #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.
14
+
15
+ def preprocess(text):
16
+ new_text = []
17
+ for t in text.split(" "):
18
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
19
+ t = 'http' if t.startswith('http') else t
20
+ new_text.append(t)
21
+ return " ".join(new_text)
22
+
23
+ def sentiment_analysis(text):
24
+ text = preprocess(text)
25
+
26
+ # PyTorch-based models
27
+ encoded_input = tokenizer(text, return_tensors='pt')
28
+ output = model(**encoded_input)
29
+ scores_ = output[0][0].detach().numpy()
30
+ scores_ = softmax(scores_)
31
+
32
+ # Format output dict of scores
33
+ labels = ['Negative', 'Neutral', 'Positive']
34
+ scores = {l: float(s) for (l, s) in zip(labels, scores_)}
35
+
36
+ return scores
37
+
38
+ # Streamlit app layout with two columns
39
+ st.title("Sentiment Analysis App")
40
+ st.write(" Sentiment analysis, whether it's positive,negative, or neutral")
41
+
42
+
43
+ # Input text area for user to enter a tweet in the left column
44
+ input_text = st.text_area("Write your tweet here...")
45
+
46
+ # Output area for displaying sentiment in the right column
47
+ if st.button("Analyze Sentiment"):
48
+ if input_text:
49
+ # Perform sentiment analysis using the loaded model
50
+ scores = sentiment_analysis(input_text)
51
+
52
+ # Display sentiment scores in the right column
53
+ st.text("Sentiment Scores:")
54
+ for label, score in scores.items():
55
+ st.text(f"{label}: {score:.2f}")
56
+
57
+ # Determine the overall sentiment label
58
+ sentiment_label = max(scores, key=scores.get)
59
+
60
+ # Map sentiment labels to human-readable forms
61
+ sentiment_mapping = {
62
+ "Negative": "Negative",
63
+ "Neutral": "Neutral",
64
+ "Positive": "Positive"
65
+ }
66
+ sentiment_readable = sentiment_mapping.get(sentiment_label, "Unknown")
67
+
68
+ # Display the sentiment label in the right column
69
+ st.text(f"Sentiment: {sentiment_readable}")
70
+
71
+ # Button to Clear the input text
72
+ if st.button("Clear Input"):
73
+ input_text = ""