Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
bchaoss authored Dec 20, 2023
1 parent c97b574 commit 093fff1
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import gradio as gr
import pandas as pd
import nltk
import pickle
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import load_model

def cleaning_text(stop_words, tokenizer, title, text):
text = title + text
text.lower().replace('[^A-Za-z0-9\s]', '')

text = " ".join([word for word in text.split() if word not in stop_words])
df_text = pd.DataFrame({'new': text}, index=[0])

sequence = tokenizer.texts_to_sequences(df_text['new'])
print(len(sequence))
padded_sequence = pad_sequences(sequence, maxlen=600, padding='post', truncating='post')

return padded_sequence

nltk.download('stopwords')
from nltk.corpus import stopwords
stop_words = stopwords.words('english')

# load Tokenizer
with open('tokenizer.pkl', 'rb') as f:
tokenizer = pickle.load(f)

# load model
model_lstm = load_model('model_lstm_1.0.h5')

def detecter(title, text, stop_words, tokenizer, model):
seq = cleaning_text(stop_words, tokenizer, title=title, text=text)
pred = model.predict(seq)[0, 0]
if pred > 0.5:
return "True!"
elif pred <= 0.5:
return "False!"
else:
return "Please try again."

title = gr.Textbox(label="Input the news title")
text = gr.Textbox(label="Input the news full content")

stop_words = stop_words
tokenizer = tokenizer
model = model_lstm

imf = gr.Interface(fn=lambda input1, input2: detecter(input1, input2, stop_words, tokenizer, model), inputs=[title, text], outputs="text")

if __name__ == "__main__":
imf.launch()

0 comments on commit 093fff1

Please sign in to comment.