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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
app.py CHANGED
@@ -1,49 +1,44 @@
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()
 
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()