-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (39 loc) · 1.86 KB
/
main.py
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
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.models import load_model
print ('AI Simulation + Agent')
print('Predicted Emotion')
# تحميل نموذج معالج النصوص (BERT مثلاً)
module_url = "https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1"
bert_layer = hub.KerasLayer(module_url, trainable=False)
# بيانات تدريب وهمية للتوضيح (بيانات مشاعر بسيطة)
texts = ["I am happy", "I feel sad", "This is great", "I am angry"]
labels = ["positive", "negative", "positive", "negative"]
# تحويل النصوص إلى أرقام باستخدام Tokenizer
tokenizer = tf.keras.preprocessing.text.Tokenizer()
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
padded_sequences = pad_sequences(sequences, padding='post')
# ترميز الأهداف (المشاعر)
label_encoder = LabelEncoder()
encoded_labels = label_encoder.fit_transform(labels)
# بناء نموذج بسيط باستخدام BERT كطبقة
model = tf.keras.Sequential([
bert_layer,
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# تدريب النموذج (مع بيانات وهمية هنا)
model.fit(padded_sequences, np.array(encoded_labels), epochs=5)
# اختبار النموذج
test_text = ["I am feeling excited"]
test_sequence = tokenizer.texts_to_sequences(test_text)
test_padded = pad_sequences(test_sequence, padding='post', maxlen=10)
# التنبؤ بالمشاعر
pred = model.predict(test_padded)
pred_label = label_encoder.inverse_transform([int(pred[0][0] > 0.5)])[0]
print(f"Predicted Emotion: {pred_label}")