Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,34 @@
|
|
1 |
-
import
|
2 |
-
import tensorflow as tf
|
3 |
from transformers import BertTokenizer
|
4 |
-
|
5 |
import numpy as np
|
6 |
|
7 |
-
# تحميل
|
8 |
-
|
9 |
-
model = tf.keras.models.load_model(model_path)
|
10 |
|
11 |
-
# تحميل
|
12 |
-
|
13 |
-
tokenizer = BertTokenizer.from_pretrained(tokenizer_dir)
|
14 |
|
15 |
-
# دالة
|
16 |
def predict_sentiment(text):
|
17 |
-
|
18 |
-
tokens = tokenizer.encode_plus(
|
19 |
text,
|
20 |
-
|
21 |
padding='max_length',
|
|
|
22 |
truncation=True,
|
23 |
return_tensors='tf'
|
24 |
)
|
25 |
-
|
26 |
-
|
27 |
-
prediction = model.predict(tokens['input_ids'])
|
28 |
-
|
29 |
-
# افتراضيا 0 أو 1 (سلبي أو إيجابي)
|
30 |
-
pred_label = int(np.round(prediction[0][0]))
|
31 |
-
label_map = {0: "Negative", 1: "Positive"}
|
32 |
-
return label_map.get(pred_label, "Unknown")
|
33 |
|
34 |
-
# واجهة
|
35 |
-
|
36 |
-
|
37 |
-
inputs=gr.Textbox(lines=3, placeholder="اكتب نص هنا..."),
|
38 |
-
outputs="text",
|
39 |
-
title="Sentiment Analysis with Bi-RNN",
|
40 |
-
description="ادخل جملة لتحليل المشاعر إلى إيجابية أو سلبية"
|
41 |
-
)
|
42 |
|
43 |
-
if
|
44 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
|
|
2 |
from transformers import BertTokenizer
|
3 |
+
import tensorflow as tf
|
4 |
import numpy as np
|
5 |
|
6 |
+
# تحميل التوكنيزر من الملفات المرفوعة
|
7 |
+
tokenizer = BertTokenizer.from_pretrained('./')
|
|
|
8 |
|
9 |
+
# تحميل النموذج
|
10 |
+
model = tf.keras.models.load_model('rnn_Bi.h5')
|
|
|
11 |
|
12 |
+
# دالة تحليل المشاعر
|
13 |
def predict_sentiment(text):
|
14 |
+
encoded_input = tokenizer.encode_plus(
|
|
|
15 |
text,
|
16 |
+
add_special_tokens=True,
|
17 |
padding='max_length',
|
18 |
+
max_length=100,
|
19 |
truncation=True,
|
20 |
return_tensors='tf'
|
21 |
)
|
22 |
+
prediction = model.predict({'input_ids': encoded_input['input_ids'], 'attention_mask': encoded_input['attention_mask']})
|
23 |
+
return 'إيجابية' if np.argmax(prediction) == 1 else 'سلبية'
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# واجهة التطبيق
|
26 |
+
st.title("تحليل المشاعر - Sentiment Analysis")
|
27 |
+
user_input = st.text_input("ادخل جملة لتحليل المشاعر إلى إيجابية أو سلبية")
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
if st.button("Submit"):
|
30 |
+
if user_input.strip() == "":
|
31 |
+
st.warning("من فضلك أدخل نصًا.")
|
32 |
+
else:
|
33 |
+
result = predict_sentiment(user_input)
|
34 |
+
st.success(f"تحليل المشاعر: {result}")
|