AboutKeywords / app.py
Mohzen321's picture
Update app.py
d064c14 verified
raw
history blame
11.1 kB
import streamlit as st
from transformers import pipeline
import re
import time
# تحميل النموذج
classifier = pipeline("zero-shot-classification", model="cross-encoder/nli-distilroberta-base")
# عنوان التطبيق
st.title("Text Classification App")
# اختيار العملية
operation = st.radio("Choose an operation:", ["Filter Keywords", "Extra & Filter Param (URLs)"])
# إدخال الملف النصي
uploaded_file = st.file_uploader("Upload a text file", type=["txt"])
if uploaded_file is not None:
# قراءة الملف النصي
content = uploaded_file.read().decode("utf-8")
items = [line.strip() for line in content.splitlines() if line.strip()]
# تحديد الفئات
categories = ["shop", "game", "stream"]
# قوائم لتخزين النتائج
shopping_items = []
gaming_items = []
streaming_items = []
unknown_items = []
# قوائم خاصة بالباراميترات
param_categories = {
"shop_params": [],
"game_params": [],
"stream_params": [],
"unknown_params": []
}
# قائمة لتحليل الصيغ (Extensions)
extensions = {}
# متغيرات للتحكم في العملية
progress_bar = st.progress(0)
pause_button = st.button("Pause")
stop_button = st.button("Stop")
continue_button = st.button("Continue")
paused = False
stopped = False
current_index = 0 # مؤشر للكلمة الحالية
batch_size = 10 # عدد العناصر التي يتم معالجتها في الدفعة الواحدة
# دالة التحقق من صحة الملف
def validate_file(items, operation):
if operation == "Filter Keywords":
# التحقق من أن الملف يحتوي على كلمات مفتاحية فقط
for item in items:
if re.match(r'https?://', item):
return False, "File should contain keywords only."
elif operation == "Extra & Filter Param (URLs)":
# التحقق من أن الملف يحتوي على روابط فقط
for item in items:
if not re.match(r'https?://', item):
return False, "File should contain URLs only."
return True, ""
# دالة تصنيف الكلمات باستخدام الدفعات
def classify_keywords_batch(items, categories, start_index=0):
global paused, stopped, current_index
total_items = len(items)
for i in range(start_index, total_items, batch_size):
if stopped:
break
if paused:
time.sleep(0.5)
continue
# معالجة دفعة من العناصر
batch = items[i:i + batch_size]
results = classifier(batch, categories)
for j, result in enumerate(results):
best_category = result['labels'][0]
score = result['scores'][0]
if best_category == "shop" and score > 0.5:
shopping_items.append(batch[j])
elif best_category == "game" and score > 0.5:
gaming_items.append(batch[j])
elif best_category == "stream" and score > 0.5:
streaming_items.append(batch[j])
else:
unknown_items.append(batch[j])
# تحديث المؤشر الحالي
current_index = i + batch_size
# تحديث شريط التقدم
progress = (current_index) / total_items
progress_bar.progress(progress)
# تحديث النتائج في الوقت الحقيقي
update_results()
# إبطاء العملية قليلاً للسماح بتحديث الواجهة
time.sleep(0.1)
# دالة تصنيف الباراميترات
def classify_parameters(items, categories, start_index=0):
global paused, stopped, current_index
total_items = len(items)
for i in range(start_index, total_items, batch_size):
if stopped:
break
if paused:
time.sleep(0.5)
continue
# معالجة دفعة من الروابط
batch = items[i:i + batch_size]
for url in batch:
# استخراج الباراميترات من الرابط باستخدام RegEx
params = re.findall(r'(\w+)=\w+', url)
for param in params:
# تصنيف الباراميتر باستخدام zero-shot-classification
result = classifier(param, categories)
best_category = result['labels'][0]
score = result['scores'][0]
if best_category == "shop" and score > 0.5:
param_categories["shop_params"].append(param)
elif best_category == "game" and score > 0.5:
param_categories["game_params"].append(param)
elif best_category == "stream" and score > 0.5:
param_categories["stream_params"].append(param)
else:
param_categories["unknown_params"].append(param)
# استخراج الصيغ (Extensions) من الروابط
match = re.search(r'\.([a-zA-Z0-9]+)$', url)
if match:
ext = match.group(1)
if ext not in extensions:
extensions[ext] = 0
extensions[ext] += 1
# تحديث المؤشر الحالي
current_index = i + batch_size
# تحديث شريط التقدم
progress = (current_index) / total_items
progress_bar.progress(progress)
# تحديث النتائج في الوقت الحقيقي
update_results()
# إبطاء العملية قليلاً للسماح بتحديث الواجهة
time.sleep(0.1)
# دالة تحديث النتائج
def update_results():
# تحديث محتوى المربعات النصية
st.session_state.shopping_text = "\n".join(shopping_items)
st.session_state.gaming_text = "\n".join(gaming_items)
st.session_state.streaming_text = "\n".join(streaming_items)
st.session_state.unknown_text = "\n".join(unknown_items)
# تحديث محتوى المربعات الخاصة بالباراميترات
st.session_state.shop_params = "\n".join(set(param_categories["shop_params"]))
st.session_state.game_params = "\n".join(set(param_categories["game_params"]))
st.session_state.stream_params = "\n".join(set(param_categories["stream_params"]))
st.session_state.unknown_params = "\n".join(set(param_categories["unknown_params"]))
# تحديث محتوى المربع الخاص بالصيغ
st.session_state.extensions_text = "\n".join(extensions.keys())
# زر البدء
if st.button("Start"):
stopped = False
paused = False
current_index = 0
# التحقق من صحة الملف
is_valid, error_message = validate_file(items, operation)
if not is_valid:
st.error(error_message)
else:
if operation == "Filter Keywords":
classify_keywords_batch(items, categories, start_index=current_index)
elif operation == "Extra & Filter Param (URLs)":
classify_parameters(items, categories, start_index=current_index)
# زر الإيقاف المؤقت
if pause_button:
paused = True
st.write("Classification paused.")
# زر الاستمرار
if continue_button and paused:
paused = False
st.write("Classification resumed.")
if operation == "Filter Keywords":
classify_keywords_batch(items, categories, start_index=current_index)
elif operation == "Extra & Filter Param (URLs)":
classify_parameters(items, categories, start_index=current_index)
# زر التوقف الكامل
if stop_button:
stopped = True
st.write("Classification stopped.")
# عرض النتائج بناءً على الخيار المختار
if operation == "Filter Keywords":
# عرض النتائج للكلمات المفتاحية
st.header("Shopping Keywords")
if 'shopping_text' not in st.session_state:
st.session_state.shopping_text = ""
st.text_area("Copy the shopping keywords here:", value=st.session_state.shopping_text, height=200, key="shopping")
st.header("Gaming Keywords")
if 'gaming_text' not in st.session_state:
st.session_state.gaming_text = ""
st.text_area("Copy the gaming keywords here:", value=st.session_state.gaming_text, height=200, key="gaming")
st.header("Streaming Keywords")
if 'streaming_text' not in st.session_state:
st.session_state.streaming_text = ""
st.text_area("Copy the streaming keywords here:", value=st.session_state.streaming_text, height=200, key="streaming")
st.header("Unknown Keywords")
if 'unknown_text' not in st.session_state:
st.session_state.unknown_text = ""
st.text_area("Copy the unknown keywords here:", value=st.session_state.unknown_text, height=200, key="unknown")
elif operation == "Extra & Filter Param (URLs)":
# عرض النتائج للباراميترات
st.header("Shop Parameters")
if 'shop_params' not in st.session_state:
st.session_state.shop_params = ""
st.text_area("Copy the shop parameters here:", value=st.session_state.shop_params, height=200, key="shop_params")
st.header("Game Parameters")
if 'game_params' not in st.session_state:
st.session_state.game_params = ""
st.text_area("Copy the game parameters here:", value=st.session_state.game_params, height=200, key="game_params")
st.header("Stream Parameters")
if 'stream_params' not in st.session_state:
st.session_state.stream_params = ""
st.text_area("Copy the stream parameters here:", value=st.session_state.stream_params, height=200, key="stream_params")
st.header("Unknown Parameters")
if 'unknown_params' not in st.session_state:
st.session_state.unknown_params = ""
st.text_area("Copy the unknown parameters here:", value=st.session_state.unknown_params, height=200, key="unknown_params")
# عرض الصيغ (Extensions)
st.header("File Extensions")
if 'extensions_text' not in st.session_state:
st.session_state.extensions_text = ""
st.text_area("Copy the file extensions here:", value=st.session_state.extensions_text, height=200, key="extensions")
else:
st.warning("Please upload a text file to start classification.")