Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +65 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from io import StringIO
|
| 5 |
+
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# ✅ إعداد التخزين المؤقت للنماذج
|
| 8 |
+
os.environ["HF_HOME"] = "/tmp"
|
| 9 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp"
|
| 10 |
+
|
| 11 |
+
# ---- تحميل نموذج GPT-2 ----
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_gpt2():
|
| 14 |
+
return pipeline("text-generation", model="gpt2", model_kwargs={"cache_dir": "/tmp"})
|
| 15 |
+
|
| 16 |
+
gpt2_pipeline = load_gpt2()
|
| 17 |
+
|
| 18 |
+
# ---- إعداد صفحة Streamlit ----
|
| 19 |
+
st.set_page_config(page_title="رصد الإعلانات المخالفة")
|
| 20 |
+
st.title("📢 نظام رصد الإعلانات المخالفة")
|
| 21 |
+
|
| 22 |
+
st.markdown("""
|
| 23 |
+
### 🧠 هذا النظام يستخدم الذكاء الاصطناعي لرصد الإعلانات المخالفة
|
| 24 |
+
ارفع نص إعلان وسنقوم بتحليله واكتشاف أي محتوى مخالف بناءً على قواعد محددة.
|
| 25 |
+
""")
|
| 26 |
+
|
| 27 |
+
# ---- رفع نص الإعلان ----
|
| 28 |
+
uploaded_text = st.text_area("✍️ أدخل نص الإعلان هنا:", height=150)
|
| 29 |
+
|
| 30 |
+
# ---- قاعدة بيانات كلمات/عبارات مخالفة ----
|
| 31 |
+
offensive_keywords = [
|
| 32 |
+
"مطلوب سعودية", "مظهر لائق", "براتب 1500", "للرجال فقط", "بدون عقد",
|
| 33 |
+
"يفضل سعودية", "شرط العمر", "جنسية معينة", "تحمل ضغط العمل", "الوظيفة للذكور فقط"
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
# ---- تحليل النص ----
|
| 37 |
+
if st.button("🔍 تحليل الإعلان") and uploaded_text:
|
| 38 |
+
violations = [kw for kw in offensive_keywords if kw in uploaded_text]
|
| 39 |
+
|
| 40 |
+
if violations:
|
| 41 |
+
st.error("❌ الإعلان يحتوي على عبارات مخالفة!")
|
| 42 |
+
st.write("**العبارات المكتشفة:**")
|
| 43 |
+
for v in violations:
|
| 44 |
+
st.write(f"- {v}")
|
| 45 |
+
violation_score = min(5 + len(violations) * 20, 95)
|
| 46 |
+
else:
|
| 47 |
+
st.success("✅ الإعلان لا يحتوي على عبارات مخالفة ظاهرة.")
|
| 48 |
+
violation_score = 5
|
| 49 |
+
|
| 50 |
+
st.metric(label="نسبة المخالفة المتوقعة", value=f"{violation_score}%")
|
| 51 |
+
|
| 52 |
+
# ---- نموذج GPT-2 لتوليد توصيات ----
|
| 53 |
+
st.subheader("🧠 توصيات الذكاء الاصطناعي (GPT-2)")
|
| 54 |
|
| 55 |
+
if uploaded_text:
|
| 56 |
+
gpt_prompt = f"""
|
| 57 |
+
You are an AI that checks for violations in job advertisements based on Saudi labor laws.
|
| 58 |
+
Here is the ad:
|
| 59 |
+
"{uploaded_text}"
|
| 60 |
|
| 61 |
+
Please identify any potential violations and give recommendations to fix them.
|
| 62 |
+
Violations:
|
| 63 |
"""
|
| 64 |
|
| 65 |
+
with st.spinner("يتم الآن تحليل الإعلان بواسطة GPT-2..."):
|
| 66 |
+
try:
|
| 67 |
+
result = gpt2_pipeline(gpt_prompt, max_new_tokens=150)[0]["generated_text"]
|
| 68 |
+
st.success("✅ تم تحليل الإعلان")
|
| 69 |
+
st.text_area("📋 مخرجات GPT-2:", result, height=300)
|
| 70 |
+
except Exception as e:
|
| 71 |
+
st.error(f"حدث خطأ أثناء استخدام GPT-2: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|