File size: 3,335 Bytes
ced22e1
 
a00ff89
ced22e1
 
4c32405
ced22e1
 
 
 
95a596a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a00ff89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95a596a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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.")