FECUOY commited on
Commit
e6e65b5
·
verified ·
1 Parent(s): a92ceb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -29
app.py CHANGED
@@ -4,16 +4,18 @@ 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, 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
- # تعيين التوكن في البيئة لضمان توافق الوكلاء
15
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token
16
 
 
17
  file_content = ""
18
  if uploaded_file is not None:
19
  try:
@@ -22,12 +24,11 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
22
  return f"❌ خطأ في قراءة الملف: {str(e)}", None
23
 
24
  combined_context = f"{file_content}\n\n{manual_text}".strip()
25
-
26
  if len(combined_context) < 10:
27
- return "⚠️ النص المقدم قصير جداً، يرجى تقديم مسودة كافية.", None
28
 
29
  try:
30
- # إنشاء فريق الوكلاء
31
  agents_dict = create_agents(user_token)
32
 
33
  assistant_agents = [
@@ -37,10 +38,11 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
37
  agents_dict["psychologist"], agents_dict["critic"]
38
  ]
39
 
 
40
  groupchat = autogen.GroupChat(
41
  agents=assistant_agents,
42
  messages=[],
43
- max_round=15,
44
  speaker_selection_method="auto"
45
  )
46
 
@@ -49,44 +51,47 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
49
  llm_config=agents_dict["editor"].llm_config
50
  )
51
 
52
- init_message = f"إليك مادة الرواية الخام، ابدأوا المعالجة فوراً:\n\n{combined_context}"
53
-
54
  chat_result = agents_dict["editor"].initiate_chat(
55
  manager,
56
  message=init_message
57
  )
58
 
59
- # استخراج النص النهائي
60
- final_story_text = chat_result.chat_history[-1]['content']
61
- output_file_path = export_to_docx(final_story_text)
62
 
63
- return final_story_text, output_file_path
64
 
65
  except Exception as e:
 
 
 
66
  return f"❌ فشل النظام: {str(e)}", None
67
 
68
- # واجهة Gradio المصححة
69
- with gr.Blocks(theme=gr.themes.Soft(), css="custom.css", title="Ling-1T Novel Studio") as demo:
70
  with gr.Sidebar():
71
- gr.Markdown("# 🔐 الدخول")
72
- # تم إزالة scopes لحل خطأ TypeError في إصدار 5.13.0
73
- gr.LoginButton()
74
-
 
 
75
  gr.Markdown("---")
76
- gr.Markdown("### 📂 ملقم المسودة")
77
- file_upload = gr.File(label="ارفع مسودة (.docx)", file_types=[".docx"])
78
- text_area = gr.Textbox(label="تعليمات إضافية", lines=8, placeholder="اكتب توجهاتك الفنية هنا...")
79
  submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
80
 
81
  with gr.Column():
82
  gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
83
- output_markdown = gr.Markdown(value="**سجل دخولك ثم ارفع ملفك لبدء المعالجة.**")
84
  download_btn = gr.File(label="تحميل المخطوطة النهائية")
85
 
86
- # الربط البرمجي مع تعطيل api_name لتجنب أخطاء Gradio 5 المعروفة
87
  submit_btn.click(
88
  fn=run_novel_studio,
89
- inputs=[file_upload, text_area],
90
  outputs=[output_markdown, download_btn],
91
  api_name=False
92
  )
 
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
+ # التأكد من وجود التوكن
12
+ if not user_token or not user_token.startswith("hf_"):
13
+ return "⚠️ يرجى إدخال Hugging Face Token صحيح يبدأ بـ hf_.", None
14
 
15
+ # تعيين التوكن كمتغير بيئة للنظام
16
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token
17
 
18
+ # 1. معالجة الملف والنص
19
  file_content = ""
20
  if uploaded_file is not None:
21
  try:
 
24
  return f"❌ خطأ في قراءة الملف: {str(e)}", None
25
 
26
  combined_context = f"{file_content}\n\n{manual_text}".strip()
 
27
  if len(combined_context) < 10:
28
+ return "⚠️ يرجى تقديم مسودة أو نص كافٍ للعمل.", None
29
 
30
  try:
31
+ # 2. إنشاء الوكلاء بالتوكن الجديد
32
  agents_dict = create_agents(user_token)
33
 
34
  assistant_agents = [
 
38
  agents_dict["psychologist"], agents_dict["critic"]
39
  ]
40
 
41
+ # 3. إعداد غرفة الدردشة
42
  groupchat = autogen.GroupChat(
43
  agents=assistant_agents,
44
  messages=[],
45
+ max_round=12,
46
  speaker_selection_method="auto"
47
  )
48
 
 
51
  llm_config=agents_dict["editor"].llm_config
52
  )
53
 
54
+ # 4. بدء العملية
55
+ init_message = f"المادة الخام للرواية:\n\n{combined_context}"
56
  chat_result = agents_dict["editor"].initiate_chat(
57
  manager,
58
  message=init_message
59
  )
60
 
61
+ final_text = chat_result.chat_history[-1]['content']
62
+ file_path = export_to_docx(final_text)
 
63
 
64
+ return final_text, file_path
65
 
66
  except Exception as e:
67
+ # إذا ظهر خطأ 403 هنا، فهذا يعني أن توكن المستخدم نفسه لا يملك صلاحية الـ Inference
68
+ if "403" in str(e):
69
+ return "❌ خطأ 403: التوكن الخاص بك لا يملك صلاحية Inference API. تأكد من إعدادات التوكن في حسابك.", None
70
  return f"❌ فشل النظام: {str(e)}", None
71
 
72
+ # بناء الواجهة
73
+ with gr.Blocks(theme=gr.themes.Soft(), css="custom.css") as demo:
74
  with gr.Sidebar():
75
+ gr.Markdown("# 🔑 إعدادات الوصول")
76
+ user_token_input = gr.Textbox(
77
+ label="Hugging Face Token",
78
+ placeholder="hf_xxxxxxxxxxxx",
79
+ type="password"
80
+ )
81
  gr.Markdown("---")
82
+ file_upload = gr.File(label="ارفع المسودة (.docx)", file_types=[".docx"])
83
+ text_area = gr.Textbox(label="تعليمات إضافية", lines=8)
 
84
  submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
85
 
86
  with gr.Column():
87
  gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
88
+ output_markdown = gr.Markdown(value="أدخل التوكن الخاص بك ثم اضغط 'إطلاق' للبدء.")
89
  download_btn = gr.File(label="تحميل المخطوطة النهائية")
90
 
91
+ # الربط البرمجي
92
  submit_btn.click(
93
  fn=run_novel_studio,
94
+ inputs=[file_upload, text_area, user_token_input],
95
  outputs=[output_markdown, download_btn],
96
  api_name=False
97
  )