Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,122 +1,116 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
def extract_youtube_id(url):
|
8 |
-
"""استخراج معرف فيديو يوتيوب من الرابط"""
|
9 |
-
match = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', url)
|
10 |
-
return match.group(1) if match else None
|
11 |
-
|
12 |
-
def extract_tiktok_id(url):
|
13 |
-
"""استخراج معرف فيديو تيكتوك من الرابط"""
|
14 |
-
match = re.search(r'video/(\d+)', url)
|
15 |
-
return match.group(1) if match else None
|
16 |
-
|
17 |
-
def get_hashtags_from_text(text):
|
18 |
-
"""استخراج الهاشتاغات من النص"""
|
19 |
-
return re.findall(r'#\w+', text)
|
20 |
-
|
21 |
-
def extract_from_url(url):
|
22 |
-
"""استخراج البيانات من الرابط"""
|
23 |
-
domain = urlparse(url).netloc
|
24 |
-
headers = {
|
25 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
26 |
-
}
|
27 |
-
|
28 |
try:
|
29 |
-
#
|
30 |
-
if
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# استخراج الوصف
|
43 |
-
description_element = soup.find('div', {'id': 'description'})
|
44 |
-
description = description_element.text.strip() if description_element else "الوصف غير متوفر"
|
45 |
-
|
46 |
-
# استخراج الهاشتاغات
|
47 |
-
hashtags = get_hashtags_from_text(description)
|
48 |
-
|
49 |
-
return title, description, "\n".join(hashtags)
|
50 |
-
|
51 |
-
# استخراج البيانات من TikTok
|
52 |
-
elif 'tiktok.com' in domain:
|
53 |
-
video_id = extract_tiktok_id(url)
|
54 |
-
if not video_id:
|
55 |
-
return "رابط تيكتوك غير صالح", "", ""
|
56 |
-
|
57 |
-
response = requests.get(url, headers=headers)
|
58 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
59 |
-
|
60 |
-
# استخراج العنوان
|
61 |
-
title_element = soup.find('h1')
|
62 |
-
title = title_element.text.strip() if title_element else "العنوان غير متوفر"
|
63 |
-
|
64 |
-
# استخراج الوصف
|
65 |
-
description = title # الوصف موجود داخل نفس العنصر <h1>
|
66 |
-
|
67 |
-
# استخراج الهاشتاغات
|
68 |
-
hashtags_elements = soup.find_all('a', {'class': re.compile(r'.*hashtag.*')})
|
69 |
-
hashtags = [tag.text.strip() for tag in hashtags_elements]
|
70 |
-
|
71 |
-
return title, description, "\n".join(hashtags)
|
72 |
-
|
73 |
except Exception as e:
|
74 |
-
return f"
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
|
|
|
|
78 |
def gradio_interface():
|
79 |
with gr.Blocks() as demo:
|
80 |
-
gr.Markdown("##
|
81 |
-
|
82 |
-
with gr.Row():
|
83 |
-
url_input = gr.Textbox(
|
84 |
-
label="🔗 أدخل رابط يوتيوب/تيكتوك",
|
85 |
-
placeholder="https://.../video/..."
|
86 |
-
)
|
87 |
-
|
88 |
with gr.Row():
|
89 |
-
|
90 |
-
|
91 |
with gr.Row():
|
92 |
-
|
93 |
-
|
94 |
-
lines=2,
|
95 |
-
interactive=False
|
96 |
-
)
|
97 |
-
|
98 |
with gr.Row():
|
99 |
-
|
100 |
-
label="
|
101 |
-
lines=
|
102 |
-
interactive=False
|
|
|
103 |
)
|
104 |
-
|
105 |
-
with gr.Row():
|
106 |
hashtags_output = gr.Textbox(
|
107 |
-
label="🏷️ الهاشتاغات",
|
108 |
-
lines=
|
109 |
-
interactive=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
)
|
111 |
-
|
112 |
analyze_btn.click(
|
113 |
-
fn=
|
114 |
-
inputs=[
|
115 |
-
outputs=[
|
116 |
)
|
117 |
-
|
118 |
return demo
|
119 |
|
|
|
|
|
120 |
if __name__ == "__main__":
|
121 |
demo = gradio_interface()
|
122 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from collections import Counter
|
3 |
from bs4 import BeautifulSoup
|
4 |
+
|
5 |
+
|
6 |
+
def extract_titles_and_hashtags_and_views(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
try:
|
8 |
+
# قراءة محتوى الملف
|
9 |
+
if hasattr(file, 'read'):
|
10 |
+
content = file.read()
|
11 |
+
else:
|
12 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
13 |
+
content = f.read()
|
14 |
+
except Exception as e:
|
15 |
+
return f"خطأ أثناء قراءة الملف: {str(e)}", "", "", ""
|
16 |
+
|
17 |
+
# تحليل HTML باستخدام BeautifulSoup
|
18 |
+
try:
|
19 |
+
soup = BeautifulSoup(content, 'html.parser')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
except Exception as e:
|
21 |
+
return f"خطأ في تحليل محتوى HTML: {str(e)}", "", "", ""
|
22 |
+
|
23 |
+
# استخراج البيانات
|
24 |
+
data = []
|
25 |
+
hashtags_counter = Counter()
|
26 |
+
views_text = []
|
27 |
+
|
28 |
+
# البحث عن الحاويات
|
29 |
+
desc_containers = soup.find_all('div', class_="css-vi46v1-DivDesContainer")
|
30 |
+
if not desc_containers:
|
31 |
+
return "لم يتم العثور على أي بيانات مطابقة.", "", "", ""
|
32 |
+
|
33 |
+
# معالجة كل حاوية
|
34 |
+
for container in desc_containers:
|
35 |
+
# البحث عن العنوان
|
36 |
+
title = (
|
37 |
+
container.find('h2', class_='title')
|
38 |
+
or container.find('h1', class_='title')
|
39 |
+
or container.find('div', class_='title')
|
40 |
+
or container.find(class_='title')
|
41 |
+
)
|
42 |
+
title = title.get_text(strip=True) if title else container.get('aria-label', 'بدون عنوان').strip()
|
43 |
+
|
44 |
+
# استخراج الهاشتاغات
|
45 |
+
hashtags = [tag.get_text(strip=True) for tag in container.find_all('a') if tag.get_text(strip=True).startswith('#')]
|
46 |
+
if hashtags:
|
47 |
+
hashtags_counter.update(hashtags)
|
48 |
+
|
49 |
+
# استخراج نسبة المشاهدة
|
50 |
+
view = container.find('strong', class_="css-ws4x78-StrongVideoCount etrd4pu10")
|
51 |
+
view = view.get_text(strip=True) if view else "غير متوفر"
|
52 |
+
|
53 |
+
# تخزين البيانات
|
54 |
+
data.append({
|
55 |
+
"Title": title,
|
56 |
+
"Hashtags": ", ".join(hashtags),
|
57 |
+
"Views": view
|
58 |
+
})
|
59 |
+
views_text.append(f"{title}: {view}")
|
60 |
+
|
61 |
+
# إعداد النصوص النهائية
|
62 |
+
titles_text = "\n".join(f"{i+1}. {row['Title']}" for i, row in enumerate(data) if row['Title'] != 'بدون عنوان')
|
63 |
+
hashtags_text = "\n".join(f"{hashtag}: {count}" for hashtag, count in sorted(hashtags_counter.items(), key=lambda x: (-x[1], x[0])))
|
64 |
+
views_summary_text = "\n".join(views_text)
|
65 |
+
|
66 |
+
return (
|
67 |
+
titles_text or "لا توجد عناوين مستخرجة.",
|
68 |
+
hashtags_text or "لا توجد هاشتاغات مستخرجة.",
|
69 |
+
views_summary_text or "لا توجد بيانات مشاهدة."
|
70 |
+
)
|
71 |
|
72 |
+
|
73 |
+
# إنشاء واجهة Gradio
|
74 |
def gradio_interface():
|
75 |
with gr.Blocks() as demo:
|
76 |
+
gr.Markdown("## 📝 محلل النصوص المتقدم")
|
77 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
with gr.Row():
|
79 |
+
file_input = gr.File(label="📂 رفع ملف TXT", file_types=[".txt"])
|
80 |
+
|
81 |
with gr.Row():
|
82 |
+
analyze_btn = gr.Button("تحليل البيانات", variant="primary")
|
83 |
+
|
|
|
|
|
|
|
|
|
84 |
with gr.Row():
|
85 |
+
titles_output = gr.Textbox(
|
86 |
+
label="📜 العناوين المستخرجة",
|
87 |
+
lines=10,
|
88 |
+
interactive=False,
|
89 |
+
placeholder="ستظهر العناوين هنا"
|
90 |
)
|
|
|
|
|
91 |
hashtags_output = gr.Textbox(
|
92 |
+
label="🏷️ الهاشتاغات المستخرجة (مع التكرار)",
|
93 |
+
lines=10,
|
94 |
+
interactive=False,
|
95 |
+
placeholder="ستظهر الهاشتاغات هنا"
|
96 |
+
)
|
97 |
+
views_output = gr.Textbox(
|
98 |
+
label="👀 نسبة المشاهدة",
|
99 |
+
lines=10,
|
100 |
+
interactive=False,
|
101 |
+
placeholder="ستظهر نسب المشاهدة هنا"
|
102 |
)
|
103 |
+
|
104 |
analyze_btn.click(
|
105 |
+
fn=extract_titles_and_hashtags_and_views,
|
106 |
+
inputs=[file_input],
|
107 |
+
outputs=[titles_output, hashtags_output, views_output],
|
108 |
)
|
109 |
+
|
110 |
return demo
|
111 |
|
112 |
+
|
113 |
+
# تشغيل التطبيق
|
114 |
if __name__ == "__main__":
|
115 |
demo = gradio_interface()
|
116 |
demo.launch()
|