File size: 2,846 Bytes
ec7a35a
5ce1efa
8ac744a
df506af
ec7a35a
 
8ac744a
 
 
ac0c812
8ac744a
ac0c812
 
8ac744a
 
 
 
ac0c812
8ac744a
 
 
ec7a35a
5ce1efa
8ac744a
 
5ce1efa
ac0c812
5ce1efa
8ac744a
5ce1efa
ac0c812
c91de59
8ac744a
 
 
 
 
c91de59
ac0c812
8ac744a
c91de59
8ac744a
5ce1efa
8ac744a
5ce1efa
8ac744a
 
 
ec7a35a
 
ac0c812
8ac744a
 
 
 
 
df506af
ac0c812
8ac744a
 
c91de59
8ac744a
ac0c812
8ac744a
 
 
 
c91de59
5ce1efa
c91de59
8ac744a
 
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
import gradio as gr
import autogen
import os
from utils import process_uploaded_file, export_to_docx
from agents_config import create_agents

def start_novel_engine(file_obj, extra_text, user_token):
    if not user_token:
        return "⚠️ يرجى إدخال التوكن الخاص بك.", None
    
    # 1. استخراج النص من الملف
    context = ""
    if file_obj is not None:
        try:
            context = process_uploaded_file(file_obj)
        except Exception as e:
            return f"❌ خطأ في الملف: {str(e)}", None
    
    full_prompt = f"{context}\n\n{extra_text}".strip()
    if len(full_prompt) < 10:
        return "⚠️ النص قصير جدًا للبدء.", None

    try:
        # 2. تشغيل الوكلاء
        agents = create_agents(user_token)
        groupchat = autogen.GroupChat(
            agents=[v for k, v in agents.items() if k != "editor"],
            messages=[],
            max_round=10
        )
        manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=agents["editor"].llm_config)
        
        # 3. التواصل مع Ling-1T
        chat_result = agents["editor"].initiate_chat(
            manager, 
            message=f"قم بصياغة الفصل التالي وتنسيقه أدبيًا:\n\n{full_prompt}"
        )
        
        final_text = chat_result.chat_history[-1]['content']
        docx_path = export_to_docx(final_text)
        
        return final_text, docx_path
    except Exception as e:
        return f"❌ خطأ أثناء الإنتاج: {str(e)}", None

# واجهة مستقرة (بدون SSR وبدون مسببات الخطأ)
with gr.Blocks(analytics_enabled=False) as demo:
    gr.Markdown("# 🖋️ Ling-1T Novel Studio")
    
    with gr.Row():
        with gr.Column():
            hf_token = gr.Textbox(label="Hugging Face Token", type="password")
            # إزالة أي خصائص إضافية من الملف لضمان الاستقرار
            file_input = gr.File(label="Upload DOCX/TXT")
            instruction = gr.Textbox(label="Special Instructions", lines=3)
            run_btn = gr.Button("🚀 Start Writing", variant="primary")
            
        with gr.Column():
            output_display = gr.Textbox(label="Manuscript Preview", lines=12)
            download_link = gr.File(label="Download Final DOCX")

    # القضاء على الخطأ عبر منع الـ API تمامًا لهذا الحدث
    run_btn.click(
        fn=start_novel_engine,
        inputs=[file_input, instruction, hf_token],
        outputs=[output_display, download_link],
        api_name=False  # هذا السطر يمنع Gradio من فحص المكونات برمجياً
    )

if __name__ == "__main__":
    # تشغيل مباشر وبسيط
    demo.launch(show_api=False)