Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
from bs4 import BeautifulSoup
|
| 2 |
import pandas as pd
|
| 3 |
from collections import Counter
|
|
|
|
| 4 |
|
| 5 |
-
def extract_data_from_html(
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
content = file.read()
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# تحليل HTML باستخدام BeautifulSoup
|
| 11 |
soup = BeautifulSoup(content, 'html.parser')
|
|
@@ -38,13 +41,35 @@ def extract_data_from_html(file_path):
|
|
| 38 |
|
| 39 |
return df_titles, df_hashtags
|
| 40 |
|
| 41 |
-
# ا
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
print(titles_df)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from bs4 import BeautifulSoup
|
| 2 |
import pandas as pd
|
| 3 |
from collections import Counter
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
def extract_data_from_html(file):
|
| 7 |
+
try:
|
| 8 |
+
# قراءة محتوى الملف المرفوع
|
| 9 |
+
content = file.read().decode('utf-8')
|
| 10 |
+
except Exception as e:
|
| 11 |
+
return f"خطأ أثناء قراءة الملف: {str(e)}", None
|
| 12 |
|
| 13 |
# تحليل HTML باستخدام BeautifulSoup
|
| 14 |
soup = BeautifulSoup(content, 'html.parser')
|
|
|
|
| 41 |
|
| 42 |
return df_titles, df_hashtags
|
| 43 |
|
| 44 |
+
# واجهة Gradio
|
| 45 |
+
def gradio_interface(file):
|
| 46 |
+
result = extract_data_from_html(file)
|
| 47 |
+
|
| 48 |
+
if isinstance(result, tuple):
|
| 49 |
+
titles_df, hashtags_df = result
|
| 50 |
+
else:
|
| 51 |
+
return result, ""
|
| 52 |
|
| 53 |
+
if titles_df is None or hashtags_df is None:
|
| 54 |
+
return "لم يتم استخراج أي بيانات.", ""
|
|
|
|
| 55 |
|
| 56 |
+
# تحويل النتائج إلى HTML للعرض
|
| 57 |
+
titles_html = titles_df.to_html(index=False) if not titles_df.empty else "لا توجد عناوين مستخرجة."
|
| 58 |
+
hashtags_html = hashtags_df.to_html(index=False) if not hashtags_df.empty else "لا توجد هاشتاغات مستخرجة."
|
| 59 |
+
|
| 60 |
+
return titles_html, hashtags_html
|
| 61 |
+
|
| 62 |
+
# إنشاء واجهة Gradio
|
| 63 |
+
interface = gr.Interface(
|
| 64 |
+
fn=gradio_interface,
|
| 65 |
+
inputs=gr.File(label="ارفع ملف HTML"),
|
| 66 |
+
outputs=[
|
| 67 |
+
gr.HTML(label="العناوين والهاشتاغات المستخرجة"),
|
| 68 |
+
gr.HTML(label="الهاشتاغات مع عدد مرات تكرارها")
|
| 69 |
+
],
|
| 70 |
+
title="استخراج العناوين والهاشتاغات",
|
| 71 |
+
description="ارفع ملف HTML لاستخراج العناوين والهاشتاغات مع عدد مرات تكرار كل هاشتاغ."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# تشغيل التطبيق
|
| 75 |
+
interface.launch()
|