Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
import time | |
# تحميل النموذج | |
classifier = pipeline("zero-shot-classification", model="cross-encoder/nli-distilroberta-base") | |
# عنوان التطبيق | |
st.title("Text Classification App") | |
# إدخال الملف النصي | |
uploaded_file = st.file_uploader("Upload a text file containing keywords", type=["txt"]) | |
if uploaded_file is not None: | |
# قراءة الملف النصي | |
content = uploaded_file.read().decode("utf-8") | |
keywords = [line.strip() for line in content.splitlines() if line.strip()] | |
# تحديد الفئات | |
categories = ["shopping", "gaming", "streaming"] | |
# قوائم لتخزين الكلمات حسب الفئة | |
shopping_words = [] | |
gaming_words = [] | |
streaming_words = [] | |
# متغيرات للتحكم في العملية | |
progress_bar = st.progress(0) | |
pause_button = st.button("Pause") | |
stop_button = st.button("Stop") | |
paused = False | |
stopped = False | |
# دالة تصنيف الكلمات | |
def classify_keywords(keywords, categories): | |
nonlocal paused, stopped | |
total_keywords = len(keywords) | |
for i, word in enumerate(keywords): | |
if stopped: | |
break | |
if paused: | |
time.sleep(0.5) # توقف مؤقت عند الضغط على Pause | |
continue | |
# تصنيف الكلمة | |
result = classifier(word, categories) | |
best_category = result['labels'][0] | |
# إضافة الكلمة إلى القائمة المناسبة | |
if best_category == "shopping": | |
shopping_words.append(word) | |
elif best_category == "gaming": | |
gaming_words.append(word) | |
elif best_category == "streaming": | |
streaming_words.append(word) | |
# تحديث شريط التقدم | |
progress = (i + 1) / total_keywords | |
progress_bar.progress(progress) | |
# تحديث النتائج في الوقت الحقيقي | |
update_results() | |
# إبطاء العملية قليلاً للسماح بتحديث الواجهة | |
time.sleep(0.1) | |
# دالة تحديث النتائج | |
def update_results(): | |
st.header("Shopping Keywords") | |
st.text_area("Copy the shopping keywords here:", value="\n".join(shopping_words), height=200, key="shopping") | |
st.header("Gaming Keywords") | |
st.text_area("Copy the gaming keywords here:", value="\n".join(gaming_words), height=200, key="gaming") | |
st.header("Streaming Keywords") | |
st.text_area("Copy the streaming keywords here:", value="\n".join(streaming_words), height=200, key="streaming") | |
# زر البدء | |
if st.button("Start"): | |
stopped = False | |
paused = False | |
classify_keywords(keywords, categories) | |
# زر الإيقاف المؤقت | |
if pause_button: | |
paused = not paused | |
if paused: | |
st.write("Classification paused.") | |
else: | |
st.write("Classification resumed.") | |
# زر التوقف الكامل | |
if stop_button: | |
stopped = True | |
st.write("Classification stopped.") | |
else: | |
st.warning("Please upload a text file to classify the keywords.") |