samiNCL commited on
Commit
9d910f6
1 Parent(s): f07cc92

new ammendments

Browse files
Files changed (1) hide show
  1. app.py +14 -25
app.py CHANGED
@@ -1,48 +1,39 @@
 
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]
@@ -58,7 +49,5 @@ 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
-
 
1
+ # Sami Alghamdi 21 May 2023
2
  import pandas as pd
3
  import spacy
4
  import gradio as gr
 
5
  from nrclex import NRCLex
6
  from transformers import pipeline
7
  from rake_nltk import Rake
8
+ import io
9
 
10
  # Initialize objects
11
+ emotion_pipeline = pipeline('sentiment-analysis', 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
+ df = pd.read_csv(io.StringIO(file.decode('utf-8')))
17
  emotions = []
18
  sentiments = []
19
  entities = []
20
  keywords = []
21
+ for _, row in df.iterrows():
22
+ text = row['Content'] # Replace 'Content' with the correct column name
 
23
  nrc_obj = NRCLex(text)
24
  emotion_scores = nrc_obj.affect_frequencies
25
  emotions.append(emotion_scores)
26
  sentiment = analyze_emotion(text)
27
  sentiments.append(sentiment)
28
  entities.append(analyze_entities(text))
29
+ keywords.append(extract_keywords(text)) # Extract keywords for each text
 
30
 
31
+ df['emotions'] = emotions
32
+ df['sentiment'] = sentiments
33
+ df['entities'] = entities
34
+ df['keywords'] = keywords
35
+
36
+ return df.to_csv(index=False)
 
 
 
 
 
 
 
37
 
38
  def analyze_emotion(text):
39
  result = emotion_pipeline(text)[0]
 
49
  rake.extract_keywords_from_text(text)
50
  return rake.get_ranked_phrases() # Extract keywords from text
51
 
52
+ iface = gr.Interface(fn=process_csv, inputs=gr.inputs.Textbox(lines=13, label="Paste CSV Here"), outputs="text")
 
53
  iface.launch()