| 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 |
| |
| |
| 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: |
| |
| 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) |
| |
| |
| 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 |
|
|
| |
| 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") |
|
|
| |
| run_btn.click( |
| fn=start_novel_engine, |
| inputs=[file_input, instruction, hf_token], |
| outputs=[output_display, download_link], |
| api_name=False |
| ) |
|
|
| if __name__ == "__main__": |
| |
| demo.launch(show_api=False) |
|
|