Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,72 @@
|
|
1 |
-
from transformers import FSMTForConditionalGeneration, FSMTTokenizer
|
2 |
-
from transformers import AutoModelForSequenceClassification
|
3 |
-
from transformers import AutoTokenizer
|
4 |
-
from langdetect import detect
|
5 |
-
from newspaper import Article
|
6 |
-
from PIL import Image
|
7 |
-
import streamlit as st
|
8 |
-
import requests
|
9 |
-
import torch
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
model.
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
model_translator.
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
article
|
37 |
-
article.
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
st.markdown(
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
st.markdown(
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
st.
|
65 |
-
st.
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
71 |
st.success('We would trust this text!')
|
|
|
1 |
+
from transformers import FSMTForConditionalGeneration, FSMTTokenizer
|
2 |
+
from transformers import AutoModelForSequenceClassification
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
from langdetect import detect
|
5 |
+
from newspaper import Article
|
6 |
+
from PIL import Image
|
7 |
+
import streamlit as st
|
8 |
+
import requests
|
9 |
+
import torch
|
10 |
+
|
11 |
+
|
12 |
+
st.markdown("## Prediction of Fakeness by Given URL")
|
13 |
+
background = Image.open('logo.jpg')
|
14 |
+
st.image(background)
|
15 |
+
|
16 |
+
st.markdown(f"### Article URL")
|
17 |
+
text = st.text_area("Insert some url here",
|
18 |
+
value="https://en.globes.co.il/en/article-yandex-looks-to-expand-activities-in-israel-1001406519")
|
19 |
+
|
20 |
+
@st.cache(allow_output_mutation=True)
|
21 |
+
def get_models_and_tokenizers():
|
22 |
+
model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
|
23 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
|
24 |
+
model.eval()
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
26 |
+
model.load_state_dict(torch.load('./model.pth', map_location='cpu'))
|
27 |
+
|
28 |
+
model_name_translator = "facebook/wmt19-ru-en"
|
29 |
+
tokenizer_translator = FSMTTokenizer.from_pretrained(model_name_translator)
|
30 |
+
model_translator = FSMTForConditionalGeneration.from_pretrained(model_name_translator)
|
31 |
+
model_translator.eval()
|
32 |
+
return model, tokenizer, model_translator, tokenizer_translator
|
33 |
+
|
34 |
+
model, tokenizer, model_translator, tokenizer_translator = get_models_and_tokenizers()
|
35 |
+
|
36 |
+
article = Article(text)
|
37 |
+
article.download()
|
38 |
+
article.parse()
|
39 |
+
concated_text = article.title + '. ' + article.text
|
40 |
+
lang = detect(concated_text)
|
41 |
+
|
42 |
+
st.markdown(f"### Language detection")
|
43 |
+
|
44 |
+
if lang == 'ru':
|
45 |
+
st.markdown(f"The language of this article is {lang.upper()} so we translated it!")
|
46 |
+
with st.spinner('Waiting for translation'):
|
47 |
+
input_ids = tokenizer_translator.encode(concated_text,
|
48 |
+
return_tensors="pt", max_length=512, truncation=True)
|
49 |
+
outputs = model_translator.generate(input_ids)
|
50 |
+
decoded = tokenizer_translator.decode(outputs[0], skip_special_tokens=True)
|
51 |
+
st.markdown("### Translated Text")
|
52 |
+
st.markdown(f"{decoded[:777]}")
|
53 |
+
concated_text = decoded
|
54 |
+
else:
|
55 |
+
st.markdown(f"The language of this article for sure: {lang.upper()}!")
|
56 |
+
|
57 |
+
st.markdown("### Extracted Text")
|
58 |
+
st.markdown(f"{concated_text[:777]}")
|
59 |
+
|
60 |
+
tokens_info = tokenizer(concated_text, truncation=True, return_tensors="pt")
|
61 |
+
with torch.no_grad():
|
62 |
+
raw_predictions = model(**tokens_info)
|
63 |
+
softmaxed = int(torch.nn.functional.softmax(raw_predictions.logits[0], dim=0)[1] * 100)
|
64 |
+
st.markdown("### Fakeness Prediction")
|
65 |
+
st.progress(softmaxed)
|
66 |
+
st.markdown(f"This is fake by **{softmaxed}%**!")
|
67 |
+
if (softmaxed > 70):
|
68 |
+
st.error('We would not trust this text!')
|
69 |
+
elif (softmaxed > 40):
|
70 |
+
st.warning('We are not sure about this text!')
|
71 |
+
else:
|
72 |
st.success('We would trust this text!')
|