Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# تحميل النموذج من Hugging Face
|
11 |
+
model_path = hf_hub_download(repo_id="ahmedyoussef1/Asentement_analysis_rnn", filename="rnn_Bi.h5")
|
12 |
+
model = tf.keras.models.load_model(model_path)
|
13 |
+
|
14 |
+
# تحميل tokenizer
|
15 |
+
tokenizer_path = hf_hub_download(repo_id="ahmedyoussef1/sentiment_rnn", filename="tokenizer.pkl")
|
16 |
+
with open(tokenizer_path, 'rb') as f:
|
17 |
+
tokenizer = pickle.load(f)
|
18 |
+
|
19 |
+
# طول التسلسل (من وقت التدريب)
|
20 |
+
max_len = 100
|
21 |
+
|
22 |
+
# أسماء التصنيفات
|
23 |
+
class_names = ['Negative', 'Positive']
|
24 |
+
|
25 |
+
# -------- دالة التنبؤ --------
|
26 |
+
def predict_sentiment(text):
|
27 |
+
sequence = tokenizer.texts_to_sequences([text])
|
28 |
+
padded = pad_sequences(sequence, maxlen=max_len, padding='post')
|
29 |
+
|
30 |
+
prediction = model.predict(padded)[0][0] # لأنه binary: توقع قيمة واحدة بين 0 و 1
|
31 |
+
|
32 |
+
# نسبة الاحتمال
|
33 |
+
result = {
|
34 |
+
"Negative": float(1 - prediction),
|
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=gr.Label(num_top_classes=2),
|
45 |
+
title="تحليل المشاعر باستخدام Bi-RNN",
|
46 |
+
description="ادخل جملة وسيتم تصنيفها كـ مشاعر إيجابية أو سلبية باستخدام نموذج Bi-RNN.",
|
47 |
+
allow_flagging="never"
|
48 |
+
)
|
49 |
+
|
50 |
+
interface.launch()
|