File size: 3,689 Bytes
ec7a35a
5ce1efa
9593a27
df506af
ec7a35a
 
7a54cf4
f31ceae
7a54cf4
 
 
f31ceae
bab741b
 
 
9593a27
bab741b
9593a27
f31ceae
bab741b
 
9593a27
bab741b
 
9593a27
 
f31ceae
bab741b
 
 
 
 
 
 
 
 
9593a27
bab741b
9593a27
7a54cf4
bab741b
9593a27
 
bab741b
 
 
9593a27
bab741b
f31ceae
9593a27
bab741b
 
 
5ce1efa
c91de59
bab741b
 
c91de59
bab741b
 
5ce1efa
bab741b
5ce1efa
f31ceae
bab741b
7a54cf4
f31ceae
 
7a54cf4
f31ceae
 
 
 
7a54cf4
f31ceae
7a54cf4
f31ceae
 
 
c91de59
7a54cf4
bab741b
 
7a54cf4
ca07a7b
 
c91de59
5ce1efa
c91de59
bab741b
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
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, oauth_token: gr.OAuthToken | None):
    # جلب التوكن تلقائياً من تسجيل الدخول أو من متغيرات البيئة
    user_token = oauth_token.token if oauth_token else os.getenv("HF_TOKEN")
    
    if not user_token:
        return "⚠️ يرجى تسجيل الدخول عبر Hugging Face أولاً للوصول إلى النماذج بصلاحياتك.", None

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

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

    try:
        # إنشاء فريق الوكلاء باستخدام التوكن الموثق
        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"]
        ]
        
        groupchat = autogen.GroupChat(
            agents=assistant_agents,
            messages=[],
            max_round=15,
            speaker_selection_method="auto"
        )
        
        manager = autogen.GroupChatManager(
            groupchat=groupchat,
            llm_config=agents_dict["editor"].llm_config
        )

        init_message = f"المسودة المطلوبة للمعالجة:\n{combined_context}"
        
        chat_result = agents_dict["editor"].initiate_chat(
            manager,
            message=init_message
        )
        
        final_story_text = chat_result.chat_history[-1]['content']
        output_file_path = export_to_docx(final_story_text)
        
        return final_story_text, output_file_path

    except Exception as e:
        return f"❌ فشل النظام في المعالجة: {str(e)}", None

# تصميم الواجهة مع دعم تسجيل الدخول
with gr.Blocks(theme=gr.themes.Soft(), title="Ling-1T Novel Studio") as demo:
    with gr.Sidebar():
        gr.Markdown("# 🔐 الوصول")
        gr.LoginButton() # إضافة زر تسجيل الدخول لحل مشكلة الـ 403
        
        gr.Markdown("---")
        gr.Markdown("### 🛠️ المدخلات")
        file_upload = gr.File(label="ارفع مسودة (.docx, .txt)", file_types=[".docx", ".txt"])
        text_area = gr.Textbox(label="تعليمات إضافية", lines=6)
        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="تحميل المخطوطة (.docx)")

    # ربط الأحداث مع إرسال oauth_token تلقائياً
    submit_btn.click(
        fn=run_novel_studio,
        inputs=[file_upload, text_area],
        outputs=[output_markdown, download_btn],
        api_name=False
    )

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