File size: 3,862 Bytes
c7569af
0977ebb
f47947c
 
b54a3b2
c72d7d4
0cfa117
130fa7f
 
 
 
 
 
c72d7d4
a9f1b96
472c950
ab3acff
130fa7f
 
 
 
472c950
ab3acff
c72d7d4
 
 
130fa7f
c72d7d4
 
a9f1b96
472c950
130fa7f
c72d7d4
130fa7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664290f
130fa7f
 
 
 
 
c72d7d4
130fa7f
 
 
 
472c950
130fa7f
a10305f
130fa7f
 
 
 
 
 
c7569af
b54a3b2
472c950
f9d9aab
ab3acff
472c950
f9d9aab
472c950
 
 
 
 
ab3acff
 
 
 
 
 
 
a9f1b96
ab3acff
 
 
 
a9f1b96
 
 
 
 
 
c7569af
b54a3b2
5896011
 
a9f1b96
b54a3b2
c7569af
b54a3b2
6fb8772
472c950
457b896
472c950
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
from collections import Counter
from bs4 import BeautifulSoup


def extract_titles_and_hashtags(file):
    try:
        # قراءة محتوى الملف بطريقة أكثر كفاءة
        if hasattr(file, 'read'):
            content = file.read()
        else:
            with open(file.name, 'r', encoding='utf-8') as f:
                content = f.read()
    except Exception as e:
        return f"خطأ أثناء قراءة الملف: {str(e)}", "", ""

    # تحليل HTML باستخدام BeautifulSoup
    try:
        soup = BeautifulSoup(content, 'html.parser')
    except Exception as e:
        return f"خطأ في تحليل محتوى HTML: {str(e)}", "", ""

    # استخراج البيانات
    data = []
    hashtags_counter = Counter()

    # البحث عن الحاويات
    desc_containers = soup.find_all('div', class_="css-vi46v1-DivDesContainer")
    if not desc_containers:
        return "لم يتم العثور على أي بيانات مطابقة.", "", ""

    # معالجة كل حاوية
    for container in desc_containers:
        title = container.get('aria-label', 'بدون عنوان').strip()
        hashtags = []
        
        # استخراج الهاشتاغات
        for tag in container.find_all('a'):
            tag_text = tag.get_text(strip=True)
            if tag_text.startswith('#'):
                hashtags.append(tag_text)
        
        if hashtags:
            hashtags_counter.update(hashtags)
            data.append({
                "Title": title,
                "Hashtags": ", ".join(hashtags)
            })

    # إعداد النصوص النهائية
    titles_text = "\n".join(
        f"{i+1}. {row['Title']}" 
        for i, row in enumerate(data)
    )

    hashtags_text = "\n".join(
        f"{hashtag}: {count}" 
        for hashtag, count in sorted(hashtags_counter.items(), key=lambda x: (-x[1], x[0]))
    )

    unique_hashtags_text = "\n".join(sorted(hashtags_counter.keys()))

    # إرجاع النتائج مع رسائل افتراضية
    return (
        titles_text or "لا توجد عناوين مستخرجة.",
        hashtags_text or "لا توجد هاشتاغات مستخرجة.",
        unique_hashtags_text or "لا توجد هاشتاغات فريدة."
    )
# إنشاء واجهة Gradio
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("## 📝 محلل النصوص المتقدم")

        with gr.Row():
            file_input = gr.File(label="📂 رفع ملف TXT", file_types=[".txt"])

        with gr.Row():
            analyze_btn = gr.Button("تحليل البيانات", variant="primary")

        with gr.Row():
            titles_output = gr.Textbox(
                label="📜 العناوين المستخرجة",
                lines=10,
                interactive=False,
                placeholder="ستظهر العناوين هنا"
            )
            hashtags_output = gr.Textbox(
                label="🏷️ الهاشتاغات المستخرجة (مع التكرار)",
                lines=10,
                interactive=False,
                placeholder="ستظهر الهاشتاغات هنا"
            )
            unique_hashtags_output = gr.Textbox(
                label="🏷️ الهاشتاغات الفريدة (غير المكررة)",
                lines=10,
                interactive=False,
                placeholder="ستظهر الهاشتاغات الفريدة هنا"
            )

        analyze_btn.click(
            fn=extract_titles_and_hashtags,
            inputs=[file_input],
            outputs=[titles_output, hashtags_output, unique_hashtags_output],
        )

    return demo


# تشغيل التطبيق
if __name__ == "__main__":
    demo = gradio_interface()
    demo.launch()