Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
import tensorflow as tf
|
|
|
|
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
6 |
-
import pickle
|
7 |
-
|
8 |
-
# -------- تحميل النموذج --------
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
model = tf.keras.models.load_model(model_path)
|
13 |
|
14 |
-
# تحميل tokenizer
|
15 |
-
tokenizer_path = hf_hub_download(
|
16 |
-
|
|
|
|
|
|
|
17 |
tokenizer = pickle.load(f)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
# -------- دالة التنبؤ --------
|
26 |
def predict_sentiment(text):
|
27 |
-
|
28 |
-
padded = pad_sequences(
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
"Positive": float(prediction)
|
36 |
-
}
|
37 |
-
|
38 |
-
return result
|
39 |
-
|
40 |
-
# -------- واجهة Gradio --------
|
41 |
interface = gr.Interface(
|
42 |
fn=predict_sentiment,
|
43 |
-
inputs=gr.Textbox(lines=3, placeholder="
|
44 |
-
outputs=
|
45 |
-
title="
|
46 |
-
description="
|
47 |
-
allow_flagging="never"
|
48 |
)
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
import pickle
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
|
|
|
|
|
|
7 |
|
8 |
+
# -------------------------------
|
9 |
+
# تحميل الموديل من Hugging Face
|
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 |
+
# تحميل الـ tokenizer
|
17 |
+
tokenizer_path = hf_hub_download(
|
18 |
+
repo_id="ahmedyoussef1/Asentement_analysis_rnn",
|
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 |
+
seq = tokenizer.texts_to_sequences([text])
|
31 |
+
padded = pad_sequences(seq, maxlen=MAX_LEN, padding='post', truncating='post')
|
32 |
+
prediction = model.predict(padded)[0][0]
|
33 |
+
sentiment = "Positive (1)" if prediction >= 0.5 else "Negative (0)"
|
34 |
+
return f"Sentiment: {sentiment} (Confidence: {prediction:.2f})"
|
35 |
+
|
36 |
+
# -------------------------------
|
37 |
+
# واجهة Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
interface = gr.Interface(
|
39 |
fn=predict_sentiment,
|
40 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a sentence..."),
|
41 |
+
outputs="text",
|
42 |
+
title="Sentiment Analysis with Bi-RNN",
|
43 |
+
description="Enter a sentence and the model will predict its sentiment (Positive or Negative)."
|
|
|
44 |
)
|
45 |
|
46 |
+
# -------------------------------
|
47 |
+
# تشغيل التطبيق
|
48 |
+
if __name__ == "__main__":
|
49 |
+
interface.launch()
|