David commited on
Commit
963d538
·
1 Parent(s): a75bd41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -1,13 +1,20 @@
 
1
  import gradio as gr
2
- from transformers import pipeline
3
  from pysentimiento.preprocessing import preprocess_tweet
4
 
5
- pl = pipeline("ner", tokenizer="cardiffnlp/twitter-roberta-base", model="Recognai/veganuary_ner", aggregation_strategy="first")
 
 
 
6
 
7
  def ner(text):
8
  text = preprocess_tweet(text)
9
- predictions = pl(text)
10
- return "\n".join([pred["word"] for pred in predictions if pred["entity_group"] == "LABEL_1"])
 
 
 
11
 
12
  iface = gr.Interface(
13
  ner,
 
1
+ import spacy
2
  import gradio as gr
3
+ from transformers import pipeline, AutoTokenizer
4
  from pysentimiento.preprocessing import preprocess_tweet
5
 
6
+ nlp = spacy.load("en_core_web_sm")
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base", add_prefix_space=True, model_max_length=512)
9
+ pl = pipeline("ner", tokenizer=tokenizer, model="Recognai/veganuary_ner", aggregation_strategy="first")
10
 
11
  def ner(text):
12
  text = preprocess_tweet(text)
13
+ doc = nlp(text)
14
+ text = " ".join([token.text for token in doc])
15
+ predictions = pl(text)
16
+ mentions = [pred["word"].strip() for pred in predictions if pred["entity_group"] == "LABEL_1"]
17
+ return "\n".join(mentions)
18
 
19
  iface = gr.Interface(
20
  ner,