File size: 1,573 Bytes
fc80343
83fd955
fc80343
83fd955
79a930e
83fd955
b6b63c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ce479f
8a510c7
14d439d
 
8a510c7
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

import gradio as gr
import tensorflow as tf 

model = tf.saved_model.load('arabert_pretrained')





from transformers import TFAutoModel, AutoTokenizer
arabert_tokenizer = AutoTokenizer.from_pretrained('aubmindlab/bert-base-arabert')



import pandas as pd



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)
    preds = model(X_input_ids)
    import numpy as np
    predicted_classe=list(np.where(preds <0.5,0,1).reshape(len(preds),1))
    predicted_class = ''.join(str(x) for x in np.where(preds < 0.5, 0, 1).flatten())
    return predicted_class


iface = gr.Interface(fn=sentiment_analysis, inputs="text", outputs="text")
iface.launch()