ahmedyoussef1 commited on
Commit
7a55072
·
verified ·
1 Parent(s): 966f986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -1,34 +1,40 @@
1
- import streamlit as st
2
- from transformers import BertTokenizer
3
  import tensorflow as tf
4
  import numpy as np
 
5
 
6
- # تحميل التوكنيزر من الملفات المرفوعة
7
- tokenizer = BertTokenizer.from_pretrained('./')
8
 
9
- # تحميل النموذج
10
- model = tf.keras.models.load_model('rnn_Bi.h5')
 
 
 
11
 
12
- # دالة تحليل المشاعر
13
  def predict_sentiment(text):
14
- encoded_input = tokenizer.encode_plus(
15
  text,
16
- add_special_tokens=True,
17
- padding='max_length',
18
- max_length=100,
19
  truncation=True,
20
- return_tensors='tf'
 
21
  )
22
- prediction = model.predict({'input_ids': encoded_input['input_ids'], 'attention_mask': encoded_input['attention_mask']})
23
- return 'إيجابية' if np.argmax(prediction) == 1 else 'سلبية'
 
 
 
 
 
24
 
25
- # واجهة التطبيق
26
- st.title("تحليل المشاعر - Sentiment Analysis")
27
- user_input = st.text_input("ادخل جملة لتحليل المشاعر إلى إيجابية أو سلبية")
 
 
 
 
28
 
29
- if st.button("Submit"):
30
- if user_input.strip() == "":
31
- st.warning("من فضلك أدخل نصًا.")
32
- else:
33
- result = predict_sentiment(user_input)
34
- st.success(f"تحليل المشاعر: {result}")
 
1
+ import gradio as gr
 
2
  import tensorflow as tf
3
  import numpy as np
4
+ from transformers import BertTokenizer
5
 
6
+ # حمِّل التوكنيزر من مجلد الملفات المرفوعة (هنا "." هو جذر الـ Space)
7
+ tokenizer = BertTokenizer.from_pretrained(".")
8
 
9
+ # حمِّل النموذج
10
+ model = tf.keras.models.load_model("rnn_Bi.h5")
11
+
12
+ MAX_LEN = 100 # نفس الطول الذي استخدمته في التدريب
13
+ label_map = {0: "Negative", 1: "Positive"}
14
 
 
15
  def predict_sentiment(text):
16
+ enc = tokenizer(
17
  text,
18
+ padding="max_length",
 
 
19
  truncation=True,
20
+ max_length=MAX_LEN,
21
+ return_tensors="tf"
22
  )
23
+ # إن كان النموذج يأخذ input_ids فقط
24
+ preds = model.predict(enc["input_ids"])[0][0]
25
+
26
+ # لو مخرَج الموديل Sigmoid (قيمة بين 0‑1)
27
+ label = int(round(float(preds)))
28
+ conf = preds if label == 1 else 1 - preds
29
+ return f"{label_map[label]} (confidence ≈ {conf:.2%})"
30
 
31
+ demo = gr.Interface(
32
+ fn=predict_sentiment,
33
+ inputs=gr.Textbox(lines=3, placeholder="اكتب جملة هنا…"),
34
+ outputs="text",
35
+ title="تحليل المشاعر (Bi‑RNN)",
36
+ description="يدعم إيجابي / سلبي فقط"
37
+ )
38
 
39
+ if __name__ == "__main__":
40
+ demo.launch()