Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -454,12 +454,20 @@ def check_grammar_and_style(text):
|
|
454 |
|
455 |
return text
|
456 |
|
457 |
-
def generate_article(topic, article_type):
|
458 |
template = get_article_template(article_type)
|
459 |
structure = template["structure"]
|
460 |
style = template["style"]
|
461 |
transitions = template["transitions"]
|
462 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
463 |
# إنشاء المقال الأساسي
|
464 |
article = {
|
465 |
"title": generate_title(topic, style),
|
@@ -469,14 +477,61 @@ def generate_article(topic, article_type):
|
|
469 |
"faq": generate_faq(topic, style)
|
470 |
}
|
471 |
|
472 |
-
#
|
473 |
formatted_text = format_article(article)
|
|
|
|
|
474 |
enhanced_text = enhance_text_variation(formatted_text)
|
475 |
humanized_text = add_human_touch(enhanced_text)
|
476 |
final_text = check_grammar_and_style(humanized_text)
|
477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
return final_text
|
479 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
480 |
def format_article(article):
|
481 |
"""تنسيق المقال بشكل جميل"""
|
482 |
formatted_text = f"""# {article['title']}
|
@@ -568,12 +623,18 @@ def generate_faq(topic, style):
|
|
568 |
|
569 |
# إنشاء واجهة المستخدم
|
570 |
with gr.Blocks(title="مساعد كتابة المقالات العربية") as demo:
|
571 |
-
gr.Markdown("
|
572 |
-
|
|
|
|
|
573 |
|
574 |
with gr.Tab("إنشاء مقال احترافي"):
|
575 |
with gr.Row():
|
576 |
-
topic_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
577 |
article_type = gr.Dropdown(
|
578 |
choices=["علمي", "صحفي", "تقني", "تعليمي", "تسويقي", "اجتماعي", "رأي", "ثقافي"],
|
579 |
label="نوع المقال",
|
@@ -582,7 +643,7 @@ with gr.Blocks(title="مساعد كتابة المقالات العربية") as
|
|
582 |
|
583 |
with gr.Row():
|
584 |
word_count_slider = gr.Slider(
|
585 |
-
minimum=
|
586 |
maximum=3000,
|
587 |
value=1500,
|
588 |
step=100,
|
@@ -606,16 +667,17 @@ with gr.Blocks(title="مساعد كتابة المقالات العربية") as
|
|
606 |
|
607 |
word_count_output = gr.Textbox(label="عدد الكلمات الفعلي")
|
608 |
|
|
|
|
|
|
|
|
|
609 |
def count_words(text):
|
610 |
words = text.split()
|
611 |
return f"عدد الكلمات: {len(words)}"
|
612 |
|
613 |
-
def copy_article(text):
|
614 |
-
return text
|
615 |
-
|
616 |
generate_btn.click(
|
617 |
-
fn=
|
618 |
-
inputs=[topic_input, article_type],
|
619 |
outputs=[output]
|
620 |
).then(
|
621 |
fn=count_words,
|
@@ -624,7 +686,7 @@ with gr.Blocks(title="مساعد كتابة المقالات العربية") as
|
|
624 |
)
|
625 |
|
626 |
copy_btn.click(
|
627 |
-
fn=
|
628 |
inputs=[output],
|
629 |
outputs=[output]
|
630 |
)
|
|
|
454 |
|
455 |
return text
|
456 |
|
457 |
+
def generate_article(topic, article_type, word_count=1500, formality="رسمي"):
|
458 |
template = get_article_template(article_type)
|
459 |
structure = template["structure"]
|
460 |
style = template["style"]
|
461 |
transitions = template["transitions"]
|
462 |
|
463 |
+
# تعديل الأسلوب حسب مستوى الرسمية
|
464 |
+
if formality == "رسمي":
|
465 |
+
style = "أكاديمي رسمي"
|
466 |
+
elif formality == "شبه رسمي":
|
467 |
+
style = "إخباري موضوعي"
|
468 |
+
else: # عادي
|
469 |
+
style = "تعليمي تفاعلي"
|
470 |
+
|
471 |
# إنشاء المقال الأساسي
|
472 |
article = {
|
473 |
"title": generate_title(topic, style),
|
|
|
477 |
"faq": generate_faq(topic, style)
|
478 |
}
|
479 |
|
480 |
+
# تنسيق المقال
|
481 |
formatted_text = format_article(article)
|
482 |
+
|
483 |
+
# تحسين المحتوى
|
484 |
enhanced_text = enhance_text_variation(formatted_text)
|
485 |
humanized_text = add_human_touch(enhanced_text)
|
486 |
final_text = check_grammar_and_style(humanized_text)
|
487 |
|
488 |
+
# ضبط عدد الكلمات
|
489 |
+
current_word_count = len(final_text.split())
|
490 |
+
if current_word_count < word_count:
|
491 |
+
# إضافة محتوى إضافي
|
492 |
+
additional_content = generate_additional_content(topic, style, word_count - current_word_count)
|
493 |
+
final_text = insert_additional_content(final_text, additional_content)
|
494 |
+
elif current_word_count > word_count:
|
495 |
+
# تقليص المحتوى مع الحفاظ على التماسك
|
496 |
+
final_text = trim_content(final_text, word_count)
|
497 |
+
|
498 |
return final_text
|
499 |
|
500 |
+
def generate_additional_content(topic, style, words_needed):
|
501 |
+
"""توليد محتوى إضافي للوصول إلى عدد الكلمات المطلوب"""
|
502 |
+
sections = [
|
503 |
+
f"وفي سياق متصل، يمكن الإشارة إلى أن {topic} يشهد تطورات مستمرة",
|
504 |
+
f"ومن الجدير بالذكر أن دراسات حديثة في مجال {topic} قد أظهرت نتائج مثيرة للاهتمام",
|
505 |
+
f"وعلاوة على ذلك، فإن التطبيقات العملية لـ {topic} تتنوع بشكل كبير"
|
506 |
+
]
|
507 |
+
return " ".join(sections[:words_needed//50]) # تقريب تقديري لعدد الكلمات المطلوبة
|
508 |
+
|
509 |
+
def trim_content(text, target_word_count):
|
510 |
+
"""تقليص المحتوى للوصول إلى عدد الكلمات المطلوب"""
|
511 |
+
words = text.split()
|
512 |
+
if len(words) <= target_word_count:
|
513 |
+
return text
|
514 |
+
|
515 |
+
# الحفاظ على المقدمة والخاتمة
|
516 |
+
paragraphs = text.split("\n\n")
|
517 |
+
intro = paragraphs[0]
|
518 |
+
conclusion = paragraphs[-1]
|
519 |
+
|
520 |
+
# تقليص المحتوى الرئيسي
|
521 |
+
main_content = "\n\n".join(paragraphs[1:-1])
|
522 |
+
main_words = main_content.split()
|
523 |
+
words_to_keep = target_word_count - len(intro.split()) - len(conclusion.split())
|
524 |
+
trimmed_main = " ".join(main_words[:words_to_keep])
|
525 |
+
|
526 |
+
return f"{intro}\n\n{trimmed_main}\n\n{conclusion}"
|
527 |
+
|
528 |
+
def insert_additional_content(text, additional_content):
|
529 |
+
"""إدراج المحتوى الإضافي في المكان المناسب من المقال"""
|
530 |
+
paragraphs = text.split("\n\n")
|
531 |
+
# إدراج المحتوى قبل الخاتمة
|
532 |
+
paragraphs.insert(-2, additional_content)
|
533 |
+
return "\n\n".join(paragraphs)
|
534 |
+
|
535 |
def format_article(article):
|
536 |
"""تنسيق المقال بشكل جميل"""
|
537 |
formatted_text = f"""# {article['title']}
|
|
|
623 |
|
624 |
# إنشاء واجهة المستخدم
|
625 |
with gr.Blocks(title="مساعد كتابة المقالات العربية") as demo:
|
626 |
+
gr.Markdown("""
|
627 |
+
# 📝 مساعد كتابة المقالات العربية
|
628 |
+
### نظام متطور لإنشاء مقالات عربية احترافية ومتنوعة
|
629 |
+
""")
|
630 |
|
631 |
with gr.Tab("إنشاء مقال احترافي"):
|
632 |
with gr.Row():
|
633 |
+
topic_input = gr.Textbox(
|
634 |
+
label="موضوع المقال",
|
635 |
+
placeholder="أدخل موضوع المقال هنا...",
|
636 |
+
lines=2
|
637 |
+
)
|
638 |
article_type = gr.Dropdown(
|
639 |
choices=["علمي", "صحفي", "تقني", "تعليمي", "تسويقي", "اجتماعي", "رأي", "ثقافي"],
|
640 |
label="نوع المقال",
|
|
|
643 |
|
644 |
with gr.Row():
|
645 |
word_count_slider = gr.Slider(
|
646 |
+
minimum=500,
|
647 |
maximum=3000,
|
648 |
value=1500,
|
649 |
step=100,
|
|
|
667 |
|
668 |
word_count_output = gr.Textbox(label="عدد الكلمات الفعلي")
|
669 |
|
670 |
+
def generate_and_format(topic, article_type, word_count, formality):
|
671 |
+
article = generate_article(topic, article_type, word_count, formality)
|
672 |
+
return article
|
673 |
+
|
674 |
def count_words(text):
|
675 |
words = text.split()
|
676 |
return f"عدد الكلمات: {len(words)}"
|
677 |
|
|
|
|
|
|
|
678 |
generate_btn.click(
|
679 |
+
fn=generate_and_format,
|
680 |
+
inputs=[topic_input, article_type, word_count_slider, formality_level],
|
681 |
outputs=[output]
|
682 |
).then(
|
683 |
fn=count_words,
|
|
|
686 |
)
|
687 |
|
688 |
copy_btn.click(
|
689 |
+
fn=lambda x: x,
|
690 |
inputs=[output],
|
691 |
outputs=[output]
|
692 |
)
|