Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import DistilBertTokenizer, TFDistilBertModel
|
3 |
import numpy as np
|
4 |
import tensorflow as tf
|
5 |
-
from
|
6 |
import pandas as pd
|
7 |
|
8 |
# Load your data
|
@@ -16,50 +15,8 @@ labels = df["label"]
|
|
16 |
distil_bert_tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
|
17 |
distil_bert_model = TFDistilBertModel.from_pretrained('distilbert-base-uncased')
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
input_ids = []
|
22 |
-
attention_masks = []
|
23 |
-
|
24 |
-
for sent in sentences:
|
25 |
-
distil_bert_input_shape = distil_bert_tokenizer.encode_plus(sent, add_special_tokens=True, max_length=max_len, pad_to_max_length=True, return_attention_mask=True, truncation=True)
|
26 |
-
input_ids.append(distil_bert_input_shape['input_ids'])
|
27 |
-
attention_masks.append(distil_bert_input_shape['attention_mask'])
|
28 |
-
|
29 |
-
input_ids = np.array(input_ids)
|
30 |
-
attention_masks = np.array(attention_masks)
|
31 |
-
|
32 |
-
# Split data into train and test sets
|
33 |
-
X_train_input, X_test_input, Y_train_label, Y_test_label, train_mask, test_mask = train_test_split(input_ids, labels, attention_masks, test_size=0.3, random_state=42, shuffle=True)
|
34 |
-
|
35 |
-
# Define model architecture
|
36 |
-
def create_model():
|
37 |
-
input_shape = tf.keras.Input(shape=(max_len,), dtype='int32')
|
38 |
-
masks = tf.keras.Input(shape=(max_len,), dtype='int32')
|
39 |
-
distil_bert_layer = distil_bert_model(input_ids=input_shape, attention_mask=masks)[0]
|
40 |
-
X = tf.keras.layers.GRU(128, return_sequences=True)(distil_bert_layer)
|
41 |
-
X = tf.keras.layers.GlobalMaxPool1D()(X)
|
42 |
-
X = tf.keras.layers.Dense(64, activation="tanh")(X)
|
43 |
-
X = tf.keras.layers.Dense(32)(X)
|
44 |
-
X = tf.keras.layers.Dense(16)(X)
|
45 |
-
X = tf.keras.layers.Dense(8)(X)
|
46 |
-
X = tf.keras.layers.Dense(4)(X)
|
47 |
-
X = tf.keras.layers.Dropout(0.5)(X)
|
48 |
-
X = tf.keras.layers.Dense(2, activation='softmax')(X)
|
49 |
-
model = tf.keras.Model(inputs=[input_shape, masks], outputs=X)
|
50 |
-
return model
|
51 |
-
|
52 |
-
# Create model instance
|
53 |
-
model = create_model()
|
54 |
-
|
55 |
-
# Compile model
|
56 |
-
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
|
57 |
-
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
|
58 |
-
optimizer = tf.keras.optimizers.Adam(learning_rate=2e-5)
|
59 |
-
model.compile(loss=loss, optimizer=optimizer, metrics=[metric])
|
60 |
-
|
61 |
-
# Train model
|
62 |
-
history = model.fit([X_train_input, train_mask], Y_train_label, batch_size=32, epochs=2, validation_data=([X_test_input, test_mask], Y_test_label))
|
63 |
|
64 |
# Define Gradio interface
|
65 |
def classify_bangla_fake_news(description):
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
+
from transformers import DistilBertTokenizer, TFDistilBertModel
|
5 |
import pandas as pd
|
6 |
|
7 |
# Load your data
|
|
|
15 |
distil_bert_tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
|
16 |
distil_bert_model = TFDistilBertModel.from_pretrained('distilbert-base-uncased')
|
17 |
|
18 |
+
# Load the saved model
|
19 |
+
model = tf.keras.models.load_model("bangla_fake.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Define Gradio interface
|
22 |
def classify_bangla_fake_news(description):
|