hamdie's picture
Update app.py
b6b63c7 verified
raw
history blame
2.45 kB
import gradio as gr
import tensorflow as tf
model = tf.saved_model.load('arabert_pretrained')
import pandas as pd
df = pd.read_csv('put\data_cleaned1.csv')
from transformers import TFAutoModel, AutoTokenizer
arabert_tokenizer = AutoTokenizer.from_pretrained('aubmindlab/bert-base-arabert')
import pandas as pd
# Assuming your DataFrame is named 'df'
# Split the DataFrame into two parts: label=1 and label=0
label_1_df = df[df['data_labels'] == 1]
label_0_df = df[df['data_labels'] == 0]
# Sample an equal number of rows from each label
sample_size = min(len(label_1_df), len(label_0_df))
sample_label_1 = label_1_df.sample(n=sample_size, random_state=42)
sample_label_0 = label_0_df.sample(n=sample_size, random_state=42)
# Concatenate the two samples to get the final balanced sample
balanced_sample = pd.concat([sample_label_1, sample_label_0])
# Shuffle the rows in the balanced sample
balanced_sample = balanced_sample.sample(frac=1, random_state=42)
balanced_sample.reset_index(inplace=True,drop=True)
from sklearn.model_selection import train_test_split
tweets = balanced_sample['cleaned_text']
labels = balanced_sample['data_labels']
X_train, X_test, y_train, y_test = train_test_split(tweets, labels,stratify=labels, test_size=0.15, random_state=1)
def preprocess_input_data(texts, tokenizer, max_len=120):
"""Tokenize and preprocess the input data for Arabert model.
Args:
texts (list): List of text strings.
tokenizer (AutoTokenizer): Arabert tokenizer from transformers library.
max_len (int, optional): Maximum sequence length. Defaults to 120.
Returns:
Tuple of numpy arrays: Input token IDs and attention masks.
"""
# Tokenize the text data using the tokenizer
tokenized_data = [tokenizer.encode_plus(
t,
max_length=max_len,
pad_to_max_length=True,
add_special_tokens=True) for t in texts]
# Extract tokenized input IDs and attention masks
input_ids = [data['input_ids'] for data in tokenized_data]
attention_mask = [data['attention_mask'] for data in tokenized_data]
return input_ids, attention_mask
def sentiment_analysis(text):
X_input_ids, X_attention_mask = preprocess_input_data(text, arabert_tokenizer)
predictions = modelez(X_input_ids)
a=predictions.numpy()
return a
import gradio as gr
iface = gr.Interface(fn=sentiment_analysis, inputs="text", outputs="text")
iface.launch()