Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
-
|
4 |
-
import pickle
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
-
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
model_path = hf_hub_download(
|
11 |
-
repo_id="ahmedyoussef1/Asentement_analysis_rnn",
|
12 |
-
filename="rnn_Bi.h5"
|
13 |
-
)
|
14 |
model = tf.keras.models.load_model(model_path)
|
15 |
|
16 |
-
# تحميل
|
17 |
-
|
18 |
-
|
19 |
-
filename="tokenizer.pkl"
|
20 |
-
)
|
21 |
-
with open(tokenizer_path, "rb") as f:
|
22 |
-
tokenizer = pickle.load(f)
|
23 |
-
|
24 |
-
# إعدادات البيانات
|
25 |
-
MAX_LEN = 100 # لازم تكون نفس الطول اللي استخدمته أثناء التدريب
|
26 |
|
27 |
-
#
|
28 |
-
# دالة التنبؤ
|
29 |
def predict_sentiment(text):
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
# -------------------------------
|
37 |
# واجهة Gradio
|
38 |
-
|
39 |
fn=predict_sentiment,
|
40 |
-
inputs=gr.Textbox(lines=3, placeholder="
|
41 |
outputs="text",
|
42 |
title="Sentiment Analysis with Bi-RNN",
|
43 |
-
description="
|
44 |
)
|
45 |
|
46 |
-
# -------------------------------
|
47 |
-
# تشغيل التطبيق
|
48 |
if __name__ == "__main__":
|
49 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
+
from transformers import BertTokenizer
|
|
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
+
import numpy as np
|
6 |
|
7 |
+
# تحميل موديل Bi-RNN من الريبو
|
8 |
+
model_path = hf_hub_download(repo_id="ahmedyoussef1/Asentement_analysis_rnn", filename="rnn_Bi.h5")
|
|
|
|
|
|
|
|
|
9 |
model = tf.keras.models.load_model(model_path)
|
10 |
|
11 |
+
# تحميل التوكنيزر من الريبو (دي مجلد التوكنيزر اللي فيه vocab.txt وغيرها)
|
12 |
+
tokenizer_dir = hf_hub_download(repo_id="ahmedyoussef1/Asentement_analysis_rnn", filename="vocab.txt").rsplit('/', 1)[0]
|
13 |
+
tokenizer = BertTokenizer.from_pretrained(tokenizer_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# دالة لتحضير النص و توقع النتيجة
|
|
|
16 |
def predict_sentiment(text):
|
17 |
+
# توكنيز النص
|
18 |
+
tokens = tokenizer.encode_plus(
|
19 |
+
text,
|
20 |
+
max_length=100, # اختار طول مناسب لنصك
|
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 |
# واجهة Gradio
|
35 |
+
iface = gr.Interface(
|
36 |
fn=predict_sentiment,
|
37 |
+
inputs=gr.Textbox(lines=3, placeholder="اكتب نص هنا..."),
|
38 |
outputs="text",
|
39 |
title="Sentiment Analysis with Bi-RNN",
|
40 |
+
description="ادخل جملة لتحليل المشاعر إلى إيجابية أو سلبية"
|
41 |
)
|
42 |
|
|
|
|
|
43 |
if __name__ == "__main__":
|
44 |
+
iface.launch()
|