Spaces:
Sleeping
Sleeping
samiNCL
commited on
Commit
·
3173d64
1
Parent(s):
d594d7c
Update code to be 1 task only
Browse files
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
app.py
CHANGED
@@ -1,56 +1,27 @@
|
|
1 |
import pandas as pd
|
2 |
-
import spacy
|
3 |
-
import gradio as gr
|
4 |
-
from nrclex import NRCLex
|
5 |
from transformers import pipeline
|
6 |
-
|
7 |
import io
|
8 |
-
import spacy.cli
|
9 |
-
|
10 |
-
# Download the spaCy model
|
11 |
-
spacy.cli.download("en_core_web_sm")
|
12 |
|
13 |
-
# Initialize
|
14 |
-
|
15 |
-
nlp = spacy.load('en_core_web_sm')
|
16 |
-
rake = Rake()
|
17 |
|
18 |
def process_csv(file):
|
19 |
df = pd.read_csv(io.StringIO(file.decode('utf-8')))
|
20 |
-
emotions = []
|
21 |
sentiments = []
|
22 |
-
entities = []
|
23 |
-
keywords = []
|
24 |
for _, row in df.iterrows():
|
25 |
text = row['Content']
|
26 |
-
|
27 |
-
emotion_scores = nrc_obj.affect_frequencies
|
28 |
-
emotions.append(emotion_scores)
|
29 |
-
sentiment = analyze_emotion(text)
|
30 |
sentiments.append(sentiment)
|
31 |
-
entities.append(analyze_entities(text))
|
32 |
-
keywords.append(extract_keywords(text))
|
33 |
|
34 |
-
df['emotions'] = emotions
|
35 |
df['sentiment'] = sentiments
|
36 |
-
df['entities'] = entities
|
37 |
-
df['keywords'] = keywords
|
38 |
|
39 |
return df.to_csv(index=False)
|
40 |
|
41 |
-
def
|
42 |
-
result =
|
43 |
sentiment = result['label']
|
44 |
return sentiment
|
45 |
|
46 |
-
def analyze_entities(text):
|
47 |
-
doc = nlp(text)
|
48 |
-
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
49 |
-
return entities
|
50 |
-
|
51 |
-
def extract_keywords(text):
|
52 |
-
rake.extract_keywords_from_text(text)
|
53 |
-
return rake.get_ranked_phrases()
|
54 |
-
|
55 |
iface = gr.Interface(fn=process_csv, inputs=gr.inputs.Textbox(lines=13, label="Paste CSV Here"), outputs="text")
|
56 |
iface.launch()
|
|
|
1 |
import pandas as pd
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
import io
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Initialize sentiment analysis pipeline
|
7 |
+
sentiment_pipeline = pipeline('sentiment-analysis')
|
|
|
|
|
8 |
|
9 |
def process_csv(file):
|
10 |
df = pd.read_csv(io.StringIO(file.decode('utf-8')))
|
|
|
11 |
sentiments = []
|
|
|
|
|
12 |
for _, row in df.iterrows():
|
13 |
text = row['Content']
|
14 |
+
sentiment = analyze_sentiment(text)
|
|
|
|
|
|
|
15 |
sentiments.append(sentiment)
|
|
|
|
|
16 |
|
|
|
17 |
df['sentiment'] = sentiments
|
|
|
|
|
18 |
|
19 |
return df.to_csv(index=False)
|
20 |
|
21 |
+
def analyze_sentiment(text):
|
22 |
+
result = sentiment_pipeline(text)[0]
|
23 |
sentiment = result['label']
|
24 |
return sentiment
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
iface = gr.Interface(fn=process_csv, inputs=gr.inputs.Textbox(lines=13, label="Paste CSV Here"), outputs="text")
|
27 |
iface.launch()
|