FECUOY commited on
Commit
c91de59
·
verified ·
1 Parent(s): e2a3c3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -37
app.py CHANGED
@@ -1,65 +1,126 @@
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
  def run_novel_studio(uploaded_file, manual_text, user_token):
7
- if not user_token:
8
- return "⚠️ يرجى إدخال التوكن.", None
 
 
 
9
 
10
- # معالجة النص من الملف أو الكتابة اليدوية
11
- context = ""
12
  if uploaded_file is not None:
13
- context = process_uploaded_file(uploaded_file)
14
-
15
- context += "\n" + manual_text
 
 
 
 
16
 
17
- if len(context.strip()) < 10:
18
- return "⚠️ المحتوى فارغ جداً.", None
19
 
20
  try:
21
- agents = create_agents(user_token)
22
- # إعداد المجموعة
 
 
 
 
 
 
 
 
 
 
23
  groupchat = autogen.GroupChat(
24
- agents=[v for k, v in agents.items() if k != "editor"],
25
  messages=[],
26
- max_round=10
 
27
  )
 
28
  manager = autogen.GroupChatManager(
29
- groupchat=groupchat,
30
- llm_config=agents["editor"].llm_config
31
  )
32
 
33
- # بدء الحوار
34
- chat_result = agents["editor"].initiate_chat(
 
 
 
 
 
 
35
  manager,
36
- message=f"أكمل هذه الرواية ونسقها:\n\n{context}"
37
  )
38
 
39
- # استخراج النتيجة
40
- final_text = chat_result.chat_history[-1]['content']
41
- file_path = export_to_docx(final_text)
 
 
 
42
 
43
- return final_text, file_path
 
44
  except Exception as e:
45
- return f"❌ خطأ: {str(e)}", None
46
 
47
- # واجهة بسيطة ومستقرة
48
- with gr.Blocks() as demo:
49
- gr.HTML("<h1 style='text-align: center;'>👑 Ling-1T Novel Studio</h1>")
 
 
 
 
 
50
 
51
  with gr.Row():
52
- with gr.Column():
53
- token = gr.Textbox(label="HF Token", type="password")
54
- # حددنا الأنواع بشكل صريح لضمان الاستقرار
55
- file_in = gr.File(label="Upload docx/txt")
56
- text_in = gr.Textbox(label="Manual Input", lines=5)
57
- btn = gr.Button("🚀 Start Production")
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- with gr.Column():
60
- text_out = gr.Textbox(label="Final Manuscript", lines=15)
61
- file_out = gr.File(label="Download DOCX")
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- btn.click(run_novel_studio, [file_in, text_in, token], [text_out, file_out])
 
 
 
64
 
65
- demo.launch()
 
 
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
  def run_novel_studio(uploaded_file, manual_text, user_token):
8
+ """
9
+ الدالة الرئيسية التي تدير تدفق العمل من الرفع إلى التصدير.
10
+ """
11
+ if not user_token or not user_token.startswith("hf_"):
12
+ return "⚠️ يرجى إدخال Hugging Face Token صحيح (يبدأ بـ hf_).", None
13
 
14
+ # 1. معالجة المدخلات (الملف المرفوع + النص اليدوي)
15
+ file_content = ""
16
  if uploaded_file is not None:
17
+ try:
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(combined_context) < 10:
26
+ return "⚠️ المحتوى المقدم غير كافٍ. يرجى رفع ملف أو كتابة مسودة.", None
27
 
28
  try:
29
+ # 2. إنشاء فريق الوكلاء الـ 9
30
+ agents_dict = create_agents(user_token)
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=assistant_agents,
43
  messages=[],
44
+ max_round=12,
45
+ speaker_selection_method="auto"
46
  )
47
+
48
  manager = autogen.GroupChatManager(
49
+ groupchat=groupchat,
50
+ llm_config=agents_dict["editor"].llm_config
51
  )
52
 
53
+ # 4. إطلاق المهمة بقيادة Ling-1T
54
+ init_message = f"""بصفتي رئيس التحرير، أضع بين أيديكم هذه المسودة.
55
+ المطلوب: تحليلها، إكمال أحداث الفصل التالي، وتنسيقها أدبياً بشكل احترافي.
56
+
57
+ المسودة:
58
+ {combined_context}"""
59
+
60
+ chat_result = agents_dict["editor"].initiate_chat(
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"❌ فشل النظام في المعالجة: {str(e)}", None
76
 
77
+ # تصميم واجهة المستخدم (تنسيق احترافي وهادئ)
78
+ with gr.Blocks(theme=gr.themes.Soft(), title="Ling-1T Novel Studio") as demo:
79
+ gr.HTML("""
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(scale=1):
88
+ gr.Markdown("### 🛠️ إعدادات المدخلات")
89
+ token_input = gr.Textbox(
90
+ label="Hugging Face Token",
91
+ type="password",
92
+ placeholder="hf_..."
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(scale=2):
106
+ gr.Markdown("### 📖 المخطوطة النهائية")
107
+ output_markdown = gr.Markdown(
108
+ label="معاينة النص",
109
+ value="ستظهر الرواية هنا بعد اكتمال نقاش الوكلاء..."
110
+ )
111
+ download_btn = gr.File(label="تحميل ملف الوورد المنسق")
112
+
113
+ # ربط الأحداث
114
+ submit_btn.click(
115
+ fn=run_novel_studio,
116
+ inputs=[file_upload, text_area, token_input],
117
+ outputs=[output_markdown, download_btn]
118
+ )
119
 
120
+ gr.Markdown("""
121
+ ---
122
+ **ملاحظة:** هذا النظام يستخدم 9 نماذج متخصصة تعمل بالتوازي. قد تستغرق العملية من 1-3 دقائق حسب طول النص.
123
+ """)
124
 
125
+ if __name__ == "__main__":
126
+ demo.launch()