Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
| 5 |
+
import nltk
|
| 6 |
+
|
| 7 |
+
# Download VADER lexicon
|
| 8 |
+
nltk.download('vader_lexicon', quiet=True)
|
| 9 |
+
|
| 10 |
+
def perform_sentiment_analysis(text):
|
| 11 |
+
sia = SentimentIntensityAnalyzer()
|
| 12 |
+
return sia.polarity_scores(text)
|
| 13 |
+
|
| 14 |
+
def categorize_sentiment(compound_score):
|
| 15 |
+
if compound_score >= 0.05:
|
| 16 |
+
return 'Positive'
|
| 17 |
+
elif compound_score <= -0.05:
|
| 18 |
+
return 'Negative'
|
| 19 |
+
else:
|
| 20 |
+
return 'Neutral'
|
| 21 |
+
|
| 22 |
+
def analyze_sentiment(input_text):
|
| 23 |
+
scores = perform_sentiment_analysis(input_text)
|
| 24 |
+
sentiment = categorize_sentiment(scores['compound'])
|
| 25 |
+
return {"Sentiment": sentiment, "Scores": scores}
|
| 26 |
+
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=analyze_sentiment,
|
| 29 |
+
inputs=gr.Textbox(label="Enter text for sentiment analysis"),
|
| 30 |
+
outputs="json",
|
| 31 |
+
title="Sentiment Analysis Tool using Reddit Data",
|
| 32 |
+
description="Analyze sentiment of text using Reddit data."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
demo.launch()
|