Spaces:
Sleeping
Sleeping
Update agents_config.py
Browse files- agents_config.py +105 -49
agents_config.py
CHANGED
|
@@ -1,54 +1,110 @@
|
|
|
|
|
| 1 |
import autogen
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
"
|
| 5 |
-
|
| 6 |
-
"
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
-
|
|
|
|
|
|
|
|
|
| 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()
|