import streamlit as st from transformers import T5Tokenizer, T5ForConditionalGeneration import torch # تعيين سمات CSS مخصصة لتصميم أنثوي متجاوب st.markdown(""" """, unsafe_allow_html=True) # تحميل النموذج والـ Tokenizer model_path = "./saved_model" tokenizer_path = "./saved_tokenizer" try: tokenizer = T5Tokenizer.from_pretrained( tokenizer_path, local_files_only=True ) model = T5ForConditionalGeneration.from_pretrained( model_path, local_files_only=True, ignore_mismatched_sizes=True ) device = torch.device("cpu") # تحديد الجهاز على CPU model.to(device) # نقل النموذج إلى CPU model_loaded = True except Exception as e: st.error(f"Error loading model: {e}") model_loaded = False # دالة توليد الملخص def generate_summary(text): try: inputs = ["summarize: " + text] inputs = tokenizer(inputs, max_length=1024, truncation=True, return_tensors="pt").to(device) outputs = model.generate( inputs.input_ids, max_length=150, length_penalty=2.0, num_beams=4, early_stopping=True ) return tokenizer.decode(outputs[0], skip_special_tokens=True) except Exception as e: st.error(f"Error generating summary: {e}") return None # واجهة المستخدم باستخدام Streamlit st.markdown('
', unsafe_allow_html=True) st.title("✨ تطبيق التلخيص الذكي ✨") # إضافة صورة زخرفية (اختياري) st.markdown("""
""", unsafe_allow_html=True) text = st.text_area("أدخل النص الذي تريد تلخيصه...", height=200) col1, col2, col3 = st.columns([1, 2, 1]) with col2: if st.button("✨ قم بالتلخيص ✨"): if text and model_loaded: with st.spinner("جاري إنشاء الملخص... 💫"): summary = generate_summary(text) if summary: st.markdown('
💕 الملخص 💕
' + summary + '
', unsafe_allow_html=True) else: st.error("❌ فشل إنشاء الملخص. يرجى التحقق من النص المدخل.") elif not model_loaded: st.error("❌ فشل تحميل النموذج. يرجى التحقق من سجلات التطبيق.") else: st.warning("⚠️ يرجى إدخال نص للتلخيص.") # إضافة فوتر جميل st.markdown("""
""", unsafe_allow_html=True)