Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,18 @@ import os
|
|
| 4 |
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,
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
# تعيين التوكن
|
| 15 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token
|
| 16 |
|
|
|
|
| 17 |
file_content = ""
|
| 18 |
if uploaded_file is not None:
|
| 19 |
try:
|
|
@@ -22,12 +24,11 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
|
|
| 22 |
return f"❌ خطأ في قراءة الملف: {str(e)}", None
|
| 23 |
|
| 24 |
combined_context = f"{file_content}\n\n{manual_text}".strip()
|
| 25 |
-
|
| 26 |
if len(combined_context) < 10:
|
| 27 |
-
return "⚠️
|
| 28 |
|
| 29 |
try:
|
| 30 |
-
# إنشاء
|
| 31 |
agents_dict = create_agents(user_token)
|
| 32 |
|
| 33 |
assistant_agents = [
|
|
@@ -37,10 +38,11 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
|
|
| 37 |
agents_dict["psychologist"], agents_dict["critic"]
|
| 38 |
]
|
| 39 |
|
|
|
|
| 40 |
groupchat = autogen.GroupChat(
|
| 41 |
agents=assistant_agents,
|
| 42 |
messages=[],
|
| 43 |
-
max_round=
|
| 44 |
speaker_selection_method="auto"
|
| 45 |
)
|
| 46 |
|
|
@@ -49,44 +51,47 @@ def run_novel_studio(uploaded_file, manual_text, oauth_token: gr.OAuthToken | No
|
|
| 49 |
llm_config=agents_dict["editor"].llm_config
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
chat_result = agents_dict["editor"].initiate_chat(
|
| 55 |
manager,
|
| 56 |
message=init_message
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
output_file_path = export_to_docx(final_story_text)
|
| 62 |
|
| 63 |
-
return
|
| 64 |
|
| 65 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 66 |
return f"❌ فشل النظام: {str(e)}", None
|
| 67 |
|
| 68 |
-
# واجهة
|
| 69 |
-
with gr.Blocks(theme=gr.themes.Soft(), css="custom.css"
|
| 70 |
with gr.Sidebar():
|
| 71 |
-
gr.Markdown("#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
gr.Markdown("---")
|
| 76 |
-
gr.
|
| 77 |
-
|
| 78 |
-
text_area = gr.Textbox(label="تعليمات إضافية", lines=8, placeholder="اكتب توجهاتك الفنية هنا...")
|
| 79 |
submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
|
| 80 |
|
| 81 |
with gr.Column():
|
| 82 |
gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
|
| 83 |
-
output_markdown = gr.Markdown(value="
|
| 84 |
download_btn = gr.File(label="تحميل المخطوطة النهائية")
|
| 85 |
|
| 86 |
-
# الربط البرمجي
|
| 87 |
submit_btn.click(
|
| 88 |
fn=run_novel_studio,
|
| 89 |
-
inputs=[file_upload, text_area],
|
| 90 |
outputs=[output_markdown, download_btn],
|
| 91 |
api_name=False
|
| 92 |
)
|
|
|
|
| 4 |
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, user_token):
|
| 8 |
+
"""
|
| 9 |
+
الدالة الرئيسية: تستخدم التوكن المدخل يدوياً من قبل المستخدم.
|
| 10 |
+
"""
|
| 11 |
+
# التأكد من وجود التوكن
|
| 12 |
+
if not user_token or not user_token.startswith("hf_"):
|
| 13 |
+
return "⚠️ يرجى إدخال Hugging Face Token صحيح يبدأ بـ hf_.", None
|
| 14 |
|
| 15 |
+
# تعيين التوكن كمتغير بيئة للنظام
|
| 16 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = user_token
|
| 17 |
|
| 18 |
+
# 1. معالجة الملف والنص
|
| 19 |
file_content = ""
|
| 20 |
if uploaded_file is not None:
|
| 21 |
try:
|
|
|
|
| 24 |
return f"❌ خطأ في قراءة الملف: {str(e)}", None
|
| 25 |
|
| 26 |
combined_context = f"{file_content}\n\n{manual_text}".strip()
|
|
|
|
| 27 |
if len(combined_context) < 10:
|
| 28 |
+
return "⚠️ يرجى تقديم مسودة أو نص كافٍ للعمل.", None
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
# 2. إنشاء الوكلاء بالتوكن الجديد
|
| 32 |
agents_dict = create_agents(user_token)
|
| 33 |
|
| 34 |
assistant_agents = [
|
|
|
|
| 38 |
agents_dict["psychologist"], agents_dict["critic"]
|
| 39 |
]
|
| 40 |
|
| 41 |
+
# 3. إعداد غرفة الدردشة
|
| 42 |
groupchat = autogen.GroupChat(
|
| 43 |
agents=assistant_agents,
|
| 44 |
messages=[],
|
| 45 |
+
max_round=12,
|
| 46 |
speaker_selection_method="auto"
|
| 47 |
)
|
| 48 |
|
|
|
|
| 51 |
llm_config=agents_dict["editor"].llm_config
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# 4. بدء العملية
|
| 55 |
+
init_message = f"المادة الخام للرواية:\n\n{combined_context}"
|
| 56 |
chat_result = agents_dict["editor"].initiate_chat(
|
| 57 |
manager,
|
| 58 |
message=init_message
|
| 59 |
)
|
| 60 |
|
| 61 |
+
final_text = chat_result.chat_history[-1]['content']
|
| 62 |
+
file_path = export_to_docx(final_text)
|
|
|
|
| 63 |
|
| 64 |
+
return final_text, file_path
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
+
# إذا ظهر خطأ 403 هنا، فهذا يعني أن توكن المستخدم نفسه لا يملك صلاحية الـ Inference
|
| 68 |
+
if "403" in str(e):
|
| 69 |
+
return "❌ خطأ 403: التوكن الخاص بك لا يملك صلاحية Inference API. تأكد من إعدادات التوكن في حسابك.", None
|
| 70 |
return f"❌ فشل النظام: {str(e)}", None
|
| 71 |
|
| 72 |
+
# بناء الواجهة
|
| 73 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="custom.css") as demo:
|
| 74 |
with gr.Sidebar():
|
| 75 |
+
gr.Markdown("# 🔑 إعدادات الوصول")
|
| 76 |
+
user_token_input = gr.Textbox(
|
| 77 |
+
label="Hugging Face Token",
|
| 78 |
+
placeholder="hf_xxxxxxxxxxxx",
|
| 79 |
+
type="password"
|
| 80 |
+
)
|
| 81 |
gr.Markdown("---")
|
| 82 |
+
file_upload = gr.File(label="ارفع المسودة (.docx)", file_types=[".docx"])
|
| 83 |
+
text_area = gr.Textbox(label="تعليمات إضافية", lines=8)
|
|
|
|
| 84 |
submit_btn = gr.Button("🚀 إطلاق فريق العمل", variant="primary")
|
| 85 |
|
| 86 |
with gr.Column():
|
| 87 |
gr.HTML("<h1 style='text-align:center;'>🖋️ Ling-1T Novel Studio</h1>")
|
| 88 |
+
output_markdown = gr.Markdown(value="أدخل التوكن الخاص بك ثم اضغط 'إطلاق' للبدء.")
|
| 89 |
download_btn = gr.File(label="تحميل المخطوطة النهائية")
|
| 90 |
|
| 91 |
+
# الربط البرمجي
|
| 92 |
submit_btn.click(
|
| 93 |
fn=run_novel_studio,
|
| 94 |
+
inputs=[file_upload, text_area, user_token_input],
|
| 95 |
outputs=[output_markdown, download_btn],
|
| 96 |
api_name=False
|
| 97 |
)
|