File size: 3,465 Bytes
59ea8c7
 
 
 
b157a0a
 
c2c974b
59ea8c7
 
 
 
b157a0a
c2c974b
b157a0a
0746ad2
 
bd80957
59ea8c7
 
 
 
c2c974b
59ea8c7
c2c974b
59ea8c7
 
bd80957
59ea8c7
 
 
bd80957
59ea8c7
bd80957
3cebf36
bd80957
c2c974b
3cebf36
 
 
 
c2c974b
 
3cebf36
9e2c014
bd80957
9e2c014
3cebf36
 
c2c974b
 
 
 
 
3cebf36
 
bd80957
3cebf36
c2c974b
 
 
 
 
 
 
 
3cebf36
 
 
c2c974b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1ad665
c2c974b
 
 
3cebf36
7eb75aa
 
 
 
bd80957
7eb75aa
3cebf36
 
bd80957
c2c974b
 
 
 
 
 
bd80957
3cebf36
c2c974b
bd80957
b157a0a
59ea8c7
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import gradio as gr
import numpy as np
import torch
from transformers import BertTokenizer, AutoModel
import tensorflow as tf

# Load tokenizer and BERT model for embeddings extraction
model_name = "aubmindlab/bert-base-arabertv02"
tokenizer = BertTokenizer.from_pretrained(model_name)
bert_model = AutoModel.from_pretrained(model_name)
bert_model.eval()

# Load your trained RNN model
model = tf.keras.models.load_model("rnn_Bi.h5")
print("✅ Model loaded successfully!")

# Function to extract BERT embedding
def get_bert_embedding(text, max_length=100):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
    with torch.no_grad():
        outputs = bert_model(**inputs)
    # Use CLS token embedding as sentence embedding
    embedding = outputs.last_hidden_state[:, 0, :].numpy()
    embedding = embedding.reshape(1, 1, 768)  # shape (1, 1, 768)
    return embedding

# Real sentiment prediction function using the model
def predict_sentiment(text):
    embedding = get_bert_embedding(text)
    pred = model.predict(embedding)[0][0]
    label = "إيجابي" if pred > 0.5 else "سلبي"
    confidence = pred if pred > 0.5 else 1 - pred
    return label, f"{confidence * 100:.2f}%"

# Custom CSS for soft Arabic interface
custom_css = """
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    direction: rtl;
    text-align: right;
    background-color: #f5f7fa;
    color: #202123;
}
.gradio-container h2, .gradio-container p {
    color: #000000 !important;
}
.gradio-container {
    max-width: 600px;
    margin: 40px auto;
    background: #ffffff;
    padding: 25px 35px;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(32, 33, 35, 0.1);
}
.gr-button {
    background-color: #4caf50 !important;
    color: white !important;
    font-weight: 600;
    border-radius: 12px !important;
    padding: 12px 30px !important;
    font-size: 18px !important;
    transition: background-color 0.3s ease;
}
.gr-button:hover {
    background-color: #45a049 !important;
}
.gr-textbox textarea {
    font-size: 18px !important;
    padding: 14px !important;
    border: 1.5px solid #d1d9e6 !important;
    border-radius: 12px !important;
    background-color: #f9fbfd !important;
    color: #202123 !important;
    transition: border-color 0.3s ease;
}
.gr-textbox textarea:focus {
    border-color: #4caf50 !important;
    outline: none;
}
.gr-label {
    font-size: 20px !important;
    font-weight: 600 !important;
    margin-bottom: 8px !important;
}
.gr-textbox label,
.gr-label label {
    color: #8B0000 !important;
}
.gr-textbox input[type="text"] {
    background-color: #f9fbfd !important;
}
.gr-label .label-value,
.gr-label .label-item,
.gr-label .label,
.gr-label span {
    color: #000000 !important;
}
"""

# Build Gradio interface
with gr.Blocks(css=custom_css) as iface:
    gr.Markdown("## تحليل المشاعر العربية بالذكاء الاصطناعي")
    gr.Markdown("اكتب جملة لتحليل المشاعر (إيجابي أو سلبي)")

    input_text = gr.Textbox(lines=2, placeholder="اكتب الجملة هنا...")
    sentiment_label = gr.Label(num_top_classes=2, label="المشاعر")
    confidence_score = gr.Textbox(label="نسبة الثقة")

    btn = gr.Button("تحليل")
    btn.click(fn=predict_sentiment, inputs=input_text, outputs=[sentiment_label, confidence_score])

if __name__ == "__main__":
    iface.launch()