Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,18 @@ from bs4 import BeautifulSoup
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
|
| 7 |
-
# دالة استخراج ال
|
| 8 |
def extract_titles_and_hashtags(file):
|
| 9 |
try:
|
|
|
|
| 10 |
content = file.read() if hasattr(file, 'read') else open(file.name, 'r', encoding='utf-8').read()
|
| 11 |
except Exception as e:
|
| 12 |
-
return f"خطأ أثناء قراءة الملف: {str(e)}",
|
| 13 |
|
|
|
|
| 14 |
soup = BeautifulSoup(content, 'html.parser')
|
| 15 |
|
|
|
|
| 16 |
data = []
|
| 17 |
hashtags_counter = Counter()
|
| 18 |
|
|
@@ -22,43 +25,26 @@ def extract_titles_and_hashtags(file):
|
|
| 22 |
|
| 23 |
for container in desc_containers:
|
| 24 |
title = container.get('aria-label', 'بدون عنوان')
|
| 25 |
-
|
| 26 |
hashtags = [
|
| 27 |
tag.get_text(strip=True)
|
| 28 |
for tag in container.find_all('a')
|
| 29 |
if tag.get_text(strip=True).startswith('#')
|
| 30 |
]
|
| 31 |
hashtags_counter.update(hashtags)
|
| 32 |
-
|
| 33 |
data.append({"Title": title, "Hashtags": ", ".join(hashtags)})
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
hashtags_html = df_hashtags.to_html(index=False) if not df_hashtags.empty else "لا توجد هاشتاغات مستخرجة."
|
| 40 |
-
|
| 41 |
-
return titles_html, hashtags_html
|
| 42 |
|
| 43 |
|
| 44 |
# إنشاء واجهة Gradio
|
| 45 |
-
|
| 46 |
-
|
| 47 |
def gradio_interface():
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("## 📝 محلل النصوص المتقدم")
|
| 50 |
-
|
| 51 |
-
gr.Markdown(
|
| 52 |
-
"""
|
| 53 |
-
### تثبيت المكتبات الضرورية
|
| 54 |
-
انسخ الكود التالي لتثبيت المكتبات المطلوبة:
|
| 55 |
-
|
| 56 |
-
```
|
| 57 |
-
pip install gradio beautifulsoup4 pandas
|
| 58 |
-
```
|
| 59 |
-
"""
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
with gr.Row():
|
| 63 |
file_input = gr.File(label="📂 رفع ملف TXT", file_types=[".txt"])
|
| 64 |
|
|
@@ -66,13 +52,23 @@ def gradio_interface():
|
|
| 66 |
analyze_btn = gr.Button("تحليل البيانات", variant="primary")
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
analyze_btn.click(
|
| 73 |
fn=extract_titles_and_hashtags,
|
| 74 |
inputs=[file_input],
|
| 75 |
-
outputs=[
|
| 76 |
)
|
| 77 |
|
| 78 |
return demo
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
|
| 7 |
+
# دالة استخراج العناوين والهاشتاغات
|
| 8 |
def extract_titles_and_hashtags(file):
|
| 9 |
try:
|
| 10 |
+
# قراءة محتوى الملف
|
| 11 |
content = file.read() if hasattr(file, 'read') else open(file.name, 'r', encoding='utf-8').read()
|
| 12 |
except Exception as e:
|
| 13 |
+
return f"خطأ أثناء قراءة الملف: {str(e)}", ""
|
| 14 |
|
| 15 |
+
# تحليل HTML باستخدام BeautifulSoup
|
| 16 |
soup = BeautifulSoup(content, 'html.parser')
|
| 17 |
|
| 18 |
+
# استخراج البيانات
|
| 19 |
data = []
|
| 20 |
hashtags_counter = Counter()
|
| 21 |
|
|
|
|
| 25 |
|
| 26 |
for container in desc_containers:
|
| 27 |
title = container.get('aria-label', 'بدون عنوان')
|
|
|
|
| 28 |
hashtags = [
|
| 29 |
tag.get_text(strip=True)
|
| 30 |
for tag in container.find_all('a')
|
| 31 |
if tag.get_text(strip=True).startswith('#')
|
| 32 |
]
|
| 33 |
hashtags_counter.update(hashtags)
|
|
|
|
| 34 |
data.append({"Title": title, "Hashtags": ", ".join(hashtags)})
|
| 35 |
|
| 36 |
+
# تحويل النتائج إلى نصوص
|
| 37 |
+
titles_text = "\n".join([f"{i+1}. {row['Title']}" for i, row in enumerate(data)])
|
| 38 |
+
hashtags_text = "\n".join([f"{hashtag}: {count}" for hashtag, count in hashtags_counter.items()])
|
| 39 |
|
| 40 |
+
return titles_text or "لا توجد عناوين مستخرجة.", hashtags_text or "لا توجد هاشتاغات مستخرجة."
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
# إنشاء واجهة Gradio
|
|
|
|
|
|
|
| 44 |
def gradio_interface():
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
gr.Markdown("## 📝 محلل النصوص المتقدم")
|
| 47 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
with gr.Row():
|
| 49 |
file_input = gr.File(label="📂 رفع ملف TXT", file_types=[".txt"])
|
| 50 |
|
|
|
|
| 52 |
analyze_btn = gr.Button("تحليل البيانات", variant="primary")
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
+
titles_output = gr.Textbox(
|
| 56 |
+
label="📜 العناوين المستخرجة",
|
| 57 |
+
lines=10,
|
| 58 |
+
interactive=False,
|
| 59 |
+
placeholder="ستظهر العناوين هنا"
|
| 60 |
+
)
|
| 61 |
+
hashtags_output = gr.Textbox(
|
| 62 |
+
label="🏷️ الهاشتاغات المستخرجة",
|
| 63 |
+
lines=10,
|
| 64 |
+
interactive=False,
|
| 65 |
+
placeholder="ستظهر الهاشتاغات هنا"
|
| 66 |
+
)
|
| 67 |
|
| 68 |
analyze_btn.click(
|
| 69 |
fn=extract_titles_and_hashtags,
|
| 70 |
inputs=[file_input],
|
| 71 |
+
outputs=[titles_output, hashtags_output],
|
| 72 |
)
|
| 73 |
|
| 74 |
return demo
|