FECUOY's picture
Update app.py
e6e65b5 verified
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, user_token):
"""
الدالة الرئيسية: تستخدم التوكن المدخل يدوياً من قبل المستخدم.
"""
# التأكد من وجود التوكن
if not user_token or not user_token.startswith("hf_"):
return "⚠️ يرجى إدخال Hugging Face Token صحيح يبدأ بـ hf_.", None
# تعيين التوكن كمتغير بيئة للنظام
os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token
# 1. معالجة الملف والنص
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:
# 2. إنشاء الوكلاء بالتوكن الجديد
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"]
]
# 3. إعداد غرفة الدردشة
groupchat = autogen.GroupChat(
agents=assistant_agents,
messages=[],
max_round=12,
speaker_selection_method="auto"
)
manager = autogen.GroupChatManager(
groupchat=groupchat,
llm_config=agents_dict["editor"].llm_config
)
# 4. بدء العملية
init_message = f"المادة الخام للرواية:\n\n{combined_context}"
chat_result = agents_dict["editor"].initiate_chat(
manager,
message=init_message
)
final_text = chat_result.chat_history[-1]['content']
file_path = export_to_docx(final_text)
return final_text, file_path
except Exception as e:
# إذا ظهر خطأ 403 هنا، فهذا يعني أن توكن المستخدم نفسه لا يملك صلاحية الـ Inference
if "403" in str(e):
return "❌ خطأ 403: التوكن الخاص بك لا يملك صلاحية Inference API. تأكد من إعدادات التوكن في حسابك.", None
return f"❌ فشل النظام: {str(e)}", None
# بناء الواجهة
with gr.Blocks(theme=gr.themes.Soft(), css="custom.css") as demo:
with gr.Sidebar():
gr.Markdown("# 🔑 إعدادات الوصول")
user_token_input = gr.Textbox(
label="Hugging Face Token",
placeholder="hf_xxxxxxxxxxxx",
type="password"
)
gr.Markdown("---")
file_upload = gr.File(label="ارفع المسودة (.docx)", file_types=[".docx"])
text_area = gr.Textbox(label="تعليمات إضافية", lines=8)
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="تحميل المخطوطة النهائية")
# الربط البرمجي
submit_btn.click(
fn=run_novel_studio,
inputs=[file_upload, text_area, user_token_input],
outputs=[output_markdown, download_btn],
api_name=False
)
if __name__ == "__main__":
demo.launch()