Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from bs4 import BeautifulSoup
|
| 3 |
from collections import Counter
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
# دالة استخراج ال
|
| 7 |
-
def
|
| 8 |
if not file:
|
| 9 |
return "❌ لم يتم رفع ملف", "❌ لم يتم رفع ملف"
|
| 10 |
|
| 11 |
-
# قراءة محتوى الملف
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# استخراج العناوين من عناصر HTML
|
| 17 |
-
titles = [title.get_text() for title in soup.find_all("title")]
|
| 18 |
-
titles_output = "\n".join(titles) if titles else "❌ لم يتم العثور على عناوين"
|
| 19 |
|
| 20 |
# استخراج النصوص المحتوية على هاشتاغات
|
| 21 |
-
|
| 22 |
-
hashtags = re.findall(r"#\w+", text)
|
| 23 |
|
| 24 |
-
# حساب تكرار الهاشتاغات
|
| 25 |
hashtags_counter = Counter(hashtags)
|
| 26 |
filtered_hashtags = [
|
| 27 |
f"{tag}: {count}" for tag, count in hashtags_counter.items() if count >= min_frequency
|
| 28 |
]
|
| 29 |
hashtags_output = "\n".join(filtered_hashtags) if filtered_hashtags else "❌ لا توجد هاشتاغات متكررة"
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
return f"❌ خطأ أثناء معالجة الملف: {str(e)}", "❌ خطأ أثناء معالجة الملف"
|
|
@@ -37,21 +38,21 @@ def extract_titles_and_hashtags(file, min_frequency=1):
|
|
| 37 |
# إنشاء واجهة Gradio
|
| 38 |
def gradio_interface():
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
-
gr.Markdown("##
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
-
file_input = gr.File(label="📂 رفع ملف
|
| 44 |
min_freq_slider = gr.Slider(
|
| 45 |
minimum=1, maximum=50, value=1, step=1,
|
| 46 |
-
label="📊 الحد الأدنى لتكرار الهاشتاغ"
|
| 47 |
)
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
analyze_btn = gr.Button("تحليل البيانات", variant="primary")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
-
|
| 54 |
-
label="📜 ال
|
| 55 |
lines=10,
|
| 56 |
interactive=False
|
| 57 |
)
|
|
@@ -62,9 +63,9 @@ def gradio_interface():
|
|
| 62 |
)
|
| 63 |
|
| 64 |
analyze_btn.click(
|
| 65 |
-
fn=
|
| 66 |
inputs=[file_input, min_freq_slider],
|
| 67 |
-
outputs=[
|
| 68 |
)
|
| 69 |
|
| 70 |
return demo
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from collections import Counter
|
| 3 |
import re
|
| 4 |
|
| 5 |
+
# دالة استخراج الكلمات والهاشتاغات من ملف .txt
|
| 6 |
+
def extract_from_txt(file, min_frequency=1):
|
| 7 |
if not file:
|
| 8 |
return "❌ لم يتم رفع ملف", "❌ لم يتم رفع ملف"
|
| 9 |
|
|
|
|
| 10 |
try:
|
| 11 |
+
# قراءة محتوى الملف
|
| 12 |
+
content = file.read().decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# استخراج النصوص المحتوية على هاشتاغات
|
| 15 |
+
hashtags = re.findall(r"#\w+", content)
|
|
|
|
| 16 |
|
| 17 |
+
# حساب تكرار الهاشتاغات
|
| 18 |
hashtags_counter = Counter(hashtags)
|
| 19 |
filtered_hashtags = [
|
| 20 |
f"{tag}: {count}" for tag, count in hashtags_counter.items() if count >= min_frequency
|
| 21 |
]
|
| 22 |
hashtags_output = "\n".join(filtered_hashtags) if filtered_hashtags else "❌ لا توجد هاشتاغات متكررة"
|
| 23 |
|
| 24 |
+
# تحليل الكلمات المتكررة في النص
|
| 25 |
+
words = re.findall(r"\b\w+\b", content.lower())
|
| 26 |
+
words_counter = Counter(words)
|
| 27 |
+
filtered_words = [
|
| 28 |
+
f"{word}: {count}" for word, count in words_counter.items() if count >= min_frequency
|
| 29 |
+
]
|
| 30 |
+
words_output = "\n".join(filtered_words) if filtered_words else "❌ لا توجد كلمات متكررة"
|
| 31 |
+
|
| 32 |
+
return words_output, hashtags_output
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
return f"❌ خطأ أثناء معالجة الملف: {str(e)}", "❌ خطأ أثناء معالجة الملف"
|
|
|
|
| 38 |
# إنشاء واجهة Gradio
|
| 39 |
def gradio_interface():
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## 📝 محلل النصوص المتقدم")
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
+
file_input = gr.File(label="📂 رفع ملف TXT", file_types=[".txt"])
|
| 45 |
min_freq_slider = gr.Slider(
|
| 46 |
minimum=1, maximum=50, value=1, step=1,
|
| 47 |
+
label="📊 الحد الأدنى لتكرار الكلمات والهاشتاغات"
|
| 48 |
)
|
| 49 |
|
| 50 |
with gr.Row():
|
| 51 |
analyze_btn = gr.Button("تحليل البيانات", variant="primary")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
+
words_output = gr.Textbox(
|
| 55 |
+
label="📜 الكلمات المتكررة",
|
| 56 |
lines=10,
|
| 57 |
interactive=False
|
| 58 |
)
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
analyze_btn.click(
|
| 66 |
+
fn=extract_from_txt,
|
| 67 |
inputs=[file_input, min_freq_slider],
|
| 68 |
+
outputs=[words_output, hashtags_output],
|
| 69 |
)
|
| 70 |
|
| 71 |
return demo
|