Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,24 +5,35 @@ from utils import process_uploaded_file, export_to_docx
|
|
| 5 |
from agents_config import create_agents
|
| 6 |
|
| 7 |
def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | None):
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
user_token = oauth_token.token if oauth_token else os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
if not user_token:
|
| 12 |
-
return "⚠️ يرجى تسجيل الدخول أولاً عبر
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
file_content = ""
|
| 15 |
if uploaded_file is not None:
|
| 16 |
try:
|
| 17 |
file_content = process_uploaded_file(uploaded_file)
|
| 18 |
except Exception as e:
|
| 19 |
-
return f"❌ خطأ في الملف: {str(e)}", None
|
| 20 |
|
| 21 |
combined_context = f"{file_content}\n\n{manual_text}".strip()
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
|
|
|
| 24 |
agents_dict = create_agents(user_token)
|
| 25 |
-
|
| 26 |
assistant_agents = [
|
| 27 |
agents_dict["analyst"], agents_dict["style_guardian"],
|
| 28 |
agents_dict["architect"], agents_dict["draft_writer"],
|
|
@@ -30,10 +41,11 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
|
|
| 30 |
agents_dict["psychologist"], agents_dict["critic"]
|
| 31 |
]
|
| 32 |
|
|
|
|
| 33 |
groupchat = autogen.GroupChat(
|
| 34 |
agents=assistant_agents,
|
| 35 |
messages=[],
|
| 36 |
-
max_round=
|
| 37 |
speaker_selection_method="auto"
|
| 38 |
)
|
| 39 |
|
|
@@ -42,39 +54,57 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
|
|
| 42 |
llm_config=agents_dict["editor"].llm_config
|
| 43 |
)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
chat_result = agents_dict["editor"].initiate_chat(
|
| 46 |
manager,
|
| 47 |
-
message=
|
| 48 |
)
|
| 49 |
|
|
|
|
| 50 |
final_story_text = chat_result.chat_history[-1]['content']
|
| 51 |
output_file_path = export_to_docx(final_story_text)
|
| 52 |
|
| 53 |
return final_story_text, output_file_path
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
-
return f"❌ فشل النظام: {str(e)}", None
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
with gr.Sidebar():
|
| 60 |
-
gr.Markdown("#
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
gr.Markdown("---")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
with gr.Column():
|
| 68 |
-
gr.HTML("
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
# ال
|
| 73 |
submit_btn.click(
|
| 74 |
fn=run_novel_studio,
|
| 75 |
inputs=[file_upload, text_area],
|
| 76 |
outputs=[output_markdown, download_btn],
|
| 77 |
-
api_name=False
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from agents_config import create_agents
|
| 6 |
|
| 7 |
def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | None):
|
| 8 |
+
"""
|
| 9 |
+
الدالة الرئيسية التي تستخدم OAuth Token لحل مشكلة الصلاحيات 403 بشكل تلقائي.
|
| 10 |
+
"""
|
| 11 |
+
# 1. جلب التوكن من جلسة تسجيل الدخول (OAuth)
|
| 12 |
user_token = oauth_token.token if oauth_token else os.getenv("HF_TOKEN")
|
| 13 |
|
| 14 |
if not user_token:
|
| 15 |
+
return "⚠️ يرجى تسجيل الدخول أولاً عبر زر (Sign in with Hugging Face) لتمكين الوصول للنماذج بصلاحياتك.", None
|
| 16 |
|
| 17 |
+
# تعيين التوكن في متغيرات البيئة لضمان عمل المكتبات التابعة
|
| 18 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token
|
| 19 |
+
|
| 20 |
+
# 2. معالجة المدخلات (الملف المرفوع + النص اليدوي)
|
| 21 |
file_content = ""
|
| 22 |
if uploaded_file is not None:
|
| 23 |
try:
|
| 24 |
file_content = process_uploaded_file(uploaded_file)
|
| 25 |
except Exception as e:
|
| 26 |
+
return f"❌ خطأ في قراءة الملف: {str(e)}", None
|
| 27 |
|
| 28 |
combined_context = f"{file_content}\n\n{manual_text}".strip()
|
| 29 |
|
| 30 |
+
if len(combined_context) < 10:
|
| 31 |
+
return "⚠️ النص المقدم قصير جداً، يرجى تقديم مسودة كافية للوكلاء.", None
|
| 32 |
+
|
| 33 |
try:
|
| 34 |
+
# 3. إنشاء فريق الوكلاء باستخدام التوكن الموثق
|
| 35 |
agents_dict = create_agents(user_token)
|
| 36 |
+
|
| 37 |
assistant_agents = [
|
| 38 |
agents_dict["analyst"], agents_dict["style_guardian"],
|
| 39 |
agents_dict["architect"], agents_dict["draft_writer"],
|
|
|
|
| 41 |
agents_dict["psychologist"], agents_dict["critic"]
|
| 42 |
]
|
| 43 |
|
| 44 |
+
# إعداد محادثة المجموعة (Group Chat)
|
| 45 |
groupchat = autogen.GroupChat(
|
| 46 |
agents=assistant_agents,
|
| 47 |
messages=[],
|
| 48 |
+
max_round=15, # عدد الجولات لضمان عمق النقاش
|
| 49 |
speaker_selection_method="auto"
|
| 50 |
)
|
| 51 |
|
|
|
|
| 54 |
llm_config=agents_dict["editor"].llm_config
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# 4. بدء التنفيذ (رئيس التحرير يبدأ النقاش)
|
| 58 |
+
init_message = f"إليك المادة الخام للرواية، ابدأوا المعالجة الجماعية فوراً:\n\n{combined_context}"
|
| 59 |
+
|
| 60 |
chat_result = agents_dict["editor"].initiate_chat(
|
| 61 |
manager,
|
| 62 |
+
message=init_message
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# استخراج النص النهائي من آخر رسالة في المحادثة
|
| 66 |
final_story_text = chat_result.chat_history[-1]['content']
|
| 67 |
output_file_path = export_to_docx(final_story_text)
|
| 68 |
|
| 69 |
return final_story_text, output_file_path
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
return f"❌ فشل النظام في المعالجة: {str(e)}", None
|
| 73 |
|
| 74 |
+
# إعداد واجهة Gradio الاحترافية
|
| 75 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="custom.css", title="Ling-1T Novel Studio") as demo:
|
| 76 |
with gr.Sidebar():
|
| 77 |
+
gr.Markdown("# 🔐 بوابة الوصول")
|
| 78 |
+
# حل مشكلة 403: طلب صلاحية الاستدلال تلقائياً من المستخدم
|
| 79 |
+
gr.LoginButton(scopes=["inference-api"])
|
| 80 |
+
|
| 81 |
gr.Markdown("---")
|
| 82 |
+
gr.Markdown("### 📂 ملقم المسودة")
|
| 83 |
+
file_upload = gr.File(label="ارفع مسودة الرواية (.docx)", file_types=[".docx"])
|
| 84 |
+
text_area = gr.Textbox(
|
| 85 |
+
label="تعليمات إضافية",
|
| 86 |
+
lines=8,
|
| 87 |
+
placeholder="اكتب هنا توجهاتك (مثال: اجعل الأسلوب سوداوياً، أو ركز على الحوارات الحميمية...)"
|
| 88 |
+
)
|
| 89 |
+
submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
|
| 90 |
|
| 91 |
with gr.Column():
|
| 92 |
+
gr.HTML("""
|
| 93 |
+
<div style='text-align:center;'>
|
| 94 |
+
<h1>🖋️ Ling-1T Novel Production Studio</h1>
|
| 95 |
+
<p style='color: #666;'>نظام ذكاء اصطناعي متعدد الوكلاء لإنتاج وتنسيق الروايات العربية</p>
|
| 96 |
+
</div>
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
output_markdown = gr.Markdown(value="**بانتظار تسجيل الدخول وبدء العملية...**")
|
| 100 |
+
download_btn = gr.File(label="تحميل المخطوطة النهائية (.docx)")
|
| 101 |
|
| 102 |
+
# ربط الأحداث مع تعطيل api_name لتجنب خطأ Schema في Gradio 5
|
| 103 |
submit_btn.click(
|
| 104 |
fn=run_novel_studio,
|
| 105 |
inputs=[file_upload, text_area],
|
| 106 |
outputs=[output_markdown, download_btn],
|
| 107 |
+
api_name=False
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|