ahmedyoussef1 commited on
Commit
bd80957
·
verified ·
1 Parent(s): f624b5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -25
app.py CHANGED
@@ -14,6 +14,7 @@ bert_model.eval()
14
  model = tf.keras.models.load_model("rnn_Bi.h5")
15
  print("✅ Model loaded successfully!")
16
 
 
17
  def get_bert_embedding(text, max_length=100):
18
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
19
  with torch.no_grad():
@@ -23,16 +24,15 @@ def get_bert_embedding(text, max_length=100):
23
  embedding = embedding.reshape(1, 1, 768) # shape (1, 1, 768)
24
  return embedding
25
 
 
26
  def predict_sentiment(text):
27
  embedding = get_bert_embedding(text)
28
  pred = model.predict(embedding)[0][0]
29
- label = "Positive" if pred > 0.5 else "Negative"
30
  confidence = pred if pred > 0.5 else 1 - pred
31
- return label, f"Confidence: {confidence:.2f}"
32
 
33
- import gradio as gr
34
-
35
- # Custom CSS for soft and pleasant colors like GPT style
36
  custom_css = """
37
  body {
38
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
@@ -42,7 +42,7 @@ body {
42
  color: #202123;
43
  }
44
  .gradio-container h2, .gradio-container p {
45
- color: #000000 !important; /* لجعل عنوان ووصف بلون أسود */
46
  }
47
  .gradio-container {
48
  max-width: 600px;
@@ -53,7 +53,7 @@ body {
53
  box-shadow: 0 8px 24px rgba(32, 33, 35, 0.1);
54
  }
55
  .gr-button {
56
- background-color: #4caf50 !important; /* soft green */
57
  color: white !important;
58
  font-weight: 600;
59
  border-radius: 12px !important;
@@ -89,39 +89,25 @@ body {
89
  .gr-textbox input[type="text"] {
90
  background-color: #f9fbfd !important;
91
  }
92
-
93
- /* تعديل لون التنبؤ داخل Label ليكون أسود */
94
  .gr-label .label-value,
95
  .gr-label .label-item,
96
  .gr-label .label,
97
  .gr-label span {
98
- color: #FFFFFF !important;
99
  }
100
  """
101
 
102
- # Example sentiment prediction function
103
- def predict_sentiment(text):
104
- # كودك الحقيقي هيكون هنا
105
- if "جيد" in text or "رائع" in text or "ممتاز" in text:
106
- return "إيجابي", "95%"
107
- else:
108
- return "سلبي", "82%"
109
-
110
- # Build Gradio interface with Blocks to add CSS
111
  with gr.Blocks(css=custom_css) as iface:
112
  gr.Markdown("## تحليل المشاعر العربية بالذكاء الاصطناعي")
113
  gr.Markdown("اكتب جملة لتحليل المشاعر (إيجابي أو سلبي)")
114
 
115
  input_text = gr.Textbox(lines=2, placeholder="اكتب الجملة هنا...")
116
  sentiment_label = gr.Label(num_top_classes=2, label="المشاعر")
117
- confidence_score = gr.Textbox(label="ثقة النموذج")
118
-
119
- def wrapped_predict(text):
120
- return predict_sentiment(text)
121
 
122
  btn = gr.Button("تحليل")
123
- btn.click(fn=wrapped_predict, inputs=input_text, outputs=[sentiment_label, confidence_score])
124
 
125
  if __name__ == "__main__":
126
  iface.launch()
127
-
 
14
  model = tf.keras.models.load_model("rnn_Bi.h5")
15
  print("✅ Model loaded successfully!")
16
 
17
+ # Function to extract BERT embedding
18
  def get_bert_embedding(text, max_length=100):
19
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=max_length)
20
  with torch.no_grad():
 
24
  embedding = embedding.reshape(1, 1, 768) # shape (1, 1, 768)
25
  return embedding
26
 
27
+ # Real sentiment prediction function using the model
28
  def predict_sentiment(text):
29
  embedding = get_bert_embedding(text)
30
  pred = model.predict(embedding)[0][0]
31
+ label = "إيجابي" if pred > 0.5 else "سلبي"
32
  confidence = pred if pred > 0.5 else 1 - pred
33
+ return label, f"{confidence * 100:.2f}%"
34
 
35
+ # Custom CSS for soft Arabic interface
 
 
36
  custom_css = """
37
  body {
38
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
 
42
  color: #202123;
43
  }
44
  .gradio-container h2, .gradio-container p {
45
+ color: #000000 !important;
46
  }
47
  .gradio-container {
48
  max-width: 600px;
 
53
  box-shadow: 0 8px 24px rgba(32, 33, 35, 0.1);
54
  }
55
  .gr-button {
56
+ background-color: #4caf50 !important;
57
  color: white !important;
58
  font-weight: 600;
59
  border-radius: 12px !important;
 
89
  .gr-textbox input[type="text"] {
90
  background-color: #f9fbfd !important;
91
  }
 
 
92
  .gr-label .label-value,
93
  .gr-label .label-item,
94
  .gr-label .label,
95
  .gr-label span {
96
+ color: #000000 !important;
97
  }
98
  """
99
 
100
+ # Build Gradio interface
 
 
 
 
 
 
 
 
101
  with gr.Blocks(css=custom_css) as iface:
102
  gr.Markdown("## تحليل المشاعر العربية بالذكاء الاصطناعي")
103
  gr.Markdown("اكتب جملة لتحليل المشاعر (إيجابي أو سلبي)")
104
 
105
  input_text = gr.Textbox(lines=2, placeholder="اكتب الجملة هنا...")
106
  sentiment_label = gr.Label(num_top_classes=2, label="المشاعر")
107
+ confidence_score = gr.Textbox(label="نسبة الثقة")
 
 
 
108
 
109
  btn = gr.Button("تحليل")
110
+ btn.click(fn=predict_sentiment, inputs=input_text, outputs=[sentiment_label, confidence_score])
111
 
112
  if __name__ == "__main__":
113
  iface.launch()