zao1234 commited on
Commit
d03b8e2
·
verified ·
1 Parent(s): d358e95

Create README.md

Browse files

# News Classifier -- The Evaluation Pipeline
## The colab link: https://colab.research.google.com/drive/1OmIHVN0joIgjGgYCdqLu2EO2By4yT5Xd#scrollTo=MsmKRoHuHyIp
## Ziao You, Samuel Vara, Surya Sandeep Akella
----------------------

## The codes here are the same as the colab link. It shows how to call our model to evaluate the test set. Please use the colab link for easier usage.
##

----------------------

### pip install package
```
!pip install datasets > delete.txt
```

### !!! Load Test Set -- Change the file path of test set
```
import pandas as pd
df_test = pd.read_csv('/content/test_data.csv',index_col="Unnamed: 0")
df_test.head()
```

### Load Model from Hugging Face Hub (Don't change)
```
from huggingface_hub import snapshot_download
import keras

# Download model from hugging face
local_path = snapshot_download(repo_id="HermesPenn/athena_model")

# Load model from local
model = keras.saving.load_model(local_path)
```
### Load Training set (Don't change)
```
from datasets import load_dataset

dataset = load_dataset("HermesPenn/athena_data")
dataset = dataset['train']
data = dataset.to_pandas()
data.head()
```

### Fit_transform label_encoder and tokenizer (Don't change)
```
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
# Data preprocessing
le = LabelEncoder()
data['label'] = le.fit_transform(data['source'])
X = data['title']
y = data['label']

# Tokenize and pad text data
tokenizer = Tokenizer(num_words=20000, oov_token="<OOV>")
tokenizer.fit_on_texts(X)
X_seq = tokenizer.texts_to_sequences(X)
X_padded = pad_sequences(X_seq, maxlen=200, padding='post', truncating='post')
```

### Test set Evaluation (Don't change)

```
from sklearn.metrics import classification_report

X_test = df_test['title']
y_test = df_test['label']


X_test_seq = tokenizer.texts_to_sequences(X_test)
X_test_padded = pad_sequences(X_test_seq, maxlen=200, padding='post', truncating='post')

# Predict the labels using the model
y_pred_probs = model.predict(X_test_padded)
y_pred = (y_pred_probs > 0.5).astype(int)

# Evaluate the model
print("Classification Report:")
print(classification_report(y_test, y_pred))


try:
news_outlets = le.inverse_transform(y_pred.flatten()) # le must be pre-fitted
df_test['Predicted News Outlet'] = news_outlets
except NameError:
df_test['Predicted News Outlet'] = y_pred.flatten()
```

```
# Display test set with predictions
print("\nTest Set with Predictions:")

df_test[['title', 'News Outlet', 'Predicted News Outlet']]
```

Files changed (1) hide show
  1. README.md +0 -15
README.md CHANGED
@@ -1,15 +0,0 @@
1
-
2
- ---
3
- library_name: keras
4
- ---
5
-
6
- This model has been uploaded using the Keras library and can be used with JAX,
7
- TensorFlow, and PyTorch backends.
8
-
9
- This model card has been generated automatically and should be completed by the
10
- model author.
11
- See [Model Cards documentation](https://huggingface.co/docs/hub/model-cards) for
12
- more information.
13
-
14
- For more details about the model architecture, check out
15
- [config.json](./config.json).A plot of the model can be found [here](./assets/summary_plot.png).