Andrey Moskalenko commited on
Commit
2981a00
1 Parent(s): 0112e2e

Upload app.py

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