ahmedyoussef1 commited on
Commit
5373f57
·
verified ·
1 Parent(s): 7a55072

Update app.py

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