-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (95 loc) · 3.71 KB
/
app.py
File metadata and controls
114 lines (95 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
"""
Created on Wed Sept 26 02:20:31 2022
@authors: Supriya, Akash, Tejaswini, Rukmananda
"""
# -*- coding: utf-8 -*-
"""
Created on Wed Sept 26 02:20:31 2022
@authors: Supriya, Akash, Tejaswini, Rukmananda
"""
from urllib import response
import numpy as np
import pickle
import pandas as pd
import streamlit as st
import requests
import sentence_transformers
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.metrics.pairwise import cosine_similarity
from collections import defaultdict
class InputTransformer(BaseEstimator, TransformerMixin):
def __init__(self, vectorizer):
self.vectorizer = vectorizer
print("initalized InputTransformer")
def fit(self, X, y=None):
print('fit')
return self
def transform(self, X, y=None):
X_ = X.copy()
print('trasform')
transformedX = self.vectorizer.transform(X_)
return transformedX
pickle_in = open("classifier_final.pkl","rb")
classifier=pickle.load(pickle_in)
# from urllib.request import urlopen
# pickle_in_2 = urlopen("https://github.com/2711-bharath/test-upload-large-files-to-gitHub/blob/main/sentence_transformer_model.pkl?raw=true")
pickle_in_2 = open("sentence_transformer_model.pkl","rb")
sentence_transformer = pickle.load(pickle_in_2)
def predict(text):
prediction=classifier.predict([text])
print(prediction)
return prediction
def encode(sentences):
prediction=sentence_transformer.encode(sentences)
print(prediction)
return prediction
def main():
st.title("Fake News Classifier")
text = st.text_area("Text", height=200)
result=""
if st.button("Predict"):
result=predict(text)
temp = requests.get('https://factchecktools.googleapis.com/v1alpha1/claims:search?key=AIzaSyBRuXbKLveIy-0H5LkCaoDLlm8BxFcs44Q&query=' + text)
temp = temp.json()
if len(temp) > 0:
claims_list = []
for x in temp['claims']:
sentences = [
x['text'],
text
]
sentence_embeddings = encode(sentences)
t = cosine_similarity(
[sentence_embeddings[0]],
sentence_embeddings[1:]
)
print(t)
if t > 0.44:
label_dict = defaultdict(lambda: -1)
label_dict['Half True'] = 0
label_dict['Mostly True'] = 0
label_dict['False'] = 1
label_dict['True'] = 0
label_dict['Barely True'] = 1
label_dict['Pants on Fire'] = 1
label_dict['Distorts the Facts'] = 1
label_dict['Partly False'] = 1
claims_list.append({'claimText': x['text'], 'rating': label_dict[x['claimReview'][0]['textualRating']], 'textualRating': x['claimReview'][0]['textualRating']})
if len(claims_list) > 0:
st.subheader('Based on Known Facts')
for claim in claims_list:
result = claim['rating']
if int(result) == 1:
st.error('"' + claim['claimText'] + '" ' + ' - Fake')
elif int(result) == 0:
st.success('"' + claim['claimText'] + '" ' + ' ' + ' - Real')
else:
st.info(claim['claimText'] + ' - ' + claim['textualRating'])
st.subheader('Based on the words used')
if int(result) == 1:
st.error('Our model predicts: The news could be Fake')
else:
st.success('Our model predicts: The news could be Real')
if __name__=='__main__':
main()