KIMOSSINO commited on
Commit
4e6c980
·
verified ·
1 Parent(s): 6e18263

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -71
app.py CHANGED
@@ -2,8 +2,7 @@ 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'):
@@ -12,104 +11,83 @@ def extract_titles_and_hashtags_and_views(file):
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_="video-count css-dirst9-StrongVideoCount e148ts222")
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()
 
2
  from collections import Counter
3
  from bs4 import BeautifulSoup
4
 
5
+ def analyze_videos(file):
 
6
  try:
7
  # قراءة محتوى الملف
8
  if hasattr(file, 'read'):
 
11
  with open(file.name, 'r', encoding='utf-8') as f:
12
  content = f.read()
13
  except Exception as e:
14
+ return f"خطأ أثناء قراءة الملف: {str(e)}", ""
15
 
16
  # تحليل HTML باستخدام BeautifulSoup
17
  try:
18
  soup = BeautifulSoup(content, 'html.parser')
19
  except Exception as e:
20
+ return f"خطأ في تحليل محتوى HTML: {str(e)}", ""
21
+
22
+ # استخراج بيانات الفيديوهات
23
+ videos_data = []
24
+ video_elements = soup.find_all('a', class_="css-1wrhn5c-AMetaCaptionLine")
25
+
26
+ if not video_elements:
27
+ return "لم يتم العثور على أي بيانات مطابقة.", ""
28
+
29
+ for video in video_elements:
30
+ video_info = {}
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ # استخراج الرابط
33
+ video_info["Link"] = video.get('href', 'رابط غير متوفر')
34
+
35
+ # استخراج العنوان
36
+ title_element = video.find('span', class_="css-j2a19r-SpanText")
37
+ video_info["Title"] = title_element.get_text(strip=True) if title_element else "عنوان غير متوفر"
38
+
39
  # استخراج الهاشتاغات
40
+ hashtags = [
41
+ tag.get_text(strip=True)
42
+ for tag in video.find_all('strong', class_="css-1p6dp51-StrongText")
43
+ ]
44
+ video_info["Hashtags"] = ", ".join(hashtags)
45
+
46
+ # استخراج عدد المشاهدات
47
+ views_element = video.find_next('strong', class_="css-ws4x78-StrongVideoCount")
48
+ video_info["Views"] = views_element.get_text(strip=True) if views_element else "عدد المشاهدات غير متوفر"
49
+
50
+ videos_data.append(video_info)
51
+
52
+ # تجهيز النصوص النهائية
53
+ videos_summary = "\n\n".join(
54
+ f"📹 الفيديو {i+1}:\n"
55
+ f"- الرابط: {video['Link']}\n"
56
+ f"- العنوان: {video['Title']}\n"
57
+ f"- الهاشتاغات: {video['Hashtags']}\n"
58
+ f"- عدد المشاهدات: {video['Views']}"
59
+ for i, video in enumerate(videos_data)
 
 
 
 
 
60
  )
61
 
62
+ return videos_summary or "لم يتم استخراج أي معلومات."
63
 
64
  # إنشاء واجهة Gradio
65
  def gradio_interface():
66
  with gr.Blocks() as demo:
67
+ gr.Markdown("## 📝 محلل فيديوهات تيك توك")
68
 
69
  with gr.Row():
70
+ file_input = gr.File(label="📂 رفع ملف HTML", file_types=[".html"])
71
 
72
  with gr.Row():
73
  analyze_btn = gr.Button("تحليل البيانات", variant="primary")
74
 
75
  with gr.Row():
76
+ output_box = gr.Textbox(
77
+ label="📜 البيانات المستخرجة",
78
+ lines=20,
 
 
 
 
 
 
79
  interactive=False,
80
+ placeholder="ستظهر البيانات هنا"
 
 
 
 
 
 
81
  )
82
 
83
  analyze_btn.click(
84
+ fn=analyze_videos,
85
  inputs=[file_input],
86
+ outputs=[output_box],
87
  )
88
 
89
  return demo
90
 
 
91
  # تشغيل التطبيق
92
  if __name__ == "__main__":
93
  demo = gradio_interface()