ahmedyoussef1 commited on
Commit
87ce0ec
·
verified ·
1 Parent(s): d805e6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
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
- # تحميل النموذج من 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()
 
 
 
 
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()