chinhon commited on
Commit
aad3e6a
1 Parent(s): 7d10b54

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import pandas as pd
4
+ import re
5
+ import shap
6
+
7
+ from transformers import (
8
+ AutoTokenizer,
9
+ AutoModelForSequenceClassification,
10
+ TextClassificationPipeline,
11
+ )
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained("chinhon/fake_tweet_detect")
14
+
15
+ model = AutoModelForSequenceClassification.from_pretrained("chinhon/fake_tweet_detect")
16
+
17
+ tweet_detector = TextClassificationPipeline(model=model, tokenizer=tokenizer)
18
+
19
+ # tweak the extent of text cleaning as you wish
20
+ def clean_text(text):
21
+ text = re.sub(r"http\S+", "", text)
22
+ text = re.sub(r"\n", " ", text)
23
+ text = re.sub(r"\'t", " not", text) # Change 't to 'not'
24
+ text = re.sub(r"(@.*?)[\s]", " ", text) # Remove @name
25
+ text = re.sub(r"$\d+\W+|\b\d+\b|\W+\d+$", " ", text) # remove digits
26
+ text = re.sub(r"[^\w\s\#]", "", text) # remove special characters except hashtags
27
+ text = text.strip(" ")
28
+ text = re.sub(
29
+ " +", " ", text
30
+ ).strip() # get rid of multiple spaces and replace with a single
31
+ return text
32
+
33
+ def tweet_detect(text):
34
+ data = [clean_text(text)]
35
+ prediction = tweet_detector(data)
36
+
37
+ pred_label = [x.get("label") for x in prediction]
38
+
39
+ if pred_label == ["LABEL_1"]:
40
+ return "Fake Tweet"
41
+ elif pred_label == ["LABEL_0"]:
42
+ return "Real Tweet"
43
+
44
+ #Define Gradio interface
45
+ gradio_ui = gr.Interface(
46
+ fn=tweet_detect,
47
+ title="Detect Fake Tweets",
48
+ description="Enter a tweet and see if the transformer model can identify if it was written by state-backed trolls.",
49
+ inputs=gr.inputs.Textbox(lines=10, label="Paste tweet text here [English Only]"),
50
+ outputs=gr.outputs.Label(type="auto", label="Prediction"),
51
+ interpretation="shap",
52
+ enable_queue=True
53
+ )
54
+
55
+ gradio_ui.launch()