KIMOSSINO commited on
Commit
0977ebb
·
verified ·
1 Parent(s): b9ff5d1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from bs4 import BeautifulSoup
3
+ import pandas as pd
4
+ from collections import Counter
5
+
6
+ # معالجة البيانات
7
+ def process_file(file):
8
+ # قراءة محتوى الملف
9
+ content = file.read().decode('utf-8')
10
+
11
+ # تحليل HTML باستخدام BeautifulSoup
12
+ soup = BeautifulSoup(content, 'html.parser')
13
+
14
+ # استخراج البيانات
15
+ data = []
16
+ hashtags_counter = Counter()
17
+ for desc_container in soup.find_all('div', {'data-e2e': 'user-post-item-desc'}):
18
+ # استخراج العنوان
19
+ title = desc_container.get('aria-label', '').strip()
20
+
21
+ # استخراج الهاشتاغات
22
+ hashtags = [tag.get_text().strip() for tag in desc_container.find_all('a') if tag.get_text().startswith('#')]
23
+ hashtags_counter.update(hashtags)
24
+
25
+ # إضافة البيانات للجدول
26
+ data.append({"Title": title, "Hashtags": ", ".join(hashtags)})
27
+
28
+ # تحويل النتائج إلى DataFrame
29
+ df = pd.DataFrame(data)
30
+
31
+ # حساب عدد مرات تكرار كل هاشتاغ
32
+ hashtags_df = pd.DataFrame(hashtags_counter.items(), columns=["Hashtag", "Count"]).sort_values(by="Count", ascending=False)
33
+
34
+ return df, hashtags_df
35
+
36
+ # واجهة Gradio
37
+ def gradio_interface(file):
38
+ titles_df, hashtags_df = process_file(file)
39
+
40
+ # تحويل النتائج إلى HTML للعرض
41
+ titles_html = titles_df.to_html(index=False)
42
+ hashtags_html = hashtags_df.to_html(index=False)
43
+
44
+ return titles_html, hashtags_html
45
+
46
+ # إنشاء واجهة Gradio
47
+ interface = gr.Interface(
48
+ fn=gradio_interface,
49
+ inputs=gr.File(label="ارفع ملف HTML"),
50
+ outputs=[
51
+ gr.HTML(label="العناوين والهاشتاغات المستخرجة"),
52
+ gr.HTML(label="الهاشتاغات مع عدد مرات تكرارها")
53
+ ],
54
+ title="استخراج العناوين والهاشتاغات",
55
+ description="ارفع ملف HTML لاستخراج العناوين والهاشتاغات مع عدد مرات تكرار كل هاشتاغ."
56
+ )
57
+
58
+ # تشغيل التطبيق
59
+ interface.launch()