FECUOY commited on
Commit
7a54cf4
·
verified ·
1 Parent(s): ca07a7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -50
app.py CHANGED
@@ -4,16 +4,19 @@ 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
- if not user_token or not user_token.startswith("hf_"):
9
- return "⚠️ يرجى إدخال Hugging Face Token صحيح (يبدأ بـ hf_).", None
 
 
 
10
 
11
  file_content = ""
12
  if uploaded_file is not None:
13
  try:
14
  file_content = process_uploaded_file(uploaded_file)
15
  except Exception as e:
16
- return f"❌ خطأ في قراءة ملف docx: {str(e)}", None
17
 
18
  combined_context = f"{file_content}\n\n{manual_text}".strip()
19
 
@@ -21,6 +24,7 @@ def run_novel_studio(uploaded_file, manual_text, user_token):
21
  return "⚠️ المحتوى المقدم غير كافٍ. يرجى رفع ملف أو كتابة مسودة.", None
22
 
23
  try:
 
24
  agents_dict = create_agents(user_token)
25
 
26
  assistant_agents = [
@@ -33,7 +37,7 @@ def run_novel_studio(uploaded_file, manual_text, user_token):
33
  groupchat = autogen.GroupChat(
34
  agents=assistant_agents,
35
  messages=[],
36
- max_round=12,
37
  speaker_selection_method="auto"
38
  )
39
 
@@ -42,11 +46,7 @@ def run_novel_studio(uploaded_file, manual_text, user_token):
42
  llm_config=agents_dict["editor"].llm_config
43
  )
44
 
45
- init_message = f"""بصفتي رئيس التحرير، أضع بين أيديكم هذه المسودة.
46
- المطلوب: تحليلها، إكمال أحداث الفصل التالي، وتنسيقها أدبياً بشكل احترافي.
47
-
48
- المسودة:
49
- {combined_context}"""
50
 
51
  chat_result = agents_dict["editor"].initiate_chat(
52
  manager,
@@ -62,52 +62,29 @@ def run_novel_studio(uploaded_file, manual_text, user_token):
62
  return f"❌ فشل النظام في المعالجة: {str(e)}", None
63
 
64
  with gr.Blocks(theme=gr.themes.Soft(), title="Ling-1T Novel Studio") as demo:
65
- gr.HTML("""
66
- <div style="text-align: center;">
67
- <h1>🖋️ Ling-1T Novel Production Studio</h1>
68
- <p>نظام ذكاء اصطناعي متعدد الوكلاء لإنتاج وتنسيق الروايات العربية</p>
69
- </div>
70
- """)
71
-
72
- with gr.Row():
73
- with gr.Column(scale=1):
74
- gr.Markdown("### 🛠️ إعدادات المدخلات")
75
- token_input = gr.Textbox(
76
- label="Hugging Face Token",
77
- type="password",
78
- placeholder="hf_..."
79
- )
80
- file_upload = gr.File(
81
- label="ارفع مسودة الرواية (.docx, .txt)",
82
- file_types=[".docx", ".txt"]
83
- )
84
- text_area = gr.Textbox(
85
- label="تعليمات إضافية أو نص تكميلي",
86
- lines=6,
87
- placeholder="اكتب هنا إذا كنت تريد توجيه الوكلاء لمسار معين..."
88
- )
89
- submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
90
-
91
- with gr.Column(scale=2):
92
- gr.Markdown("### 📖 المخطوطة النهائية")
93
- output_markdown = gr.Markdown(
94
- label="معاينة النص",
95
- value="ستظهر الرواية هنا بعد اكتمال نقاش الوكلاء..."
96
- )
97
- download_btn = gr.File(label="تحميل ملف الوورد المنسق")
98
 
99
- # التعديل ��نا: إضافة api_name=False لإصلاح خطأ Gradio 5.13
100
  submit_btn.click(
101
  fn=run_novel_studio,
102
- inputs=[file_upload, text_area, token_input],
103
  outputs=[output_markdown, download_btn],
104
  api_name=False
105
  )
106
 
107
- gr.Markdown("""
108
- ---
109
- **ملاحظة:** هذا النظام يستخدم 9 نماذج متخصصة تعمل بالتوازي. قد تستغرق العملية من 1-3 دقائق حسب طول النص.
110
- """)
111
-
112
  if __name__ == "__main__":
113
  demo.launch()
 
 
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 "⚠️ يرجى تسجيل الدخول عبر 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
 
 
24
  return "⚠️ المحتوى المقدم غير كافٍ. يرجى رفع ملف أو كتابة مسودة.", None
25
 
26
  try:
27
+ # إنشاء الوكلاء باستخدام التوكن المجلوب تلقائياً
28
  agents_dict = create_agents(user_token)
29
 
30
  assistant_agents = [
 
37
  groupchat = autogen.GroupChat(
38
  agents=assistant_agents,
39
  messages=[],
40
+ max_round=15,
41
  speaker_selection_method="auto"
42
  )
43
 
 
46
  llm_config=agents_dict["editor"].llm_config
47
  )
48
 
49
+ init_message = f"المسودة المطلوبة:\n{combined_context}"
 
 
 
 
50
 
51
  chat_result = agents_dict["editor"].initiate_chat(
52
  manager,
 
62
  return f"❌ فشل النظام في المعالجة: {str(e)}", None
63
 
64
  with gr.Blocks(theme=gr.themes.Soft(), title="Ling-1T Novel Studio") as demo:
65
+ with gr.Sidebar():
66
+ gr.Markdown("# 🖋️ التحكم")
67
+ # إضافة زر تسجيل الدخول
68
+ gr.LoginButton()
69
+
70
+ gr.Markdown("### 📂 ملقم المسودة")
71
+ file_upload = gr.File(label="ملف الرواية", file_types=[".docx", ".txt"])
72
+ text_area = gr.Textbox(label="تعليمات إضافية", lines=5)
73
+ submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
74
+
75
+ with gr.Column():
76
+ gr.HTML("<h1 style='text-align:center;'>Ling-1T Novel Production Studio</h1>")
77
+ output_markdown = gr.Markdown(value="بانتظار بدء العمل...")
78
+ download_btn = gr.File(label="تحميل المخطوطة النهائية (.docx)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ # ربط الأحداث مع إرسال oauth_token تلقائياً
81
  submit_btn.click(
82
  fn=run_novel_studio,
83
+ inputs=[file_upload, text_area],
84
  outputs=[output_markdown, download_btn],
85
  api_name=False
86
  )
87
 
 
 
 
 
 
88
  if __name__ == "__main__":
89
  demo.launch()
90
+