File size: 2,449 Bytes
fc80343
83fd955
fc80343
83fd955
79a930e
83fd955
b6b63c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43de91e
 
83fd955
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

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()