Spaces:
No application file
No application file
Update fakenewsdetection/app.py
Browse files- fakenewsdetection/app.py +32 -4
fakenewsdetection/app.py
CHANGED
@@ -30,8 +30,35 @@ nltk.download('punkt')
|
|
30 |
nltk.download('stopwords')
|
31 |
from fastapi import FastAPI, Request
|
32 |
import pickle
|
33 |
-
model = pickle.load(open("
|
34 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def predict(text):
|
36 |
text=pd.DataFrame([text], columns=["text"])
|
37 |
text=text["text"]
|
@@ -103,7 +130,8 @@ def predict(text):
|
|
103 |
|
104 |
demo = gr.Interface(
|
105 |
fn=predict,
|
106 |
-
inputs=["
|
107 |
-
outputs=["
|
108 |
)
|
109 |
-
|
|
|
|
30 |
nltk.download('stopwords')
|
31 |
from fastapi import FastAPI, Request
|
32 |
import pickle
|
33 |
+
model = pickle.load(open("/content/fakenewsdetection/fakenews.sav", 'rb'))
|
34 |
import gradio as gr
|
35 |
+
def wordpre(text):
|
36 |
+
"""
|
37 |
+
Hàm wordpre có tác dụng chuyển tất cả kí tự trong văn bản thành chữ thường, xoá bỏ các kí tự dấu, đường link, dấu câu, dấu cách, xuống dòng,...
|
38 |
+
Những kí tự này không có ý nghĩa đáng kể trong việc phân loại tính chất của câu, việc loại bỏ chúng giúp tập trung vào các từ mang ý nghĩa
|
39 |
+
quan trọng, giúp đánh giá câu chính xác hơn, ngoài ra còn giảm kích thước từ điển và cải thiện hiệu suất của mô hình.
|
40 |
+
"""
|
41 |
+
text = text.lower()
|
42 |
+
text = re.sub('\[.*?\]', '', text)
|
43 |
+
text = re.sub("\\W"," ",text) # remove special chars
|
44 |
+
text = re.sub('https?://\S+|www\.\S+', '', text)
|
45 |
+
text = re.sub('<.*?>+', '', text)
|
46 |
+
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
|
47 |
+
text = re.sub('\n', '', text)
|
48 |
+
text = re.sub('\w*\d\w*', '', text)
|
49 |
+
return text
|
50 |
+
def lower_and_tokenize(data):
|
51 |
+
"""
|
52 |
+
nltk.word_tokenize: tách một đoạn văn bản thành các từ riêng biệt.(token hóa) Việc token hóa giúp
|
53 |
+
"""
|
54 |
+
# Lowercasing and tokenization
|
55 |
+
data=data.str.lower()
|
56 |
+
data=data.apply(nltk.word_tokenize)
|
57 |
+
# Remove stopwords
|
58 |
+
stop_words= set(stopwords.words('english'))
|
59 |
+
data=data.apply(lambda x: [word for word in x if word not in stop_words])
|
60 |
+
return data
|
61 |
+
|
62 |
def predict(text):
|
63 |
text=pd.DataFrame([text], columns=["text"])
|
64 |
text=text["text"]
|
|
|
130 |
|
131 |
demo = gr.Interface(
|
132 |
fn=predict,
|
133 |
+
inputs=[gr.Textbox(label="Text", lines=3)],
|
134 |
+
outputs=[gr.Textbox(label="Predict", lines=1)],
|
135 |
)
|
136 |
+
if __name__ == "__main__":
|
137 |
+
demo.launch(share=True)
|