AboutKeywords / app.py
Mohzen321's picture
Update app.py
1646c3f verified
raw
history blame
8.99 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": []
}
# متغيرات للتحكم في العملية
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
# دالة تصنيف العناصر (للكلمات المفتاحية)
def classify_keywords(items, categories, start_index=0):
global paused, stopped, current_index
total_items = len(items)
for i, item in enumerate(items[start_index:], start=start_index):
current_index = i
if stopped:
break
if paused:
time.sleep(0.5)
continue
# تصنيف الكلمة باستخدام zero-shot-classification
result = classifier(item, categories)
best_category = result['labels'][0]
score = result['scores'][0]
if best_category == "shop" and score > 0.5:
shopping_items.append(item)
elif best_category == "game" and score > 0.5:
gaming_items.append(item)
elif best_category == "stream" and score > 0.5:
streaming_items.append(item)
else:
unknown_items.append(item)
# تحديث شريط التقدم
progress = (current_index + 1) / 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, url in enumerate(items[start_index:], start=start_index):
current_index = i
if stopped:
break
if paused:
time.sleep(0.5)
continue
# استخراج الباراميترات من الرابط باستخدام 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(f"{param}={re.search(param + r'=([^&]*)', url).group(1)}")
elif best_category == "game" and score > 0.5:
param_categories["game_params"].append(f"{param}={re.search(param + r'=([^&]*)', url).group(1)}")
elif best_category == "stream" and score > 0.5:
param_categories["stream_params"].append(f"{param}={re.search(param + r'=([^&]*)', url).group(1)}")
else:
param_categories["unknown_params"].append(f"{param}={re.search(param + r'=([^&]*)', url).group(1)}")
# تحديث شريط التقدم
progress = (current_index + 1) / 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(param_categories["shop_params"])
st.session_state.game_params = "\n".join(param_categories["game_params"])
st.session_state.stream_params = "\n".join(param_categories["stream_params"])
st.session_state.unknown_params = "\n".join(param_categories["unknown_params"])
# زر البدء
if st.button("Start"):
stopped = False
paused = False
current_index = 0
if operation == "Filter Keywords":
classify_keywords(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(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")
else:
st.warning("Please upload a text file to start classification.")