File size: 4,029 Bytes
9701b04
5ce1efa
9701b04
 
 
ec7a35a
e6e65b5
 
 
 
 
 
 
9701b04
e6e65b5
0678ff7
 
e6e65b5
9701b04
 
 
 
 
0678ff7
9701b04
 
0678ff7
e6e65b5
0678ff7
9701b04
e6e65b5
9701b04
0678ff7
9701b04
 
 
 
 
 
 
e6e65b5
9701b04
 
 
e6e65b5
9701b04
9593a27
9701b04
 
 
 
 
 
e6e65b5
 
af1b71a
 
0678ff7
af1b71a
9701b04
e6e65b5
 
9701b04
e6e65b5
af1b71a
9701b04
e6e65b5
 
 
a92ceb9
9701b04
e6e65b5
 
9701b04
e6e65b5
 
 
 
 
 
9701b04
e6e65b5
 
0678ff7
9701b04
 
a92ceb9
e6e65b5
a92ceb9
bab741b
e6e65b5
9701b04
 
e6e65b5
af1b71a
0678ff7
c91de59
5ce1efa
9701b04
 
1
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gradio as gr
import autogen
import os
from utils import process_uploaded_file, export_to_docx
from agents_config import create_agents

def run_novel_studio(uploaded_file, manual_text, user_token):
    """
    الدالة الرئيسية: تستخدم التوكن المدخل يدوياً من قبل المستخدم.
    """
    # التأكد من وجود التوكن
    if not user_token or not user_token.startswith("hf_"):
        return "⚠️ يرجى إدخال Hugging Face Token صحيح يبدأ بـ hf_.", None

    # تعيين التوكن كمتغير بيئة للنظام
    os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token

    # 1. معالجة الملف والنص
    file_content = ""
    if uploaded_file is not None:
        try:
            file_content = process_uploaded_file(uploaded_file)
        except Exception as e:
            return f"❌ خطأ في قراءة الملف: {str(e)}", None

    combined_context = f"{file_content}\n\n{manual_text}".strip()
    if len(combined_context) < 10:
        return "⚠️ يرجى تقديم مسودة أو نص كافٍ للعمل.", None

    try:
        # 2. إنشاء الوكلاء بالتوكن الجديد
        agents_dict = create_agents(user_token)
        
        assistant_agents = [
            agents_dict["analyst"], agents_dict["style_guardian"],
            agents_dict["architect"], agents_dict["draft_writer"],
            agents_dict["humanizer"], agents_dict["continuity_guard"],
            agents_dict["psychologist"], agents_dict["critic"]
        ]
        
        # 3. إعداد غرفة الدردشة
        groupchat = autogen.GroupChat(
            agents=assistant_agents,
            messages=[],
            max_round=12,
            speaker_selection_method="auto"
        )
        
        manager = autogen.GroupChatManager(
            groupchat=groupchat,
            llm_config=agents_dict["editor"].llm_config
        )

        # 4. بدء العملية
        init_message = f"المادة الخام للرواية:\n\n{combined_context}"
        chat_result = agents_dict["editor"].initiate_chat(
            manager,
            message=init_message
        )
        
        final_text = chat_result.chat_history[-1]['content']
        file_path = export_to_docx(final_text)
        
        return final_text, file_path

    except Exception as e:
        # إذا ظهر خطأ 403 هنا، فهذا يعني أن توكن المستخدم نفسه لا يملك صلاحية الـ Inference
        if "403" in str(e):
            return "❌ خطأ 403: التوكن الخاص بك لا يملك صلاحية Inference API. تأكد من إعدادات التوكن في حسابك.", None
        return f"❌ فشل النظام: {str(e)}", None

# بناء الواجهة
with gr.Blocks(theme=gr.themes.Soft(), css="custom.css") as demo:
    with gr.Sidebar():
        gr.Markdown("# 🔑 إعدادات الوصول")
        user_token_input = gr.Textbox(
            label="Hugging Face Token",
            placeholder="hf_xxxxxxxxxxxx",
            type="password"
        )
        gr.Markdown("---")
        file_upload = gr.File(label="ارفع المسودة (.docx)", file_types=[".docx"])
        text_area = gr.Textbox(label="تعليمات إضافية", lines=8)
        submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
            
    with gr.Column():
        gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
        output_markdown = gr.Markdown(value="أدخل التوكن الخاص بك ثم اضغط 'إطلاق' للبدء.")
        download_btn = gr.File(label="تحميل المخطوطة النهائية")

    # الربط البرمجي
    submit_btn.click(
        fn=run_novel_studio,
        inputs=[file_upload, text_area, user_token_input],
        outputs=[output_markdown, download_btn],
        api_name=False
    )

if __name__ == "__main__":
    demo.launch()