Ruqiya commited on
Commit
b967c4c
1 Parent(s): 7395e62

Update app.py

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