Spaces:
Runtime error
Runtime error
initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import nltk
|
4 |
+
import re
|
5 |
+
import string
|
6 |
+
|
7 |
+
# Download necessary NLTK resources
|
8 |
+
nltk.download('punkt')
|
9 |
+
nltk.download('wordnet')
|
10 |
+
|
11 |
+
# Load the sentiment analysis model
|
12 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest", tokenizer="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
13 |
+
|
14 |
+
# Text preprocessing functions
|
15 |
+
def remove_urls(text):
|
16 |
+
return re.sub(r'http[s]?://\S+', '', text)
|
17 |
+
|
18 |
+
def remove_punctuation(text):
|
19 |
+
regular_punct = string.punctuation
|
20 |
+
return re.sub(r'[' + regular_punct + ']', '', text)
|
21 |
+
|
22 |
+
def lower_case(text):
|
23 |
+
return text.lower()
|
24 |
+
|
25 |
+
def lemmatize(text):
|
26 |
+
wordnet_lemmatizer = nltk.WordNetLemmatizer()
|
27 |
+
tokens = nltk.word_tokenize(text)
|
28 |
+
lemmatized_text = [wordnet_lemmatizer.lemmatize(w) for w in tokens]
|
29 |
+
return ' '.join(lemmatized_text)
|
30 |
+
|
31 |
+
# Streamlit UI
|
32 |
+
def main():
|
33 |
+
st.title("Sentiment Analysis")
|
34 |
+
st.write("Enter the text you'd like to analyze:")
|
35 |
+
|
36 |
+
user_input = st.text_area("Text Input", height=150)
|
37 |
+
if st.button("Analyze Sentiment"):
|
38 |
+
if user_input:
|
39 |
+
# Preprocess the text
|
40 |
+
text = remove_urls(user_input)
|
41 |
+
text = remove_punctuation(text)
|
42 |
+
text = lower_case(text)
|
43 |
+
text = lemmatize(text)
|
44 |
+
|
45 |
+
# Perform sentiment analysis
|
46 |
+
try:
|
47 |
+
result = sentiment_analyzer(text)
|
48 |
+
st.write(result)
|
49 |
+
except Exception as e:
|
50 |
+
st.error(f"Error analyzing sentiment: {str(e)}")
|
51 |
+
else:
|
52 |
+
st.error("Please enter some text to analyze.")
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
main()
|