FECUOY commited on
Commit
2e00b65
·
verified ·
1 Parent(s): a70c8c5

Update agents_config.py

Browse files
Files changed (1) hide show
  1. agents_config.py +105 -49
agents_config.py CHANGED
@@ -1,54 +1,110 @@
 
1
  import autogen
 
 
 
2
 
3
- MODEL_MAP = {
4
- "Analyst": "Qwen/Qwen3-Next-80B-A3B-Instruct",
5
- "StyleGuardian": "openai/gpt-oss-120b",
6
- "Architect": "deepseek-ai/DeepSeek-V3.2-Exp",
7
- "DraftWriter": "deepcogito/cogito-v2-preview-llama-405B",
8
- "Humanizer": "deepcogito/cogito-671b-v2.1",
9
- "ContinuityGuard": "meta-llama/Llama-3.3-70B-Instruct",
10
- "Psychologist": "zai-org/GLM-4.7-FP8",
11
- "Critic": "Qwen/Qwen3-235B-A22B-Thinking-2507",
12
- "Editor": "inclusionAI/Ling-1T"
13
- }
14
-
15
- def get_llm_config(model_id, user_token):
16
- return {
17
- "config_list": [{
18
- "model": model_id,
19
- "api_key": user_token,
20
- "base_url": "https://router.huggingface.co/v1",
21
- "api_type": "openai",
22
- }],
23
- "temperature": 0.7,
24
- "cache_seed": None,
25
- }
26
-
27
- def create_agents(user_token):
28
- def mk_asst(name, msg, model_key):
29
- return autogen.AssistantAgent(
30
- name=name,
31
- system_message=msg,
32
- llm_config=get_llm_config(MODEL_MAP[model_key], user_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  )
34
 
35
- agents = {
36
- "analyst": mk_asst("Analyst",حلل نصوص.", "Analyst"),
37
- "style_guardian": mk_asst("Style_Guardian", "خبير أسلوب.", "StyleGuardian"),
38
- "architect": mk_asst("Architect", "مهندس حبكة.", "Architect"),
39
- "draft_writer": mk_asst("Draft_Writer", "كاتب مسودة.", "DraftWriter"),
40
- "humanizer": mk_asst("Humanizer", "خبير مشاعر.", "Humanizer"),
41
- "continuity_guard": mk_asst("Continuity_Guard", "مراقب استمرارية.", "ContinuityGuard"),
42
- "psychologist": mk_asst("Psychologist", "محلل نفسي.", "Psychologist"),
43
- "critic": mk_asst("Critic", "ناقد أدبي.", "Critic"),
44
- }
45
-
46
- agents["editor"] = autogen.UserProxyAgent(
47
- name="Editor_In_Chief",
48
- human_input_mode="NEVER",
49
- max_consecutive_auto_reply=10,
50
- code_execution_config=False,
51
- llm_config=get_llm_config(MODEL_MAP["Editor"], user_token),
52
- system_message="أنت رئيس التحرير، اجمع النتائج في نص واحد."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  )
54
- return agents
 
 
 
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, oauth_token: gr.OAuthToken | None):
8
+ """
9
+ الدالة الرئيسية: تستخدم oauth_token لحل مشكلة الصلاحيات 403.
10
+ """
11
+ # 1. جلب التوكن من تسجيل الدخول أو متغيرات البيئة
12
+ user_token = oauth_token.token if oauth_token else os.getenv("HF_TOKEN")
13
+
14
+ if not user_token:
15
+ return "⚠️ يرجى تسجيل الدخول أولاً عبر زر (Sign in with Hugging Face) لتمكين الوصول للنماذج.", None
16
+
17
+ # تعيين التوكن كمتغير بيئة لضمان عمل المكتبات التابعة
18
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token
19
+
20
+ # 2. معالجة المدخلات
21
+ file_content = ""
22
+ if uploaded_file is not None:
23
+ try:
24
+ file_content = process_uploaded_file(uploaded_file)
25
+ except Exception as e:
26
+ return f"❌ خطأ في قراءة الملف: {str(e)}", None
27
+
28
+ combined_context = f"{file_content}\n\n{manual_text}".strip()
29
+
30
+ if len(combined_context) < 10:
31
+ return "⚠️ النص المقدم قصير جداً، يرجى تقديم مسودة كافية.", None
32
+
33
+ try:
34
+ # 3. إنشاء فريق الوكلاء باستخدام التوكن الموثق
35
+ agents_dict = create_agents(user_token)
36
+
37
+ assistant_agents = [
38
+ agents_dict["analyst"], agents_dict["style_guardian"],
39
+ agents_dict["architect"], agents_dict["draft_writer"],
40
+ agents_dict["humanizer"], agents_dict["continuity_guard"],
41
+ agents_dict["psychologist"], agents_dict["critic"]
42
+ ]
43
+
44
+ # إعداد محادثة المجموعة
45
+ groupchat = autogen.GroupChat(
46
+ agents=assistant_agents,
47
+ messages=[],
48
+ max_round=15,
49
+ speaker_selection_method="auto"
50
+ )
51
+
52
+ manager = autogen.GroupChatManager(
53
+ groupchat=groupchat,
54
+ llm_config=agents_dict["editor"].llm_config
55
  )
56
 
57
+ # 4. بدء التنفيذ
58
+ init_message = f"إليك المادة الخام للرواية، ابدأوا المعالجة الجماعية فوراً:\n\n{combined_context}"
59
+
60
+ chat_result = agents_dict["editor"].initiate_chat(
61
+ manager,
62
+ message=init_message
63
+ )
64
+
65
+ # استخراج النص النهائي من آخر رسالة لرئيس التحرير
66
+ final_story_text = chat_result.chat_history[-1]['content']
67
+ output_file_path = export_to_docx(final_story_text)
68
+
69
+ return final_story_text, output_file_path
70
+
71
+ except Exception as e:
72
+ return f"❌ فشل النظام ��ي المعالجة: {str(e)}", None
73
+
74
+ # إعداد واجهة Gradio
75
+ with gr.Blocks(theme=gr.themes.Soft(), css="custom.css", title="Ling-1T Novel Studio") as demo:
76
+ with gr.Sidebar():
77
+ gr.Markdown("# 🔐 بوابة الوصول")
78
+ # حل مشكلة 403: طلب صلاحية Inference API
79
+ gr.LoginButton(scopes=["inference-api"])
80
+
81
+ gr.Markdown("---")
82
+ gr.Markdown("### 📂 ملقم المسودة")
83
+ file_upload = gr.File(label="ارفع مسودة (.docx)", file_types=[".docx"])
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():
92
+ gr.HTML("""
93
+ <div style='text-align:center;'>
94
+ <h1>🖋️ Ling-1T Novel Production Studio</h1>
95
+ <p>نظام وكلاء ذكاء اصطناعي لإنتاج الروايات العربية</p>
96
+ </div>
97
+ """)
98
+ output_markdown = gr.Markdown(value="**بانتظار تسجيل الدخول وبدء العملية...**")
99
+ download_btn = gr.File(label="تحميل المخطوطة النهائية (.docx)")
100
+
101
+ # ربط الأحداث مع تعطيل API Name لتجنب خطأ Schema في Gradio 5
102
+ submit_btn.click(
103
+ fn=run_novel_studio,
104
+ inputs=[file_upload, text_area],
105
+ outputs=[output_markdown, download_btn],
106
+ api_name=False
107
  )
108
+
109
+ if __name__ == "__main__":
110
+ demo.launch()