1yahoo's picture
Update app.py
8ac744a verified
raw
history blame
2.85 kB
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)