1yahoo's picture
Update app.py
f31ceae verified
raw
history blame
3.69 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 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"❌ خطأ في قراءة ملف docx: {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() # إضافة زر تسجيل الدخول لحل مشكلة الـ 403
gr.Markdown("---")
gr.Markdown("### 🛠️ المدخلات")
file_upload = gr.File(label="ارفع مسودة (.docx, .txt)", file_types=[".docx", ".txt"])
text_area = gr.Textbox(label="تعليمات إضافية", lines=6)
submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
with gr.Column():
gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
output_markdown = gr.Markdown(value="ستظهر النتائج هنا بعد تسجيل الدخول وبدء المعالجة...")
download_btn = gr.File(label="تحميل المخطوطة (.docx)")
# ربط الأحداث مع إرسال oauth_token تلقائياً
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()