README / app.py
AbdelRahman02's picture
Upload 6 files
baa0e10 verified
Raw History Blame
3.11 kB
import streamlit as st
import tensorflow as tf
from transformers import AutoTokenizer, TFT5ForConditionalGeneration
from datasets import load_dataset
from pyarabic import araby
import os
# Load Tokenizer and Model
model_name = "google/byt5-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = TFT5ForConditionalGeneration.from_pretrained(model_name)
# Define custom classification head
class BYT5ForClassification(tf.keras.Model):
def __init__(self, base_model, num_labels):
super(BYT5ForClassification, self).__init__()
self.base_model = base_model
self.classifier = tf.keras.layers.Dense(num_labels, activation='softmax')
def call(self, inputs):
# Use the encoder part of the BYT5 model only
encoder_outputs = self.base_model.encoder(
input_ids=inputs['input_ids'],
attention_mask=inputs['attention_mask']
)
# Get the output for the [CLS] token (first token in the sequence)
last_hidden_state = encoder_outputs[0]
cls_token_output = last_hidden_state[:, 0, :]
# Pass through classifier
logits = self.classifier(cls_token_output)
return logits
# Initialize the model with the classification head
num_labels = 14
model = BYT5ForClassification(base_model=model, num_labels=num_labels)
# Label Mapping (from Hugging Face dataset or local file)
label_file = os.path.join('./final_baits', 'labels.txt')
with open(label_file, 'r') as f:
label2name = [line.strip() for line in f.readlines()]
# Preprocessing function for Arabic text
def preprocess_text(text):
text = araby.strip_tashkeel(text) # Strip Tashkeel
excluded_chars = '!()*-ـ.:=o[]«»;؛,،~?؟\u200f\ufeffـ' # Define excluded characters
cleaned_text = ''.join([char for char in text if char not in excluded_chars])
return cleaned_text.strip()
# Tokenization for BYT5
def tokenize_input(texts):
return tokenizer(texts, truncation=True, padding=True, max_length=512, return_tensors='tf')
# Model output processing for classification
def classify_with_byt5(sentence):
sentence = preprocess_text(sentence) # Preprocess the sentence
inputs = tokenize_input([sentence]) # Tokenize input
# Get the model's output
logits = model(inputs)
# Apply softmax to logits to get probabilities
probabilities = tf.nn.softmax(logits, axis=-1)
# Convert probabilities to predicted label index
predicted_label = tf.argmax(probabilities, axis=-1).numpy()[0]
# Debugging output
st.write(f"Logits: {logits}")
st.write(f"Predicted Label Index: {predicted_label}")
st.write(f"Number of labels: {num_labels}")
# Ensure the predicted label is within the range of the label2name list
if predicted_label < 0 or predicted_label >= num_labels:
st.error("Predicted label is out of range.")
return
st.write(f"Predicted label: {label2name[predicted_label]}")
# Example usage in Streamlit
st.title("Arabic Poetry Classifier")
text_input = st.text_input("Enter Arabic Poetry:")
if text_input:
classify_with_byt5(text_input)