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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import re
|
4 |
+
import time
|
5 |
+
|
6 |
+
# تحميل النموذج
|
7 |
+
classifier = pipeline("zero-shot-classification", model="cross-encoder/nli-distilroberta-base")
|
8 |
+
|
9 |
+
# عنوان التطبيق
|
10 |
+
st.title("Text Classification App")
|
11 |
+
|
12 |
+
# اختيار العملية
|
13 |
+
operation = st.radio("Choose an operation:", ["Filter Keywords", "Extra & Filter Param (URLs)"])
|
14 |
+
|
15 |
+
# إدخال الملف النصي
|
16 |
+
uploaded_file = st.file_uploader("Upload a text file", type=["txt"])
|
17 |
+
|
18 |
+
if uploaded_file is not None:
|
19 |
+
# قراءة الملف النصي
|
20 |
+
content = uploaded_file.read().decode("utf-8")
|
21 |
+
items = [line.strip() for line in content.splitlines() if line.strip()]
|
22 |
+
|
23 |
+
# تحديد الفئات
|
24 |
+
categories = ["shop", "game", "stream"]
|
25 |
+
|
26 |
+
# قوائم لتخزين النتائج
|
27 |
+
shopping_items = []
|
28 |
+
gaming_items = []
|
29 |
+
streaming_items = []
|
30 |
+
unknown_items = []
|
31 |
+
|
32 |
+
# قوائم خاصة بالباراميترات
|
33 |
+
param_categories = {
|
34 |
+
"shop_params": [],
|
35 |
+
"game_params": [],
|
36 |
+
"stream_params": [],
|
37 |
+
"unknown_params": []
|
38 |
+
}
|
39 |
+
|
40 |
+
# قائمة لتحليل الصيغ (Extensions)
|
41 |
+
extensions = {}
|
42 |
+
|
43 |
+
# قائمة لتحليل أنماط الصفحات الكاملة (Full PageType)
|
44 |
+
full_page_types = []
|
45 |
+
|
46 |
+
# متغيرات للتحكم في العملية
|
47 |
+
progress_bar = st.progress(0)
|
48 |
+
pause_button = st.button("Pause")
|
49 |
+
stop_button = st.button("Stop")
|
50 |
+
continue_button = st.button("Continue")
|
51 |
+
paused = False
|
52 |
+
stopped = False
|
53 |
+
current_index = 0 # مؤشر للكلمة الحالية
|
54 |
+
batch_size = 10 # عدد العناصر التي يتم معالجتها في الدفعة الواحدة
|
55 |
+
|
56 |
+
# دالة تصنيف الكلمات باستخدام الدفعات
|
57 |
+
def classify_keywords_batch(items, categories, start_index=0):
|
58 |
+
global paused, stopped, current_index
|
59 |
+
total_items = len(items)
|
60 |
+
for i in range(start_index, total_items, batch_size):
|
61 |
+
if stopped:
|
62 |
+
break
|
63 |
+
if paused:
|
64 |
+
time.sleep(0.5)
|
65 |
+
continue
|
66 |
+
|
67 |
+
# معالجة دفعة من العناصر
|
68 |
+
batch = items[i:i + batch_size]
|
69 |
+
results = classifier(batch, categories)
|
70 |
+
|
71 |
+
for j, result in enumerate(results):
|
72 |
+
best_category = result['labels'][0]
|
73 |
+
score = result['scores'][0]
|
74 |
+
|
75 |
+
if best_category == "shop" and score > 0.5:
|
76 |
+
shopping_items.append(batch[j])
|
77 |
+
elif best_category == "game" and score > 0.5:
|
78 |
+
gaming_items.append(batch[j])
|
79 |
+
elif best_category == "stream" and score > 0.5:
|
80 |
+
streaming_items.append(batch[j])
|
81 |
+
else:
|
82 |
+
unknown_items.append(batch[j])
|
83 |
+
|
84 |
+
# تحديث المؤشر الحالي
|
85 |
+
current_index = min(i + batch_size, total_items) # تأكد من عدم تجاوز المؤشر للعدد الإجمالي
|
86 |
+
|
87 |
+
# تحديث شريط التقدم
|
88 |
+
progress = min((current_index) / total_items, 1.0) # تأكد من أن قيمة التقدم لا تتجاوز 1.0
|
89 |
+
progress_bar.progress(progress)
|
90 |
+
|
91 |
+
# تحديث النتائج في الوقت الحقيقي
|
92 |
+
update_results()
|
93 |
+
|
94 |
+
# إبطاء العملية قليلاً للسماح بتحديث الواجهة
|
95 |
+
time.sleep(0.1)
|
96 |
+
|
97 |
+
# دالة تصنيف الباراميترات
|
98 |
+
def classify_parameters(items, categories, start_index=0):
|
99 |
+
global paused, stopped, current_index
|
100 |
+
total_items = len(items)
|
101 |
+
for i in range(start_index, total_items, batch_size):
|
102 |
+
if stopped:
|
103 |
+
break
|
104 |
+
if paused:
|
105 |
+
time.sleep(0.5)
|
106 |
+
continue
|
107 |
+
|
108 |
+
# معالجة دفعة من الروابط
|
109 |
+
batch = items[i:i + batch_size]
|
110 |
+
for url in batch:
|
111 |
+
# استخراج الباراميترات من الرابط باستخدام RegEx
|
112 |
+
params = re.findall(r'(\w+)=\w+', url)
|
113 |
+
for param in params:
|
114 |
+
# تصنيف الباراميتر باستخدام zero-shot-classification
|
115 |
+
result = classifier(param, categories)
|
116 |
+
best_category = result['labels'][0]
|
117 |
+
score = result['scores'][0]
|
118 |
+
|
119 |
+
if best_category == "shop" and score > 0.5:
|
120 |
+
param_categories["shop_params"].append(param)
|
121 |
+
elif best_category == "game" and score > 0.5:
|
122 |
+
param_categories["game_params"].append(param)
|
123 |
+
elif best_category == "stream" and score > 0.5:
|
124 |
+
param_categories["stream_params"].append(param)
|
125 |
+
else:
|
126 |
+
param_categories["unknown_params"].append(param)
|
127 |
+
|
128 |
+
# استخراج الصيغ (Extensions) من الروابط
|
129 |
+
match = re.search(r'\.([a-zA-Z0-9]+)$', url)
|
130 |
+
if match:
|
131 |
+
ext = match.group(1)
|
132 |
+
if ext not in extensions:
|
133 |
+
extensions[ext] = 0
|
134 |
+
extensions[ext] += 1
|
135 |
+
|
136 |
+
# استخراج أنماط الصفحات الكاملة (Full PageType)
|
137 |
+
page_type_match = re.search(r'(\w+\.php|\w+\.html)\?', url)
|
138 |
+
if page_type_match:
|
139 |
+
page_type = page_type_match.group(1)
|
140 |
+
if page_type not in full_page_types:
|
141 |
+
full_page_types.append(page_type)
|
142 |
+
|
143 |
+
# تحديث المؤشر الحالي
|
144 |
+
current_index = min(i + batch_size, total_items) # تأكد من عدم تجاوز المؤشر للعدد الإجمالي
|
145 |
+
|
146 |
+
# تحديث شريط التقدم
|
147 |
+
progress = min((current_index) / total_items, 1.0) # تأكد من أن قيمة التقدم لا تتجاوز 1.0
|
148 |
+
progress_bar.progress(progress)
|
149 |
+
|
150 |
+
# تحديث النتائج في الوقت الحقيقي
|
151 |
+
update_results()
|
152 |
+
|
153 |
+
# إبطاء العملية قليلاً للسماح بتحديث الواجهة
|
154 |
+
time.sleep(0.1)
|
155 |
+
|
156 |
+
# دالة تحديث النتائج
|
157 |
+
def update_results():
|
158 |
+
# تحديث محتوى المربعات النصية
|
159 |
+
st.session_state.shopping_text = "\n".join(shopping_items)
|
160 |
+
st.session_state.gaming_text = "\n".join(gaming_items)
|
161 |
+
st.session_state.streaming_text = "\n".join(streaming_items)
|
162 |
+
st.session_state.unknown_text = "\n".join(unknown_items)
|
163 |
+
|
164 |
+
# تحديث محتوى المربعات الخاصة بالباراميترات
|
165 |
+
st.session_state.shop_params = "\n".join(set(param_categories["shop_params"]))
|
166 |
+
st.session_state.game_params = "\n".join(set(param_categories["game_params"]))
|
167 |
+
st.session_state.stream_params = "\n".join(set(param_categories["stream_params"]))
|
168 |
+
st.session_state.unknown_params = "\n".join(set(param_categories["unknown_params"]))
|
169 |
+
|
170 |
+
# تحديث محتوى المربع الخاص بالصيغ
|
171 |
+
st.session_state.extensions_text = "\n".join(extensions.keys())
|
172 |
+
|
173 |
+
# تحديث محتوى المربع الخاص بأنماط الصفحات الكاملة
|
174 |
+
st.session_state.full_page_types = "\n".join(full_page_types)
|
175 |
+
|
176 |
+
# دالة تصدير النتائج
|
177 |
+
def export_results(key, filename):
|
178 |
+
with open(filename, "w") as f:
|
179 |
+
f.write(st.session_state[key])
|
180 |
+
st.success(f"Results exported to {filename}")
|
181 |
+
|
182 |
+
# زر البدء
|
183 |
+
if st.button("Start"):
|
184 |
+
stopped = False
|
185 |
+
paused = False
|
186 |
+
current_index = 0
|
187 |
+
|
188 |
+
if operation == "Filter Keywords":
|
189 |
+
classify_keywords_batch(items, categories, start_index=current_index)
|
190 |
+
elif operation == "Extra & Filter Param (URLs)":
|
191 |
+
classify_parameters(items, categories, start_index=current_index)
|
192 |
+
|
193 |
+
# زر الإيقاف المؤقت
|
194 |
+
if pause_button:
|
195 |
+
paused = True
|
196 |
+
st.write("Classification paused.")
|
197 |
+
|
198 |
+
# زر الاستمرار
|
199 |
+
if continue_button and paused:
|
200 |
+
paused = False
|
201 |
+
st.write("Classification resumed.")
|
202 |
+
if operation == "Filter Keywords":
|
203 |
+
classify_keywords_batch(items, categories, start_index=current_index)
|
204 |
+
elif operation == "Extra & Filter Param (URLs)":
|
205 |
+
classify_parameters(items, categories, start_index=current_index)
|
206 |
+
|
207 |
+
# زر التوقف الكامل
|
208 |
+
if stop_button:
|
209 |
+
stopped = True
|
210 |
+
st.write("Classification stopped.")
|
211 |
+
|
212 |
+
# عرض النتائج بناءً على الخيار المختار
|
213 |
+
if operation == "Filter Keywords":
|
214 |
+
# عرض النتائج للكلمات المفتاحية
|
215 |
+
st.header("Shopping Keywords")
|
216 |
+
if 'shopping_text' not in st.session_state:
|
217 |
+
st.session_state.shopping_text = ""
|
218 |
+
st.text_area("Copy the shopping keywords here:", value=st.session_state.shopping_text, height=200, key="shopping")
|
219 |
+
st.button("Export Shopping Keywords", on_click=export_results, args=("shopping_text", "shopping_keywords.txt"))
|
220 |
+
|
221 |
+
st.header("Gaming Keywords")
|
222 |
+
if 'gaming_text' not in st.session_state:
|
223 |
+
st.session_state.gaming_text = ""
|
224 |
+
st.text_area("Copy the gaming keywords here:", value=st.session_state.gaming_text, height=200, key="gaming")
|
225 |
+
st.button("Export Gaming Keywords", on_click=export_results, args=("gaming_text", "gaming_keywords.txt"))
|
226 |
+
|
227 |
+
st.header("Streaming Keywords")
|
228 |
+
if 'streaming_text' not in st.session_state:
|
229 |
+
st.session_state.streaming_text = ""
|
230 |
+
st.text_area("Copy the streaming keywords here:", value=st.session_state.streaming_text, height=200, key="streaming")
|
231 |
+
st.button("Export Streaming Keywords", on_click=export_results, args=("streaming_text", "streaming_keywords.txt"))
|
232 |
+
|
233 |
+
st.header("Unknown Keywords")
|
234 |
+
if 'unknown_text' not in st.session_state:
|
235 |
+
st.session_state.unknown_text = ""
|
236 |
+
st.text_area("Copy the unknown keywords here:", value=st.session_state.unknown_text, height=200, key="unknown")
|
237 |
+
st.button("Export Unknown Keywords", on_click=export_results, args=("unknown_text", "unknown_keywords.txt"))
|
238 |
+
|
239 |
+
elif operation == "Extra & Filter Param (URLs)":
|
240 |
+
# عرض النتائج للباراميترات
|
241 |
+
st.header("Shop Parameters")
|
242 |
+
if 'shop_params' not in st.session_state:
|
243 |
+
st.session_state.shop_params = ""
|
244 |
+
st.text_area("Copy the shop parameters here:", value=st.session_state.shop_params, height=200, key="shop_params")
|
245 |
+
st.button("Export Shop Parameters", on_click=export_results, args=("shop_params", "shop_params.txt"))
|
246 |
+
|
247 |
+
st.header("Game Parameters")
|
248 |
+
if 'game_params' not in st.session_state:
|
249 |
+
st.session_state.game_params = ""
|
250 |
+
st.text_area("Copy the game parameters here:", value=st.session_state.game_params, height=200, key="game_params")
|
251 |
+
st.button("Export Game Parameters", on_click=export_results, args=("game_params", "game_params.txt"))
|
252 |
+
|
253 |
+
st.header("Stream Parameters")
|
254 |
+
if 'stream_params' not in st.session_state:
|
255 |
+
st.session_state.stream_params = ""
|
256 |
+
st.text_area("Copy the stream parameters here:", value=st.session_state.stream_params, height=200, key="stream_params")
|
257 |
+
st.button("Export Stream Parameters", on_click=export_results, args=("stream_params", "stream_params.txt"))
|
258 |
+
|
259 |
+
st.header("Unknown Parameters")
|
260 |
+
if 'unknown_params' not in st.session_state:
|
261 |
+
st.session_state.unknown_params = ""
|
262 |
+
st.text_area("Copy the unknown parameters here:", value=st.session_state.unknown_params, height=200, key="unknown_params")
|
263 |
+
st.button("Export Unknown Parameters", on_click=export_results, args=("unknown_params", "unknown_params.txt"))
|
264 |
+
|
265 |
+
# عرض الصيغ (Extensions)
|
266 |
+
st.header("File Extensions")
|
267 |
+
if 'extensions_text' not in st.session_state:
|
268 |
+
st.session_state.extensions_text = ""
|
269 |
+
st.text_area("Copy the file extensions here:", value=st.session_state.extensions_text, height=200, key="extensions")
|
270 |
+
st.button("Export File Extensions", on_click=export_results, args=("extensions_text", "file_extensions.txt"))
|
271 |
+
|
272 |
+
# عرض أنماط الصفحات الكاملة (Full PageType)
|
273 |
+
st.header("Full PageType")
|
274 |
+
if 'full_page_types' not in st.session_state:
|
275 |
+
st.session_state.full_page_types = ""
|
276 |
+
st.text_area("Copy the full page types here:", value=st.session_state.full_page_types, height=200, key="full_page_types")
|
277 |
+
st.button("Export Full PageTypes", on_click=export_results, args=("full_page_types", "full_page_types.txt"))
|
278 |
+
|
279 |
+
else:
|
280 |
+
st.warning("Please upload a text file to start classification.")
|