Spaces:
Sleeping
Sleeping
samiNCL
commited on
Commit
•
f07cc92
1
Parent(s):
596ec18
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import spacy
|
3 |
+
import gradio as gr
|
4 |
+
import csv
|
5 |
+
from nrclex import NRCLex
|
6 |
+
from transformers import pipeline
|
7 |
+
from rake_nltk import Rake
|
8 |
+
|
9 |
+
# Initialize objects
|
10 |
+
emotion_pipeline = pipeline('sentiment-analysis',
|
11 |
+
model='nlptown/bert-base-multilingual-uncased-sentiment')
|
12 |
+
nlp = spacy.load('en_core_web_sm')
|
13 |
+
rake = Rake()
|
14 |
+
|
15 |
+
def process_csv(file):
|
16 |
+
reader = csv.DictReader(file)
|
17 |
+
emotions = []
|
18 |
+
sentiments = []
|
19 |
+
entities = []
|
20 |
+
keywords = []
|
21 |
+
for row in reader:
|
22 |
+
text = row['Content'] # Replace 'Content' with the correct column
|
23 |
+
name
|
24 |
+
nrc_obj = NRCLex(text)
|
25 |
+
emotion_scores = nrc_obj.affect_frequencies
|
26 |
+
emotions.append(emotion_scores)
|
27 |
+
sentiment = analyze_emotion(text)
|
28 |
+
sentiments.append(sentiment)
|
29 |
+
entities.append(analyze_entities(text))
|
30 |
+
keywords.append(extract_keywords(text)) # Extract keywords for
|
31 |
+
each text
|
32 |
+
|
33 |
+
fieldnames = reader.fieldnames + list(emotions[0].keys()) +
|
34 |
+
['sentiment', 'entities', 'keywords']
|
35 |
+
output = []
|
36 |
+
for row, emotion_scores, sentiment, entity, keyword in zip(reader,
|
37 |
+
emotions, sentiments, entities, keywords):
|
38 |
+
row.update(emotion_scores) # Update the row dictionary with
|
39 |
+
emotion scores
|
40 |
+
row.update({'sentiment': sentiment, 'entities': entity,
|
41 |
+
'keywords': keyword}) # Update the row dictionary with sentiment,
|
42 |
+
entities and keywords
|
43 |
+
output.append({field: row.get(field, '') for field in fieldnames})
|
44 |
+
# Write row with matching fields or empty values
|
45 |
+
return pd.DataFrame(output).to_csv(index=False)
|
46 |
+
|
47 |
+
def analyze_emotion(text):
|
48 |
+
result = emotion_pipeline(text)[0]
|
49 |
+
sentiment = result['label']
|
50 |
+
return sentiment
|
51 |
+
|
52 |
+
def analyze_entities(text):
|
53 |
+
doc = nlp(text)
|
54 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
55 |
+
return entities
|
56 |
+
|
57 |
+
def extract_keywords(text):
|
58 |
+
rake.extract_keywords_from_text(text)
|
59 |
+
return rake.get_ranked_phrases() # Extract keywords from text
|
60 |
+
|
61 |
+
iface = gr.Interface(fn=process_csv, inputs=gr.inputs.File(type='csv'),
|
62 |
+
outputs=gr.outputs.File())
|
63 |
+
iface.launch()
|
64 |
+
|