Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,13 @@ import torch
|
|
4 |
from transformers import BertTokenizer, AutoModel
|
5 |
import tensorflow as tf
|
6 |
|
|
|
7 |
model_name = "aubmindlab/bert-base-arabertv02"
|
8 |
tokenizer = BertTokenizer.from_pretrained(model_name)
|
9 |
bert_model = AutoModel.from_pretrained(model_name)
|
10 |
bert_model.eval()
|
11 |
|
|
|
12 |
model = tf.keras.models.load_model("rnn_Bi.h5")
|
13 |
print("✅ Model loaded successfully!")
|
14 |
|
@@ -16,59 +18,88 @@ def get_bert_embedding(text, max_length=100):
|
|
16 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
|
17 |
with torch.no_grad():
|
18 |
outputs = bert_model(**inputs)
|
|
|
19 |
embedding = outputs.last_hidden_state[:, 0, :].numpy()
|
20 |
-
embedding = embedding.reshape(1, 1, 768)
|
21 |
return embedding
|
22 |
|
23 |
def predict_sentiment(text):
|
24 |
embedding = get_bert_embedding(text)
|
25 |
pred = model.predict(embedding)[0][0]
|
26 |
-
label = "
|
27 |
confidence = pred if pred > 0.5 else 1 - pred
|
28 |
-
return label, confidence
|
29 |
|
30 |
-
|
|
|
31 |
body {
|
32 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
33 |
direction: rtl;
|
34 |
text-align: right;
|
|
|
|
|
35 |
}
|
36 |
.gradio-container {
|
37 |
max-width: 600px;
|
38 |
-
margin: auto;
|
39 |
-
background: #
|
40 |
-
padding:
|
41 |
-
border-radius:
|
42 |
-
box-shadow: 0
|
43 |
}
|
44 |
.gr-button {
|
45 |
-
background-color: #
|
46 |
color: white !important;
|
47 |
-
font-weight:
|
48 |
-
border-radius:
|
49 |
-
padding:
|
50 |
-
font-size:
|
|
|
|
|
|
|
|
|
51 |
}
|
52 |
.gr-textbox textarea {
|
53 |
font-size: 18px !important;
|
54 |
-
padding:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
}
|
56 |
"""
|
57 |
|
58 |
-
with
|
59 |
-
|
60 |
-
gr.Markdown("
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
-
def
|
67 |
-
|
68 |
-
confidence_percent = f"{confidence*100:.1f}%"
|
69 |
-
return label, confidence_percent
|
70 |
|
71 |
-
|
|
|
72 |
|
73 |
if __name__ == "__main__":
|
74 |
iface.launch()
|
|
|
4 |
from transformers import BertTokenizer, AutoModel
|
5 |
import tensorflow as tf
|
6 |
|
7 |
+
# Load tokenizer and BERT model for embeddings extraction
|
8 |
model_name = "aubmindlab/bert-base-arabertv02"
|
9 |
tokenizer = BertTokenizer.from_pretrained(model_name)
|
10 |
bert_model = AutoModel.from_pretrained(model_name)
|
11 |
bert_model.eval()
|
12 |
|
13 |
+
# Load your trained RNN model
|
14 |
model = tf.keras.models.load_model("rnn_Bi.h5")
|
15 |
print("✅ Model loaded successfully!")
|
16 |
|
|
|
18 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
|
19 |
with torch.no_grad():
|
20 |
outputs = bert_model(**inputs)
|
21 |
+
# Use CLS token embedding as sentence embedding
|
22 |
embedding = outputs.last_hidden_state[:, 0, :].numpy()
|
23 |
+
embedding = embedding.reshape(1, 1, 768) # shape (1, 1, 768)
|
24 |
return embedding
|
25 |
|
26 |
def predict_sentiment(text):
|
27 |
embedding = get_bert_embedding(text)
|
28 |
pred = model.predict(embedding)[0][0]
|
29 |
+
label = "Positive" if pred > 0.5 else "Negative"
|
30 |
confidence = pred if pred > 0.5 else 1 - pred
|
31 |
+
return label, f"Confidence: {confidence:.2f}"
|
32 |
|
33 |
+
# Custom CSS for soft and pleasant colors like GPT style
|
34 |
+
custom_css = """
|
35 |
body {
|
36 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
37 |
direction: rtl;
|
38 |
text-align: right;
|
39 |
+
background-color: #f5f7fa;
|
40 |
+
color: #202123;
|
41 |
}
|
42 |
.gradio-container {
|
43 |
max-width: 600px;
|
44 |
+
margin: 40px auto;
|
45 |
+
background: #ffffff;
|
46 |
+
padding: 25px 35px;
|
47 |
+
border-radius: 16px;
|
48 |
+
box-shadow: 0 8px 24px rgba(32, 33, 35, 0.1);
|
49 |
}
|
50 |
.gr-button {
|
51 |
+
background-color: #4caf50 !important; /* soft green */
|
52 |
color: white !important;
|
53 |
+
font-weight: 600;
|
54 |
+
border-radius: 12px !important;
|
55 |
+
padding: 12px 30px !important;
|
56 |
+
font-size: 18px !important;
|
57 |
+
transition: background-color 0.3s ease;
|
58 |
+
}
|
59 |
+
.gr-button:hover {
|
60 |
+
background-color: #45a049 !important;
|
61 |
}
|
62 |
.gr-textbox textarea {
|
63 |
font-size: 18px !important;
|
64 |
+
padding: 14px !important;
|
65 |
+
border: 1.5px solid #d1d9e6 !important;
|
66 |
+
border-radius: 12px !important;
|
67 |
+
background-color: #f9fbfd !important;
|
68 |
+
color: #202123 !important;
|
69 |
+
transition: border-color 0.3s ease;
|
70 |
+
}
|
71 |
+
.gr-textbox textarea:focus {
|
72 |
+
border-color: #4caf50 !important;
|
73 |
+
outline: none;
|
74 |
+
}
|
75 |
+
.gr-label {
|
76 |
+
font-size: 20px !important;
|
77 |
+
font-weight: 600 !important;
|
78 |
+
margin-bottom: 8px !important;
|
79 |
+
}
|
80 |
+
.gr-textbox label,
|
81 |
+
.gr-label label {
|
82 |
+
color: #202123 !important;
|
83 |
+
}
|
84 |
+
.gr-textbox input[type="text"] {
|
85 |
+
background-color: #f9fbfd !important;
|
86 |
}
|
87 |
"""
|
88 |
|
89 |
+
# Build Gradio interface with Blocks to add CSS
|
90 |
+
with gr.Blocks(css=custom_css) as iface:
|
91 |
+
gr.Markdown("## تحليل المشاعر العربية بالذكاء الاصطناعي")
|
92 |
+
gr.Markdown("اكتب جملة لتحليل المشاعر (إيجابي أو سلبي)")
|
93 |
+
|
94 |
+
input_text = gr.Textbox(lines=2, placeholder="اكتب الجملة هنا...")
|
95 |
+
sentiment_label = gr.Label(num_top_classes=2, label="المشاعر")
|
96 |
+
confidence_score = gr.Textbox(label="ثقة النموذج")
|
97 |
|
98 |
+
def wrapped_predict(text):
|
99 |
+
return predict_sentiment(text)
|
|
|
|
|
100 |
|
101 |
+
btn = gr.Button("تحليل")
|
102 |
+
btn.click(fn=wrapped_predict, inputs=input_text, outputs=[sentiment_label, confidence_score])
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
iface.launch()
|