| 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"❌ خطأ في قراءة الملف: {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() |
| |
| gr.Markdown("### 📂 ملقم المسودة") |
| file_upload = gr.File(label="ملف الرواية", file_types=[".docx", ".txt"]) |
| text_area = gr.Textbox(label="تعليمات إضافية", lines=5) |
| submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary") |
|
|
| with gr.Column(): |
| gr.HTML("<h1 style='text-align:center;'>Ling-1T Novel Production Studio</h1>") |
| output_markdown = gr.Markdown(value="بانتظار بدء العمل...") |
| download_btn = gr.File(label="تحميل المخطوطة النهائية (.docx)") |
|
|
| |
| 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() |
| |