FECUOY commited on
Commit
af1b71a
·
verified ·
1 Parent(s): 29a8ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -5,25 +5,24 @@ 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, oauth_token: gr.OAuthToken | None):
8
- # جلب التوكن من جلسة تسجيل الدخول
9
  user_token = oauth_token.token if oauth_token else os.getenv("HF_TOKEN")
10
 
11
  if not user_token:
12
- return "⚠️ يرجى تسجيل الدخول عبر زر (Sign in with Hugging Face) أولاً.", None
13
 
14
  file_content = ""
15
  if uploaded_file is not None:
16
  try:
17
  file_content = process_uploaded_file(uploaded_file)
18
  except Exception as e:
19
- return f"❌ خطأ في قراءة الملف: {str(e)}", None
20
 
21
  combined_context = f"{file_content}\n\n{manual_text}".strip()
22
- if len(combined_context) < 10:
23
- return "⚠️ النص المقدم قصير جداً.", None
24
-
25
  try:
26
  agents_dict = create_agents(user_token)
 
27
  assistant_agents = [
28
  agents_dict["analyst"], agents_dict["style_guardian"],
29
  agents_dict["architect"], agents_dict["draft_writer"],
@@ -34,7 +33,7 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
34
  groupchat = autogen.GroupChat(
35
  agents=assistant_agents,
36
  messages=[],
37
- max_round=15,
38
  speaker_selection_method="auto"
39
  )
40
 
@@ -43,39 +42,39 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
43
  llm_config=agents_dict["editor"].llm_config
44
  )
45
 
46
- init_message = f"المسودة:\n{combined_context}"
47
- chat_result = agents_dict["editor"].initiate_chat(manager, message=init_message)
 
 
48
 
49
  final_story_text = chat_result.chat_history[-1]['content']
50
  output_file_path = export_to_docx(final_story_text)
51
 
52
  return final_story_text, output_file_path
 
53
  except Exception as e:
54
- return f"❌ خطأ تقني: {str(e)}", None
55
 
56
- # بناء الواجهة بشكل سليم
57
  with gr.Blocks(theme=gr.themes.Soft(), css="custom.css") as demo:
58
  with gr.Sidebar():
59
- gr.Markdown("# 🔑 تسجيل الدخول")
60
- # الزر يجب أن يكون داخل Blocks حصراً
61
- login_btn = gr.LoginButton()
62
-
63
  gr.Markdown("---")
64
- gr.Markdown("### 📂 الملفات")
65
- file_upload = gr.File(label="ارفع مسودة (.docx)", file_types=[".docx"])
66
- text_area = gr.Textbox(label="أفكار إضافية", lines=8)
67
- submit_btn = gr.Button("🚀 ابدأ العمل", variant="primary")
68
 
69
  with gr.Column():
70
- gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
71
- output_markdown = gr.Markdown(value="سجل دخولك ثم اضغط 'ابدأ العمل'")
72
- download_btn = gr.File(label="تحميل (.docx)")
73
 
74
- # الربط البرمجي (Gradio يمرر oauth_token تلقائياً)
75
  submit_btn.click(
76
  fn=run_novel_studio,
77
  inputs=[file_upload, text_area],
78
- outputs=[output_markdown, download_btn]
 
79
  )
80
 
81
  if __name__ == "__main__":
 
5
  from agents_config import create_agents
6
 
7
  def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | None):
8
+ # جلب التوكن من تسجيل الدخول
9
  user_token = oauth_token.token if oauth_token else os.getenv("HF_TOKEN")
10
 
11
  if not user_token:
12
+ return "⚠️ يرجى تسجيل الدخول أولاً عبر الزر في القائمة الجانبية.", None
13
 
14
  file_content = ""
15
  if uploaded_file is not None:
16
  try:
17
  file_content = process_uploaded_file(uploaded_file)
18
  except Exception as e:
19
+ return f"❌ خطأ في الملف: {str(e)}", None
20
 
21
  combined_context = f"{file_content}\n\n{manual_text}".strip()
22
+
 
 
23
  try:
24
  agents_dict = create_agents(user_token)
25
+ # إعداد فريق العمل
26
  assistant_agents = [
27
  agents_dict["analyst"], agents_dict["style_guardian"],
28
  agents_dict["architect"], agents_dict["draft_writer"],
 
33
  groupchat = autogen.GroupChat(
34
  agents=assistant_agents,
35
  messages=[],
36
+ max_round=12,
37
  speaker_selection_method="auto"
38
  )
39
 
 
42
  llm_config=agents_dict["editor"].llm_config
43
  )
44
 
45
+ chat_result = agents_dict["editor"].initiate_chat(
46
+ manager,
47
+ message=f"حلل وأكمل هذه الرواية:\n{combined_context}"
48
+ )
49
 
50
  final_story_text = chat_result.chat_history[-1]['content']
51
  output_file_path = export_to_docx(final_story_text)
52
 
53
  return final_story_text, output_file_path
54
+
55
  except Exception as e:
56
+ return f"❌ فشل النظام: {str(e)}", None
57
 
 
58
  with gr.Blocks(theme=gr.themes.Soft(), css="custom.css") as demo:
59
  with gr.Sidebar():
60
+ gr.Markdown("# 🔑 الدخول")
61
+ gr.LoginButton() # تسجيل الدخول عبر HF
 
 
62
  gr.Markdown("---")
63
+ file_upload = gr.File(label="ارفع المسودة", file_types=[".docx"])
64
+ text_area = gr.Textbox(label="تعليمات إضافية", lines=5)
65
+ submit_btn = gr.Button("🚀 إطلاق الوكلاء", variant="primary")
 
66
 
67
  with gr.Column():
68
+ gr.HTML("<h1 style='text-align:center;'>Ling-1T Studio</h1>")
69
+ output_markdown = gr.Markdown(value="بانتظار بدء العمل...")
70
+ download_btn = gr.File(label="تحميل الملف النهائي")
71
 
72
+ # الإصلاح الجوهري هنا بإضافة api_name=False
73
  submit_btn.click(
74
  fn=run_novel_studio,
75
  inputs=[file_upload, text_area],
76
+ outputs=[output_markdown, download_btn],
77
+ api_name=False
78
  )
79
 
80
  if __name__ == "__main__":