Dhom1 commited on
Commit
4843d43
·
verified ·
1 Parent(s): d2dddfc

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +75 -42
src/streamlit_app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import streamlit as st
3
  import pandas as pd
4
- from io import StringIO
5
  from transformers import pipeline
6
  import joblib
7
 
@@ -9,70 +8,106 @@ import joblib
9
  os.environ["HF_HOME"] = "/tmp"
10
  os.environ["TRANSFORMERS_CACHE"] = "/tmp"
11
 
12
- # ---- تحميل نموذج التصنيف العربي ----
13
  @st.cache_resource
14
  def load_classifier():
15
- return joblib.load("src/job_classifier.pkl") # تأكد من المسار الصحيح
16
 
17
- classifier = load_classifier()
18
-
19
- # ---- تحميل نموذج GPT-2 ----
20
  @st.cache_resource
21
  def load_gpt2():
22
  return pipeline("text-generation", model="gpt2", model_kwargs={"cache_dir": "/tmp"})
23
 
 
24
  gpt2_pipeline = load_gpt2()
25
 
26
- # ---- إعداد صفحة Streamlit ----
27
- st.set_page_config(page_title="رصد الإعلانات المخالفة")
28
- st.title("📢 نظام رصد الإعلانات المخالفة")
29
 
 
30
  st.markdown("""
31
- ### 🧠 هذا النظام يستخدم الذكاء الاصطناعي لرصد الإعلانات المخالفة
32
- ارفع نص إعلان وسنقوم بتحليله واكتشاف أي محتوى مخالف بناءً على قواعد محددة.
33
- """)
34
-
35
- # ---- رفع نص الإعلان ----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  uploaded_text = st.text_area("✍️ أدخل نص الإعلان هنا:", height=150)
37
 
38
- # ---- تصنيف الإعلان باستخدام النموذج ----
39
- st.subheader("📊 تصنيف الإعلان باستخدام نموذج التعلم الآلي")
40
 
41
  if uploaded_text:
42
  try:
43
  pred = classifier.predict([uploaded_text])[0]
44
  label = "✅ إعلان سليم" if pred == 1 else "❌ إعلان مخالف"
45
- st.info(f"نتيجة النموذج: {label}")
46
  except Exception as e:
47
- st.error(f"حدث خطأ في التنبؤ بالنموذج: {str(e)}")
48
 
49
- # ---- قاعدة بيانات كلمات/عبارات مخالفة ----
50
  offensive_keywords = [
51
  "مطلوب سعودية", "مظهر لائق", "براتب 1500", "للرجال فقط", "بدون عقد",
52
  "يفضل سعودية", "شرط العمر", "جنسية معينة", "تحمل ضغط العمل", "الوظيفة للذكور فقط"
53
  ]
54
 
55
- # ---- تحليل النص باستخدام الكلمات المفتاحية ----
56
- if st.button("🔍 تحليل الإعلان") and uploaded_text:
57
- violations = [kw for kw in offensive_keywords if kw in uploaded_text]
58
-
59
- if violations:
60
- st.error("❌ الإعلان يحتوي على عبارات مخالفة!")
61
- st.write("**العبارات المكتشفة:**")
62
- for v in violations:
63
- st.write(f"- {v}")
64
- violation_score = min(5 + len(violations) * 20, 95)
65
- else:
66
- st.success("✅ الإعلان لا يحتوي على عبارات مخالفة ظاهرة.")
67
- violation_score = 5
68
 
69
- st.metric(label="نسبة المخالفة المتوقعة", value=f"{violation_score}%")
70
 
71
- # ---- توليد توصيات باستخدام GPT-2 ----
72
- st.subheader("🧠 توصيات الذكاء الاصطناعي (GPT-2)")
73
 
74
  if uploaded_text:
75
- gpt_prompt = f"""
76
  You are an AI that checks for violations in job advertisements based on Saudi labor laws.
77
  Here is the ad:
78
  \"{uploaded_text}\"
@@ -80,11 +115,9 @@ Here is the ad:
80
  Please identify any potential violations and give recommendations to fix them.
81
  Violations:
82
  """
83
-
84
- with st.spinner("يتم الآن تحليل الإعلان بواسطة GPT-2..."):
85
  try:
86
- result = gpt2_pipeline(gpt_prompt, max_new_tokens=150)[0]["generated_text"]
87
- st.success(" تم تحليل الإعلان")
88
- st.text_area("📋 مخرجات GPT-2:", result, height=300)
89
  except Exception as e:
90
- st.error(f"حدث خطأ أثناء استخدام GPT-2: {str(e)}")
 
1
  import os
2
  import streamlit as st
3
  import pandas as pd
 
4
  from transformers import pipeline
5
  import joblib
6
 
 
8
  os.environ["HF_HOME"] = "/tmp"
9
  os.environ["TRANSFORMERS_CACHE"] = "/tmp"
10
 
 
11
  @st.cache_resource
12
  def load_classifier():
13
+ return joblib.load("src/job_classifier.pkl")
14
 
 
 
 
15
  @st.cache_resource
16
  def load_gpt2():
17
  return pipeline("text-generation", model="gpt2", model_kwargs={"cache_dir": "/tmp"})
18
 
19
+ classifier = load_classifier()
20
  gpt2_pipeline = load_gpt2()
21
 
22
+ # إعداد الصفحة
23
+ st.set_page_config(page_title="نظام رصد الإعلانات", layout="wide")
 
24
 
25
+ # ✅ تنسيق CSS مستوحى من الهاكاثون
26
  st.markdown("""
27
+ <style>
28
+ body {
29
+ background-color: #f6f9fc;
30
+ font-family: 'Segoe UI', sans-serif;
31
+ }
32
+ h1, h2, h3 {
33
+ color: #004c97;
34
+ }
35
+ .stButton > button {
36
+ background: linear-gradient(90deg, #007bff, #00b8a9);
37
+ color: white;
38
+ font-weight: 600;
39
+ border-radius: 8px;
40
+ padding: 0.6em 1.4em;
41
+ border: none;
42
+ }
43
+ .stButton > button:hover {
44
+ transform: scale(1.03);
45
+ }
46
+ .stTextArea textarea {
47
+ background-color: #ffffff;
48
+ border-radius: 10px;
49
+ border: 1px solid #d0d7de;
50
+ padding: 12px;
51
+ }
52
+ .metric-container {
53
+ background-color: white;
54
+ padding: 20px;
55
+ border-radius: 12px;
56
+ box-shadow: 0 2px 8px rgba(0,0,0,0.05);
57
+ margin-top: 20px;
58
+ }
59
+ </style>
60
+ """, unsafe_allow_html=True)
61
+
62
+ # ✅ رأس الصفحة
63
+ col1, col2 = st.columns([1, 6])
64
+ with col1:
65
+ st.image("https://www.healthmatrixcorp.com/web/image/website/1/logo/Health%20Matrix?unique=956ad7b", width=120)
66
+ with col2:
67
+ st.markdown("""
68
+ <h1>📢 نظام رصد الإعلانات المخالفة</h1>
69
+ <p style="color:#5BA241; margin-top: -10px;">تحليل ذكي يستند إلى القوانين السعودية</p>
70
+ """, unsafe_allow_html=True)
71
+
72
+ # ✅ إدخال الإعلان
73
  uploaded_text = st.text_area("✍️ أدخل نص الإعلان هنا:", height=150)
74
 
75
+ # نموذج التصنيف
76
+ st.subheader("📊 تصنيف الإعلان")
77
 
78
  if uploaded_text:
79
  try:
80
  pred = classifier.predict([uploaded_text])[0]
81
  label = "✅ إعلان سليم" if pred == 1 else "❌ إعلان مخالف"
82
+ st.markdown(f'<div class="metric-container"><h4>{label}</h4></div>', unsafe_allow_html=True)
83
  except Exception as e:
84
+ st.error(f"حدث خطأ في التنبؤ: {str(e)}")
85
 
86
+ # كلمات مخالفة
87
  offensive_keywords = [
88
  "مطلوب سعودية", "مظهر لائق", "براتب 1500", "للرجال فقط", "بدون عقد",
89
  "يفضل سعودية", "شرط العمر", "جنسية معينة", "تحمل ضغط العمل", "الوظيفة للذكور فقط"
90
  ]
91
 
92
+ if st.button("🔍 تحليل العبارات"):
93
+ if uploaded_text:
94
+ violations = [kw for kw in offensive_keywords if kw in uploaded_text]
95
+ if violations:
96
+ st.error("❌ الإعلان يحتوي على عبارات مخالفة:")
97
+ for v in violations:
98
+ st.markdown(f"<li style='color:#b00020'>{v}</li>", unsafe_allow_html=True)
99
+ violation_score = min(5 + len(violations) * 20, 95)
100
+ else:
101
+ st.success("✅ لا توجد عبا��ات مخالفة.")
102
+ violation_score = 5
 
 
103
 
104
+ st.metric(label="نسبة المخالفة المتوقعة", value=f"{violation_score}%")
105
 
106
+ # توصيات GPT-2
107
+ st.subheader("💡 توصيات الذكاء الاصطناعي")
108
 
109
  if uploaded_text:
110
+ prompt = f"""
111
  You are an AI that checks for violations in job advertisements based on Saudi labor laws.
112
  Here is the ad:
113
  \"{uploaded_text}\"
 
115
  Please identify any potential violations and give recommendations to fix them.
116
  Violations:
117
  """
118
+ with st.spinner("📡 جاري التحليل..."):
 
119
  try:
120
+ result = gpt2_pipeline(prompt, max_new_tokens=150)[0]["generated_text"]
121
+ st.text_area("📋 نتائج GPT-2:", result, height=300)
 
122
  except Exception as e:
123
+ st.error(f"خطأ في توليد النتائج: {str(e)}")