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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -31
app.py CHANGED
@@ -1,44 +1,34 @@
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()
 
 
 
 
 
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}")