paranitik commited on
Commit
e922fe3
1 Parent(s): 42a4a6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -70
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
- 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', map_location='cpu'))
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
- st.markdown(f"The language of this article is {lang.upper()} so we translated it!")
45
- with st.spinner('Waiting for translation'):
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!')
 
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!')