Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,82 @@
|
|
|
|
|
| 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 |
-
system_message=msg,
|
| 36 |
-
llm_config=get_llm_config(MODEL_MAP[model_key], user_token)
|
| 37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
"humanizer": mk_asst("Humanizer", "أنت من يبث الروح والمشاعر في الحوارات والشخصيات.", "Humanizer"),
|
| 45 |
-
"continuity_guard": mk_asst("Continuity_Guard", "أنت مراقب المنطق ومنع التناقضات.", "ContinuityGuard"),
|
| 46 |
-
"psychologist": mk_asst("Psychologist", "أنت محلل الأعماق النفسية للشخصيات.", "Psychologist"),
|
| 47 |
-
"critic": mk_asst("Critic", "أنت الناقد الذي يراجع الجودة النهائية.", "Critic"),
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
# رئيس التحرير (Editor) - تم ضبطه للعمل التلقائي التام
|
| 51 |
-
agents["editor"] = autogen.UserProxyAgent(
|
| 52 |
-
name="Editor_In_Chief",
|
| 53 |
-
human_input_mode="NEVER", # منع التوقف لانتظار إدخال يدوي
|
| 54 |
-
max_consecutive_auto_reply=15,
|
| 55 |
-
code_execution_config=False,
|
| 56 |
-
llm_config=get_llm_config(MODEL_MAP["Editor"], user_token),
|
| 57 |
-
system_message="""أنت رئيس التحرير. مهمتك هي استلام الأفكار، الإشراف على نقاش الوكلاء،
|
| 58 |
-
ثم تقديم النص الروائي النهائي في قالب منسق يحتوي على: العنوان، رقم الفصل، والمحتوى السردي."""
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
| 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 |
+
user_token = oauth_token.token if oauth_token else os.getenv("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
if not user_token:
|
| 12 |
+
return "⚠️ يرجى تسجيل الدخول عبر زر (Sign in with Hugging Face) أولاً.", None
|
| 13 |
+
|
| 14 |
+
file_content = ""
|
| 15 |
+
if uploaded_file is not None:
|
| 16 |
+
try:
|
| 17 |
+
file_content = process_uploaded_file(uploaded_file)
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"❌ خطأ في قراءة الملف: {str(e)}", None
|
| 20 |
+
|
| 21 |
+
combined_context = f"{file_content}\n\n{manual_text}".strip()
|
| 22 |
+
if len(combined_context) < 10:
|
| 23 |
+
return "⚠️ النص المقدم قصير جداً.", None
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
agents_dict = create_agents(user_token)
|
| 27 |
+
assistant_agents = [
|
| 28 |
+
agents_dict["analyst"], agents_dict["style_guardian"],
|
| 29 |
+
agents_dict["architect"], agents_dict["draft_writer"],
|
| 30 |
+
agents_dict["humanizer"], agents_dict["continuity_guard"],
|
| 31 |
+
agents_dict["psychologist"], agents_dict["critic"]
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
groupchat = autogen.GroupChat(
|
| 35 |
+
agents=assistant_agents,
|
| 36 |
+
messages=[],
|
| 37 |
+
max_round=15,
|
| 38 |
+
speaker_selection_method="auto"
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
+
|
| 41 |
+
manager = autogen.GroupChatManager(
|
| 42 |
+
groupchat=groupchat,
|
| 43 |
+
llm_config=agents_dict["editor"].llm_config
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
init_message = f"المسودة:\n{combined_context}"
|
| 47 |
+
chat_result = agents_dict["editor"].initiate_chat(manager, message=init_message)
|
| 48 |
+
|
| 49 |
+
final_story_text = chat_result.chat_history[-1]['content']
|
| 50 |
+
output_file_path = export_to_docx(final_story_text)
|
| 51 |
+
|
| 52 |
+
return final_story_text, output_file_path
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"❌ خطأ تقني: {str(e)}", None
|
| 55 |
+
|
| 56 |
+
# بناء الواجهة بشكل سليم
|
| 57 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="custom.css") as demo:
|
| 58 |
+
with gr.Sidebar():
|
| 59 |
+
gr.Markdown("# 🔑 تسجيل الدخول")
|
| 60 |
+
# الزر يجب أن يكون داخل Blocks حصراً
|
| 61 |
+
login_btn = gr.LoginButton()
|
| 62 |
+
|
| 63 |
+
gr.Markdown("---")
|
| 64 |
+
gr.Markdown("### 📂 الملفات")
|
| 65 |
+
file_upload = gr.File(label="ارفع مسودة (.docx)", file_types=[".docx"])
|
| 66 |
+
text_area = gr.Textbox(label="أفكار إضافية", lines=8)
|
| 67 |
+
submit_btn = gr.Button("🚀 ابدأ العمل", variant="primary")
|
| 68 |
+
|
| 69 |
+
with gr.Column():
|
| 70 |
+
gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
|
| 71 |
+
output_markdown = gr.Markdown(value="سجل دخولك ثم اضغط 'ابدأ العمل'")
|
| 72 |
+
download_btn = gr.File(label="تحميل (.docx)")
|
| 73 |
|
| 74 |
+
# الربط البرمجي (Gradio يمرر oauth_token تلقائياً)
|
| 75 |
+
submit_btn.click(
|
| 76 |
+
fn=run_novel_studio,
|
| 77 |
+
inputs=[file_upload, text_area],
|
| 78 |
+
outputs=[output_markdown, download_btn]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|