import gradio as gr import numpy as np import torch from transformers import BertTokenizer, AutoModel import tensorflow as tf # Load tokenizer and BERT model for embeddings extraction model_name = "aubmindlab/bert-base-arabertv02" tokenizer = BertTokenizer.from_pretrained(model_name) bert_model = AutoModel.from_pretrained(model_name) bert_model.eval() # Load your trained RNN model model = tf.keras.models.load_model("rnn_Bi.h5") print("✅ Model loaded successfully!") # Function to extract BERT embedding def get_bert_embedding(text, max_length=100): inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=max_length) with torch.no_grad(): outputs = bert_model(**inputs) # Use CLS token embedding as sentence embedding embedding = outputs.last_hidden_state[:, 0, :].numpy() embedding = embedding.reshape(1, 1, 768) # shape (1, 1, 768) return embedding # Real sentiment prediction function using the model def predict_sentiment(text): embedding = get_bert_embedding(text) pred = model.predict(embedding)[0][0] label = "إيجابي" if pred > 0.5 else "سلبي" confidence = pred if pred > 0.5 else 1 - pred return label, f"{confidence * 100:.2f}%" # Custom CSS for soft Arabic interface custom_css = """ body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; direction: rtl; text-align: right; background-color: #f5f7fa; color: #202123; } .gradio-container h2, .gradio-container p { color: #000000 !important; } .gradio-container { max-width: 600px; margin: 40px auto; background: #ffffff; padding: 25px 35px; border-radius: 16px; box-shadow: 0 8px 24px rgba(32, 33, 35, 0.1); } .gr-button { background-color: #4caf50 !important; color: white !important; font-weight: 600; border-radius: 12px !important; padding: 12px 30px !important; font-size: 18px !important; transition: background-color 0.3s ease; } .gr-button:hover { background-color: #45a049 !important; } .gr-textbox textarea { font-size: 18px !important; padding: 14px !important; border: 1.5px solid #d1d9e6 !important; border-radius: 12px !important; background-color: #f9fbfd !important; color: #202123 !important; transition: border-color 0.3s ease; } .gr-textbox textarea:focus { border-color: #4caf50 !important; outline: none; } .gr-label { font-size: 20px !important; font-weight: 600 !important; margin-bottom: 8px !important; } .gr-textbox label, .gr-label label { color: #8B0000 !important; } .gr-textbox input[type="text"] { background-color: #f9fbfd !important; } .gr-label .label-value, .gr-label .label-item, .gr-label .label, .gr-label span { color: #000000 !important; } """ # Build Gradio interface with gr.Blocks(css=custom_css) as iface: gr.Markdown("## تحليل المشاعر العربية بالذكاء الاصطناعي") gr.Markdown("اكتب جملة لتحليل المشاعر (إيجابي أو سلبي)") input_text = gr.Textbox(lines=2, placeholder="اكتب الجملة هنا...") sentiment_label = gr.Label(num_top_classes=2, label="المشاعر") confidence_score = gr.Textbox(label="نسبة الثقة") btn = gr.Button("تحليل") btn.click(fn=predict_sentiment, inputs=input_text, outputs=[sentiment_label, confidence_score]) if __name__ == "__main__": iface.launch()