Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import autogen
|
| 3 |
-
import os
|
| 4 |
from utils import process_uploaded_file, export_to_docx
|
| 5 |
from agents_config import create_agents
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
file_content = process_uploaded_file(uploaded_file)
|
| 19 |
-
except Exception as e:
|
| 20 |
-
return f"❌ خطأ في قراءة ملف docx: {str(e)}", None
|
| 21 |
-
|
| 22 |
-
# دمج المحتوى مع الحفاظ على الأسطر
|
| 23 |
-
combined_context = f"{file_content}\n\n{manual_text}".strip()
|
| 24 |
|
| 25 |
-
if len(
|
| 26 |
-
return "⚠️ ال
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# 3. إعداد غرفة النقاش (Group Chat)
|
| 33 |
-
# الوكلاء المساعدون (بدون المدير ورئيس التحرير)
|
| 34 |
-
assistant_agents = [
|
| 35 |
-
agents_dict["analyst"], agents_dict["style_guardian"],
|
| 36 |
-
agents_dict["architect"], agents_dict["draft_writer"],
|
| 37 |
-
agents_dict["humanizer"], agents_dict["continuity_guard"],
|
| 38 |
-
agents_dict["psychologist"], agents_dict["critic"]
|
| 39 |
-
]
|
| 40 |
-
|
| 41 |
groupchat = autogen.GroupChat(
|
| 42 |
-
agents=
|
| 43 |
messages=[],
|
| 44 |
-
max_round=
|
| 45 |
-
speaker_selection_method="auto"
|
| 46 |
)
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
llm_config=agents_dict["editor"].llm_config
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
# 4. إطلاق المهمة بقيادة Ling-1T
|
| 54 |
-
init_message = f"""بصفتي رئيس التحرير، أضع بين أيديكم هذه المسودة.
|
| 55 |
-
المطلوب: تحليلها، إكمال أحداث الفصل التالي، وتنسيقها أدبياً بشكل احترافي.
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
manager,
|
| 62 |
-
message=init_message
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
# 5. استخراج النص النهائي من Ling-1T وتوليد ملف DOCX
|
| 66 |
-
# نبحث عن آخر رسالة تحتوي على النص المدمج
|
| 67 |
-
final_story_text = chat_result.chat_history[-1]['content']
|
| 68 |
-
|
| 69 |
-
# توليد الملف باستخدام دالة utils المحدثة
|
| 70 |
-
output_file_path = export_to_docx(final_story_text)
|
| 71 |
-
|
| 72 |
-
return final_story_text, output_file_path
|
| 73 |
-
|
| 74 |
except Exception as e:
|
| 75 |
-
return f"❌
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
with gr.Blocks(
|
| 79 |
-
gr.
|
| 80 |
-
<div style="text-align: center;">
|
| 81 |
-
<h1>🖋️ Ling-1T Novel Production Studio</h1>
|
| 82 |
-
<p>نظام ذكاء اصطناعي متعدد الوكلاء لإنتاج وتنسيق الروايات العربية</p>
|
| 83 |
-
</div>
|
| 84 |
-
""")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
-
with gr.Column(
|
| 88 |
-
gr.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
)
|
| 94 |
-
file_upload = gr.File(
|
| 95 |
-
label="ارفع مسودة الرواية (.docx, .txt)",
|
| 96 |
-
file_types=[".docx", ".txt"]
|
| 97 |
-
)
|
| 98 |
-
text_area = gr.Textbox(
|
| 99 |
-
label="تعليمات إضافية أو نص تكميلي",
|
| 100 |
-
lines=6,
|
| 101 |
-
placeholder="اكتب هنا إذا كنت تريد توجيه الوكلاء لمسار معين..."
|
| 102 |
-
)
|
| 103 |
-
submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
|
| 104 |
|
| 105 |
-
with gr.Column(
|
| 106 |
-
gr.
|
| 107 |
-
|
| 108 |
-
label="معاينة النص",
|
| 109 |
-
value="ستظهر الرواية هنا بعد اكتمال نقاش الوكلاء..."
|
| 110 |
-
)
|
| 111 |
-
download_btn = gr.File(label="تحميل ملف الوورد المنسق")
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
)
|
| 119 |
|
| 120 |
-
gr.Markdown("""
|
| 121 |
-
---
|
| 122 |
-
**ملاحظة:** هذا النظام يستخدم 9 نماذج متخصصة تعمل بالتوازي. قد تستغرق العملية من 1-3 دقائق حسب طول النص.
|
| 123 |
-
""")
|
| 124 |
-
|
| 125 |
if __name__ == "__main__":
|
| 126 |
-
|
| 127 |
-
# وهذا هو الحل الجذري لمشكلة TypeError: argument of type 'bool'
|
| 128 |
-
demo.launch(
|
| 129 |
-
server_name="0.0.0.0",
|
| 130 |
-
server_port=7860,
|
| 131 |
-
show_api=False
|
| 132 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import autogen
|
|
|
|
| 3 |
from utils import process_uploaded_file, export_to_docx
|
| 4 |
from agents_config import create_agents
|
| 5 |
|
| 6 |
+
# الدالة الأساسية (تأكد أن الأسماء تطابق ما في utils.py)
|
| 7 |
+
def process_all(file_obj, manual_text, token):
|
| 8 |
+
if not token:
|
| 9 |
+
return "❌ يرجى إدخال Token", None
|
| 10 |
+
|
| 11 |
+
# قراءة الملف
|
| 12 |
+
context = ""
|
| 13 |
+
if file_obj is not None:
|
| 14 |
+
context = process_uploaded_file(file_obj)
|
| 15 |
+
|
| 16 |
+
context += "\n" + manual_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
if len(context.strip()) < 10:
|
| 19 |
+
return "⚠️ النص قصير جداً", None
|
| 20 |
|
| 21 |
try:
|
| 22 |
+
agents = create_agents(token)
|
| 23 |
+
# إعداد الحوار
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
groupchat = autogen.GroupChat(
|
| 25 |
+
agents=[v for k, v in agents.items() if k != "editor"],
|
| 26 |
messages=[],
|
| 27 |
+
max_round=8
|
|
|
|
| 28 |
)
|
| 29 |
+
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=agents["editor"].llm_config)
|
| 30 |
|
| 31 |
+
# التنفيذ
|
| 32 |
+
chat_result = agents["editor"].initiate_chat(manager, message=f"أكمل ونسق النص: {context}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
final_text = chat_result.chat_history[-1]['content']
|
| 35 |
+
docx_file = export_to_docx(final_text)
|
| 36 |
|
| 37 |
+
return final_text, docx_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"❌ خطأ تقني: {str(e)}", None
|
| 40 |
|
| 41 |
+
# واجهة مستقرة جداً (Minimalist Approach)
|
| 42 |
+
with gr.Blocks(ssr=False) as demo: # تعطيل SSR هو مفتاح الحل هنا
|
| 43 |
+
gr.Markdown("# 🖋️ Ling-1T Novel Production")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
token_input = gr.Textbox(label="HF Token", type="password")
|
| 48 |
+
# تبسيط الـ File component لأقصى حد
|
| 49 |
+
file_input = gr.File(label="Upload DOCX/TXT")
|
| 50 |
+
text_input = gr.Textbox(label="Additional Text", lines=3)
|
| 51 |
+
run_btn = gr.Button("🚀 Start")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
with gr.Column():
|
| 54 |
+
output_text = gr.Textbox(label="Manuscript", lines=10)
|
| 55 |
+
output_file = gr.File(label="Download DOCX")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
run_btn.click(
|
| 58 |
+
fn=process_all,
|
| 59 |
+
inputs=[file_input, text_input, token_input],
|
| 60 |
+
outputs=[output_text, output_file],
|
| 61 |
+
api_name=False # منع توليد API لهذا الزر تحديداً
|
| 62 |
)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
demo.launch(show_api=False) # إغلاق الـ API تماماً
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|