Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -1,65 +1,72 @@
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 تماماً
 
 
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 start_novel_engine(file_obj, extra_text, user_token):
8
+ if not user_token:
9
+ return "⚠️ يرجى إدخال التوكن الخاص بك.", None
 
10
 
11
+ # 1. استخراج النص من الملف
12
  context = ""
13
  if file_obj is not None:
14
+ try:
15
+ context = process_uploaded_file(file_obj)
16
+ except Exception as e:
17
+ return f"❌ خطأ في الملف: {str(e)}", None
18
 
19
+ full_prompt = f"{context}\n\n{extra_text}".strip()
20
+ if len(full_prompt) < 10:
21
+ return "⚠️ النص قصير جدًا للبدء.", None
 
22
 
23
  try:
24
+ # 2. تشغيل الوكلاء
25
+ agents = create_agents(user_token)
26
  groupchat = autogen.GroupChat(
27
  agents=[v for k, v in agents.items() if k != "editor"],
28
  messages=[],
29
+ max_round=10
30
  )
31
  manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=agents["editor"].llm_config)
32
 
33
+ # 3. التواصل مع Ling-1T
34
+ chat_result = agents["editor"].initiate_chat(
35
+ manager,
36
+ message=f"قم بصياغة الفصل التالي وتنسيقه أدبيًا:\n\n{full_prompt}"
37
+ )
38
 
39
  final_text = chat_result.chat_history[-1]['content']
40
+ docx_path = export_to_docx(final_text)
41
 
42
+ return final_text, docx_path
43
  except Exception as e:
44
+ return f"❌ خطأ أثناء الإنتاج: {str(e)}", None
45
 
46
+ # واجهة مستقرة دون SSR وبدون مسببات الخطأ)
47
+ with gr.Blocks(analytics_enabled=False) as demo:
48
+ gr.Markdown("# 🖋️ Ling-1T Novel Studio")
49
 
50
  with gr.Row():
51
  with gr.Column():
52
+ hf_token = gr.Textbox(label="Hugging Face Token", type="password")
53
+ # إزالة أي خصائص إضافية من الملف لضمان الاستقرار
54
+ file_input = gr.File(label="Upload DOCX/TXT")
55
+ instruction = gr.Textbox(label="Special Instructions", lines=3)
56
+ run_btn = gr.Button("🚀 Start Writing", variant="primary")
57
 
58
  with gr.Column():
59
+ output_display = gr.Textbox(label="Manuscript Preview", lines=12)
60
+ download_link = gr.File(label="Download Final DOCX")
61
 
62
+ # القضاء على الخطأ عبر منع الـ API تمامًا لهذا الحدث
63
  run_btn.click(
64
+ fn=start_novel_engine,
65
+ inputs=[file_input, instruction, hf_token],
66
+ outputs=[output_display, download_link],
67
+ api_name=False # هذا السطر يمنع Gradio من فحص المكونات برمجياً
68
  )
69
 
70
  if __name__ == "__main__":
71
+ # تشغيل مباشر وبسيط
72
+ demo.launch(show_api=False)