Nathan Luskey commited on
Commit
5fcd3cf
·
1 Parent(s): f9f1955

added NER. Cleaned code

Browse files
Files changed (1) hide show
  1. app.py +76 -9
app.py CHANGED
@@ -1,24 +1,91 @@
 
1
  import gradio as gr
2
- from tweetnlp import Sentiment
 
 
3
 
4
- def classify(tweet):
5
- # return "Hello " + name + "!!"
6
- model_output = model.sentiment(tweet)
7
- # Fill in both positive and negative values
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  if model_output["label"] == "positive":
9
- formatted_output = dict()
10
  formatted_output["positive"] = model_output["probability"]
11
  formatted_output["negative"] = 1 - model_output["probability"]
12
  else:
13
- formatted_output = dict()
14
  formatted_output["negative"] = model_output["probability"]
15
  formatted_output["positive"] = 1 - model_output["probability"]
16
  return formatted_output
17
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  if __name__ == "__main__":
21
  # https://github.com/cardiffnlp/tweetnlp
22
- model = Sentiment()
23
- iface = gr.Interface(fn=classify, inputs="text", outputs="label")
 
 
 
 
 
 
 
24
  iface.launch()
 
1
+ from ast import Str
2
  import gradio as gr
3
+ from tweetnlp import Sentiment, NER
4
+ from typing import Tuple, Dict
5
+ from statistics import mean
6
 
7
+ def clean_tweet(tweet: str, remove_chars: str = "@#") -> str:
8
+ """Remove any unwanted characters
9
+
10
+ Args:
11
+ tweet (str): The raw tweet
12
+ remove_chars (str, optional): The characters to remove. Defaults to "@#".
13
+
14
+ Returns:
15
+ str: The tweet with these characters removed
16
+ """
17
+ for char in remove_chars:
18
+ tweet = tweet.replace(char, "")
19
+ return tweet
20
+
21
+
22
+ def format_sentiment(model_output: Dict) -> Dict:
23
+ """Format the output of the sentiment model
24
+
25
+ Args:
26
+ model_output (Dict): The model output
27
+
28
+ Returns:
29
+ Dict: The format for gradio
30
+ """
31
+ formatted_output = dict()
32
  if model_output["label"] == "positive":
 
33
  formatted_output["positive"] = model_output["probability"]
34
  formatted_output["negative"] = 1 - model_output["probability"]
35
  else:
 
36
  formatted_output["negative"] = model_output["probability"]
37
  formatted_output["positive"] = 1 - model_output["probability"]
38
  return formatted_output
39
 
40
 
41
+ def format_entities(model_output: Dict) -> Dict:
42
+ """Format the output of the NER model
43
+
44
+ Args:
45
+ model_output (Dict): The model output
46
+
47
+ Returns:
48
+ Dict: The format for gradio
49
+ """
50
+ formatted_output = dict()
51
+ for entity in model_output["entity_prediction"]:
52
+ new_output = dict()
53
+ name = " ".join(entity["entity"])
54
+ entity_type = entity["type"]
55
+ new_key = f"{name}:{entity_type}"
56
+ new_value = mean(entity["probability"])
57
+ formatted_output[new_key] = new_value
58
+ return formatted_output
59
+
60
+
61
+ def classify(tweet: str) -> Tuple[Dict, Dict]:
62
+ """Runs models
63
+
64
+ Args:
65
+ tweet (str): The raw tweet
66
+
67
+ Returns:
68
+ Tuple[Dict, Dict]: The formatted_sentiment and formatted_entities of the tweet
69
+ """
70
+ tweet = clean_tweet(tweet)
71
+ # Get sentiment
72
+ model_sentiment = se_model.sentiment(tweet)
73
+ formatted_sentiment = format_sentiment(model_sentiment)
74
+ # Get entities
75
+ entities = ner_model.ner(tweet)
76
+ formatted_entities = format_entities(entities)
77
+ return formatted_sentiment, formatted_entities
78
+
79
 
80
  if __name__ == "__main__":
81
  # https://github.com/cardiffnlp/tweetnlp
82
+ se_model = Sentiment()
83
+ ner_model = NER()
84
+
85
+ # Get a few examples from: https://twitter.com/NFLFantasy
86
+ examples = list()
87
+ examples.append("Dameon Pierce is clearly the #Texans starter and he once again looks good")
88
+ examples.append("Deebo Samuel had 150+ receiving yards in 4 games last year - the most by any receiver in the league.")
89
+
90
+ iface = gr.Interface(fn=classify, inputs="text", outputs=["label", "label"], examples=examples)
91
  iface.launch()