Spaces:
Configuration error
Configuration error
| import streamlit as st | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import numpy as np | |
| import google.generativeai as genai | |
| import os | |
| # محاولة استيراد TensorFlow | |
| try: | |
| import tensorflow as tf | |
| except ImportError: | |
| st.error("TensorFlow missing! Please ensure 'tensorflow-cpu' is in requirements.txt") | |
| # --- 1. إعدادات الصفحة والتصميم --- | |
| st.set_page_config( | |
| page_title="LexGuard AI | خبير الامتثال", | |
| page_icon="⚖️", | |
| layout="wide" | |
| ) | |
| # ستايل احترافي (أسود وذهبي) | |
| st.markdown(""" | |
| <style> | |
| .main { background-color: #0e1117; } | |
| .report-card { | |
| background-color: #1a1c24; | |
| padding: 25px; | |
| border-radius: 15px; | |
| border-right: 5px solid #daa520; | |
| margin-bottom: 20px; | |
| color: white; | |
| } | |
| h1, h2, h3 { color: #daa520 !important; } | |
| .stButton>button { | |
| background-color: #b8860b; | |
| color: white; | |
| width: 100%; | |
| border-radius: 10px; | |
| font-weight: bold; | |
| height: 3em; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # --- 2. إعدادات Gemini --- | |
| # سيحاول الكود قراءة المفتاح من Secrets (في Hugging Face) أو استخدام المفتاح المباشر | |
| API_KEY = os.getenv("GEMINI_API_KEY", "AIzaSyC94B8M4NCTG58iQGDs4Ei0R7RsBNHUDJI") | |
| genai.configure(api_key=API_KEY) | |
| gemini_model = genai.GenerativeModel('gemini-1.5-pro') | |
| # --- 3. تحميل الموديل المحلي --- | |
| def load_local_model(): | |
| model_path = 'best_model.h5' | |
| if os.path.exists(model_path): | |
| try: | |
| # تحميل الموديل بدون عمل compile لضمان التوافق | |
| return tf.keras.models.load_model(model_path, compile=False) | |
| except Exception as e: | |
| st.warning(f"⚠️ وجدنا ملف الموديل ولكن فشل تحميله تقنياً: {e}") | |
| return None | |
| return None | |
| nn_model = load_local_model() | |
| # --- 4. وظائف النظام --- | |
| def scrape_policy(url): | |
| """سحب نص سياسة الخصوصية وتنظيفه""" | |
| try: | |
| headers = {'User-Agent': 'Mozilla/5.0'} | |
| res = requests.get(url, headers=headers, timeout=10) | |
| soup = BeautifulSoup(res.text, 'html.parser') | |
| for tag in soup(['script', 'style', 'nav', 'footer', 'header']): | |
| tag.decompose() | |
| text = ' '.join(soup.get_text().split()) | |
| return text[:4500] # نأخذ أول 4500 حرف لضمان عدم تجاوز حدود Gemini | |
| except: | |
| return None | |
| def get_legal_analysis(policy_text, law_context): | |
| """التحليل القانوني عبر Gemini""" | |
| prompt = f""" | |
| أنت مدقق قانوني خبير. قم بتحليل نص سياسة الخصوصية التالي بناءً على {law_context}: | |
| النص: {policy_text} | |
| المطلوب بالعربية: | |
| 1. اذكر بوضوح أهم المخالفات القانونية. | |
| 2. اشرح بلهجة مصرية بسيطة للمواطن العادي ماذا سيحدث لبياناته. | |
| 3. أعطِ درجة امتثال نهائية من 10. | |
| """ | |
| try: | |
| response = gemini_model.generate_content(prompt) | |
| return response.text | |
| except: | |
| return "⚠️ عذراً، حدث خطأ في الاتصال بمحرك التحليل." | |
| # --- 5. واجهة المستخدم --- | |
| st.markdown("<h1 style='text-align: center;'>⚖️ LexGuard AI: Auditor</h1>", unsafe_allow_html=True) | |
| st.markdown("<p style='text-align: center; color: #ccc;'>النظام الهجين للتدقيق في سياسات الخصوصية (Neural Network + Gemini)</p>", unsafe_allow_html=True) | |
| if nn_model: | |
| st.success("✅ تم تفعيل الموديل المدرب (Accuracy: 90%) بنجاح.") | |
| else: | |
| st.info("ℹ️ الموديل المحلي غير متوفر، النظام يعمل حالياً بكامل طاقة Gemini AI.") | |
| url_input = st.text_input("📍 أدخل رابط سياسة الخصوصية المراد فحصها:") | |
| if st.button("🚀 ابدأ التدقيق القانوني"): | |
| if url_input: | |
| with st.spinner("جاري استخراج النص وتحليله قانونياً..."): | |
| policy_content = scrape_policy(url_input) | |
| if policy_content: | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.markdown('<div class="report-card">', unsafe_allow_html=True) | |
| st.header("🇪🇬 القانون المصري") | |
| st.write(get_legal_analysis(policy_content, "قانون حماية البيانات الشخصية المصري رقم 151 لسنة 2020")) | |
| st.markdown('</div>', unsafe_allow_html=True) | |
| with col2: | |
| st.markdown('<div class="report-card">', unsafe_allow_html=True) | |
| st.header("🇪🇺 معايير الـ GDPR") | |
| st.write(get_legal_analysis(policy_content, "اللائحة العامة لحماية البيانات الأوروبية (GDPR)")) | |
| st.markdown('</div>', unsafe_allow_html=True) | |
| else: | |
| st.error("❌ فشلنا في سحب النص. تأكد من أن الرابط يعمل ومتاح للعامة.") | |
| else: | |
| st.warning("⚠️ من فضلك ضع الرابط أولاً.") | |
| st.markdown("<br><hr><p style='text-align: center; color: #555;'>LexGuard AI Team - 2026</p>", unsafe_allow_html=True) |