Murali2003 commited on
Commit
ddb0a11
·
verified ·
1 Parent(s): 667f179

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import FSMTForConditionalGeneration, FSMTTokenizer
2
+ from transformers import AutoModelForSequenceClassification
3
+ from lxml_html_clean import Cleaner
4
+
5
+
6
+ from transformers import AutoTokenizer
7
+ from langdetect import detect
8
+ from newspaper import Article
9
+ from PIL import Image
10
+ import streamlit as st
11
+
12
+ import requests
13
+ import torch
14
+
15
+ st.markdown("## Prediction of Misinformation by given URL")
16
+ background = Image.open('logo.jpg')
17
+ st.image(background)
18
+
19
+ st.markdown(f"### Article URL")
20
+ text = st.text_area("Insert some url here",
21
+ value="https://www.livelaw.in/news-updates/supreme-court-collegium-recommends-appointment-advocate-praveen-kumar-giri-judge-allahabad-high-court-279470")
22
+
23
+ # @st.cache(allow_output_mutation=True)
24
+ # def get_models_and_tokenizers():
25
+ # model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
26
+ # model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
27
+ # model.eval()
28
+ # tokenizer = AutoTokenizer.from_pretrained(model_name)
29
+ # model.load_state_dict(torch.load('./my_saved_model/checkpoint-6320/rng_state.pth', map_location='cpu'))
30
+
31
+ # model_name_translator = "facebook/wmt19-ru-en"
32
+ # tokenizer_translator = FSMTTokenizer.from_pretrained(model_name_translator)
33
+ # model_translator = FSMTForConditionalGeneration.from_pretrained(model_name_translator)
34
+ # model_translator.eval()
35
+ # return model, tokenizer, model_translator, tokenizer_translator
36
+ @st.cache_data()
37
+ def get_models_and_tokenizers():
38
+ model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
39
+ checkpoint_dir = './my_saved_model/checkpoint-6320/' # Path to your checkpoint folder
40
+
41
+ # Load the classification model and tokenizer
42
+ model = AutoModelForSequenceClassification.from_pretrained(checkpoint_dir, num_labels=2)
43
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
44
+
45
+ # Load the translator model and tokenizer
46
+ model_name_translator = "facebook/wmt19-ru-en"
47
+ tokenizer_translator = FSMTTokenizer.from_pretrained(model_name_translator)
48
+ model_translator = FSMTForConditionalGeneration.from_pretrained(model_name_translator)
49
+
50
+ model.eval()
51
+ model_translator.eval()
52
+ return model, tokenizer, model_translator, tokenizer_translator
53
+
54
+ model, tokenizer, model_translator, tokenizer_translator = get_models_and_tokenizers()
55
+
56
+ article = Article(text)
57
+ article.download()
58
+ article.parse()
59
+ concated_text = article.title + '. ' + article.text
60
+ lang = detect(concated_text)
61
+
62
+ st.markdown(f"### Language detection")
63
+
64
+ if lang == 'ru':
65
+ st.markdown(f"The language of this article is {lang.upper()} so we translated it!")
66
+ with st.spinner('Waiting for translation'):
67
+ input_ids = tokenizer_translator.encode(concated_text,
68
+ return_tensors="pt", max_length=512, truncation=True)
69
+ outputs = model_translator.generate(input_ids)
70
+ decoded = tokenizer_translator.decode(outputs[0], skip_special_tokens=True)
71
+ st.markdown("### Translated Text")
72
+ st.markdown(f"{decoded[:777]}")
73
+ concated_text = decoded
74
+ else:
75
+ st.markdown(f"The language of this article for sure: {lang.upper()}!")
76
+
77
+ st.markdown("### Extracted Text")
78
+ st.markdown(f"{concated_text[:777]}")
79
+
80
+ tokens_info = tokenizer(concated_text, truncation=True, return_tensors="pt")
81
+ with torch.no_grad():
82
+ raw_predictions = model(**tokens_info)
83
+ softmaxed = int(torch.nn.functional.softmax(raw_predictions.logits[0], dim=0)[1] * 100)
84
+ st.markdown("### Truthteller Predicts..")
85
+ st.progress(softmaxed)
86
+ st.markdown(f"This is fake by *{softmaxed}%*!")
87
+ if (softmaxed > 70):
88
+ st.error('We would not trust this text! This is misleading..')
89
+ elif (softmaxed > 40):
90
+ st.warning('We are not sure about this text!')
91
+ else:
92
+ st.success('We would trust this text!')