Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,11 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
import pandas as pd
|
| 4 |
from collections import Counter
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
content = file.read().decode('utf-8')
|
| 11 |
-
except Exception as e:
|
| 12 |
-
return f"خطأ أثناء قراءة الملف: {str(e)}", None
|
| 13 |
|
| 14 |
# تحليل HTML باستخدام BeautifulSoup
|
| 15 |
soup = BeautifulSoup(content, 'html.parser')
|
|
@@ -18,62 +14,37 @@ def process_file(file):
|
|
| 18 |
data = []
|
| 19 |
hashtags_counter = Counter()
|
| 20 |
|
| 21 |
-
# العثور على الحاويات ال
|
| 22 |
desc_containers = soup.find_all('div', class_="css-vi46v1-DivDesContainer")
|
| 23 |
-
if not desc_containers:
|
| 24 |
-
return "لم يتم العثور على أي بيانات مطابقة في الملف.", None
|
| 25 |
-
|
| 26 |
for container in desc_containers:
|
| 27 |
# استخراج العنوان
|
| 28 |
-
title_tag = container.find('h1')
|
| 29 |
title = title_tag.get_text(strip=True) if title_tag else "بدون عنوان"
|
| 30 |
|
| 31 |
# استخراج الهاشتاغات
|
| 32 |
hashtags = [
|
| 33 |
tag.get_text(strip=True)
|
| 34 |
-
for tag in container.find_all('a')
|
| 35 |
if tag.get_text(strip=True).startswith('#')
|
| 36 |
]
|
| 37 |
hashtags_counter.update(hashtags)
|
| 38 |
|
| 39 |
-
# إضافة البيانات لل
|
| 40 |
data.append({"Title": title, "Hashtags": ", ".join(hashtags)})
|
| 41 |
|
| 42 |
-
# تحويل الن
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return df, hashtags_df
|
| 47 |
-
|
| 48 |
-
# واجهة Gradio
|
| 49 |
-
def gradio_interface(file):
|
| 50 |
-
result = process_file(file)
|
| 51 |
-
|
| 52 |
-
if isinstance(result, tuple):
|
| 53 |
-
titles_df, hashtags_df = result
|
| 54 |
-
else:
|
| 55 |
-
return result, ""
|
| 56 |
-
|
| 57 |
-
if titles_df is None or hashtags_df is None:
|
| 58 |
-
return "لم يتم استخراج أي بيانات.", ""
|
| 59 |
|
| 60 |
-
|
| 61 |
-
titles_html = titles_df.to_html(index=False) if not titles_df.empty else "لا توجد عناوين مستخرجة."
|
| 62 |
-
hashtags_html = hashtags_df.to_html(index=False) if not hashtags_df.empty else "لا توجد هاشتاغات مستخرجة."
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
inputs=gr.File(label="ارفع ملف HTML"),
|
| 70 |
-
outputs=[
|
| 71 |
-
gr.HTML(label="العناوين والهاشتاغات المستخرجة"),
|
| 72 |
-
gr.HTML(label="الهاشتاغات مع عدد مرات تكرارها")
|
| 73 |
-
],
|
| 74 |
-
title="استخراج العناوين والهاشتاغات",
|
| 75 |
-
description="ارفع ملف HTML لاستخراج العناوين والهاشتاغات مع عدد مرات تكرار كل هاشتاغ."
|
| 76 |
-
)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 1 |
from bs4 import BeautifulSoup
|
| 2 |
import pandas as pd
|
| 3 |
from collections import Counter
|
| 4 |
|
| 5 |
+
def extract_data_from_html(file_path):
|
| 6 |
+
# قراءة محتوى الملف
|
| 7 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 8 |
+
content = file.read()
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# تحليل HTML باستخدام BeautifulSoup
|
| 11 |
soup = BeautifulSoup(content, 'html.parser')
|
|
|
|
| 14 |
data = []
|
| 15 |
hashtags_counter = Counter()
|
| 16 |
|
| 17 |
+
# العثور على الحاويات التي تحتوي على البيانات
|
| 18 |
desc_containers = soup.find_all('div', class_="css-vi46v1-DivDesContainer")
|
|
|
|
|
|
|
|
|
|
| 19 |
for container in desc_containers:
|
| 20 |
# استخراج العنوان
|
| 21 |
+
title_tag = container.find('h1', class_="css-198cw7i-H1Container")
|
| 22 |
title = title_tag.get_text(strip=True) if title_tag else "بدون عنوان"
|
| 23 |
|
| 24 |
# استخراج الهاشتاغات
|
| 25 |
hashtags = [
|
| 26 |
tag.get_text(strip=True)
|
| 27 |
+
for tag in container.find_all('a', class_="css-sbcvet-StyledCommonLink")
|
| 28 |
if tag.get_text(strip=True).startswith('#')
|
| 29 |
]
|
| 30 |
hashtags_counter.update(hashtags)
|
| 31 |
|
| 32 |
+
# إضافة البيانات إلى القائمة
|
| 33 |
data.append({"Title": title, "Hashtags": ", ".join(hashtags)})
|
| 34 |
|
| 35 |
+
# تحويل البيانات إلى DataFrame
|
| 36 |
+
df_titles = pd.DataFrame(data)
|
| 37 |
+
df_hashtags = pd.DataFrame(hashtags_counter.items(), columns=["Hashtag", "Count"]).sort_values(by="Count", ascending=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
return df_titles, df_hashtags
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# استخدام الدالة لتحليل الملف
|
| 42 |
+
file_path = "/mnt/data/Spanish.txt"
|
| 43 |
+
titles_df, hashtags_df = extract_data_from_html(file_path)
|
| 44 |
|
| 45 |
+
# عرض النتائج
|
| 46 |
+
print("العناوين والهاشتاغات المستخرجة:")
|
| 47 |
+
print(titles_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
print("\nالهاشتاغات مع عدد مرات تكرارها:")
|
| 50 |
+
print(hashtags_df)
|