File size: 1,713 Bytes
ee4fcdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 gradio as gr
import numpy as np
import tensorflow as tf
from huggingface_hub import hf_hub_download
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle

# -------- تحميل النموذج --------

# تحميل النموذج من Hugging Face
model_path = hf_hub_download(repo_id="ahmedyoussef1/Asentement_analysis_rnn", filename="rnn_Bi.h5")
model = tf.keras.models.load_model(model_path)

# تحميل tokenizer
tokenizer_path = hf_hub_download(repo_id="ahmedyoussef1/sentiment_rnn", filename="tokenizer.pkl")
with open(tokenizer_path, 'rb') as f:
    tokenizer = pickle.load(f)

# طول التسلسل (من وقت التدريب)
max_len = 100

# أسماء التصنيفات
class_names = ['Negative', 'Positive']

# -------- دالة التنبؤ --------
def predict_sentiment(text):
    sequence = tokenizer.texts_to_sequences([text])
    padded = pad_sequences(sequence, maxlen=max_len, padding='post')
    
    prediction = model.predict(padded)[0][0]  # لأنه binary: توقع قيمة واحدة بين 0 و 1
    
    # نسبة الاحتمال
    result = {
        "Negative": float(1 - prediction),
        "Positive": float(prediction)
    }
    
    return result

# -------- واجهة Gradio --------
interface = gr.Interface(
    fn=predict_sentiment,
    inputs=gr.Textbox(lines=3, placeholder="اكتب جملة لتحليل المشاعر..."),
    outputs=gr.Label(num_top_classes=2),
    title="تحليل المشاعر باستخدام Bi-RNN",
    description="ادخل جملة وسيتم تصنيفها كـ مشاعر إيجابية أو سلبية باستخدام نموذج Bi-RNN.",
    allow_flagging="never"
)

interface.launch()