Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ from flair.models import TextClassifier
|
|
| 5 |
from flair.data import Sentence
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Function to perform sentiment analysis using TextBlob model
|
| 9 |
def textblob_sentiment(text):
|
| 10 |
blob = TextBlob(text)
|
|
@@ -18,7 +21,6 @@ def vader_sentiment(text):
|
|
| 18 |
|
| 19 |
# Function to perform sentiment analysis using Flair model
|
| 20 |
def flair_sentiment(text):
|
| 21 |
-
classifier = TextClassifier.load('en-sentiment')
|
| 22 |
sentence = Sentence(text)
|
| 23 |
classifier.predict(sentence)
|
| 24 |
if sentence.labels[0].value == 'POSITIVE':
|
|
@@ -50,3 +52,4 @@ ax.bar(['TextBlob', 'VADER', 'Flair'], [textblob_score, vader_score, flair_score
|
|
| 50 |
ax.axhline(y=0, color='gray', linestyle='--')
|
| 51 |
ax.set_title('Sentiment Scores')
|
| 52 |
st.pyplot(fig)
|
|
|
|
|
|
| 5 |
from flair.data import Sentence
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
+
# Load Flair model outside the function
|
| 9 |
+
classifier = TextClassifier.load('en-sentiment')
|
| 10 |
+
|
| 11 |
# Function to perform sentiment analysis using TextBlob model
|
| 12 |
def textblob_sentiment(text):
|
| 13 |
blob = TextBlob(text)
|
|
|
|
| 21 |
|
| 22 |
# Function to perform sentiment analysis using Flair model
|
| 23 |
def flair_sentiment(text):
|
|
|
|
| 24 |
sentence = Sentence(text)
|
| 25 |
classifier.predict(sentence)
|
| 26 |
if sentence.labels[0].value == 'POSITIVE':
|
|
|
|
| 52 |
ax.axhline(y=0, color='gray', linestyle='--')
|
| 53 |
ax.set_title('Sentiment Scores')
|
| 54 |
st.pyplot(fig)
|
| 55 |
+
plt.close(fig) # Close the figure to prevent memory leaks
|