import gradio as gr import tensorflow as tf from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.preprocessing.text import tokenizer_from_json import pickle import re import os # 1. تحميل الملفات والنموذج def load_resources(): model = tf.keras.models.load_model('/content/exported_model/complaints_model.keras') with open('/content/exported_model/tokenizer.json', 'r', encoding='utf-8') as f: tokenizer_data = f.read() tokenizer = tokenizer_from_json(tokenizer_data) with open('/content/exported_model/label_encoder.pkl', 'rb') as f: encoder = pickle.load(f) return model, tokenizer, encoder try: model, tokenizer, encoder = load_resources() except Exception as e: print(f"Error loading resources: {e}") # 2. تنظيف ومعالجة النص العربي def normalize(text): text = re.sub(r'[إأآا]', 'ا', text) text = re.sub(r'ى', 'ي', text) text = re.sub(r'ة', 'ه', text) return text def clean_arabic(text): text = str(text) text = re.sub(r'[^\u0621-\u064A\s]', '', text) text = re.sub(r'(.)\1{2,}', r'\1', text) text = normalize(text) return re.sub(r'\s+', ' ', text).strip() # 3. دالة التوقع والتحليل def classify_arabic_text(text): if not text.strip(): return "الرجاء إدخال نص صالح" cleaned = clean_arabic(text) sequence = tokenizer.texts_to_sequences([cleaned]) padded = pad_sequences(sequence, maxlen=30, padding='post', truncating='post') predictions = model.predict(padded)[0] # بناء قاموس بالاحتمالات لكل فئة results = {} for i, class_name in enumerate(encoder.classes_): results[str(class_name)] = float(predictions[i]) return results # 4. بناء واجهة Gradio interface = gr.Interface( fn=classify_arabic_text, inputs=gr.Textbox(lines=5, placeholder="أدخل النص العربي هنا لتبدأ عملية التصنيف...", label="النص المدخل"), outputs=gr.Label(num_top_classes=3, label="التصنيفات المحتملة"), title="تصنيف النصوص العربية (Gradio)", description="تطبيق ذكاء اصطناعي لتصنيف النصوص والشكاوى العربية إلى فئاتها المناسبة باستخدام نموذج شبكة عصبية متكررة (RNN).", theme="soft" ) if __name__ == "__main__": interface.launch(share=True)