Spaces:
Sleeping
Sleeping
Update app.py
#1
by
1yahoo - opened
app.py
CHANGED
|
@@ -1,65 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import autogen
|
|
|
|
| 3 |
from utils import process_uploaded_file, export_to_docx
|
| 4 |
from agents_config import create_agents
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
return "❌ يرجى إدخال Token", None
|
| 10 |
|
| 11 |
-
#
|
| 12 |
context = ""
|
| 13 |
if file_obj is not None:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return "⚠️ النص قصير جداً", None
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
groupchat = autogen.GroupChat(
|
| 25 |
agents=[v for k, v in agents.items() if k != "editor"],
|
| 26 |
messages=[],
|
| 27 |
-
max_round=
|
| 28 |
)
|
| 29 |
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=agents["editor"].llm_config)
|
| 30 |
|
| 31 |
-
# الت
|
| 32 |
-
chat_result = agents["editor"].initiate_chat(
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
final_text = chat_result.chat_history[-1]['content']
|
| 35 |
-
|
| 36 |
|
| 37 |
-
return final_text,
|
| 38 |
except Exception as e:
|
| 39 |
-
return f"❌ خطأ
|
| 40 |
|
| 41 |
-
# واجهة مستقرة
|
| 42 |
-
with gr.Blocks(
|
| 43 |
-
gr.Markdown("# 🖋️ Ling-1T Novel
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
with gr.Column():
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
file_input = gr.File(label="Upload DOCX/TXT")
|
| 50 |
-
|
| 51 |
-
run_btn = gr.Button("🚀 Start")
|
| 52 |
|
| 53 |
with gr.Column():
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
|
|
|
| 57 |
run_btn.click(
|
| 58 |
-
fn=
|
| 59 |
-
inputs=[file_input,
|
| 60 |
-
outputs=[
|
| 61 |
-
api_name=False
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import autogen
|
| 3 |
+
import os
|
| 4 |
from utils import process_uploaded_file, export_to_docx
|
| 5 |
from agents_config import create_agents
|
| 6 |
|
| 7 |
+
def start_novel_engine(file_obj, extra_text, user_token):
|
| 8 |
+
if not user_token:
|
| 9 |
+
return "⚠️ يرجى إدخال التوكن الخاص بك.", None
|
|
|
|
| 10 |
|
| 11 |
+
# 1. استخراج النص من الملف
|
| 12 |
context = ""
|
| 13 |
if file_obj is not None:
|
| 14 |
+
try:
|
| 15 |
+
context = process_uploaded_file(file_obj)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"❌ خطأ في الملف: {str(e)}", None
|
| 18 |
|
| 19 |
+
full_prompt = f"{context}\n\n{extra_text}".strip()
|
| 20 |
+
if len(full_prompt) < 10:
|
| 21 |
+
return "⚠️ النص قصير جدًا للبدء.", None
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
# 2. تشغيل الوكلاء
|
| 25 |
+
agents = create_agents(user_token)
|
| 26 |
groupchat = autogen.GroupChat(
|
| 27 |
agents=[v for k, v in agents.items() if k != "editor"],
|
| 28 |
messages=[],
|
| 29 |
+
max_round=10
|
| 30 |
)
|
| 31 |
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=agents["editor"].llm_config)
|
| 32 |
|
| 33 |
+
# 3. التواصل مع Ling-1T
|
| 34 |
+
chat_result = agents["editor"].initiate_chat(
|
| 35 |
+
manager,
|
| 36 |
+
message=f"قم بصياغة الفصل التالي وتنسيقه أدبيًا:\n\n{full_prompt}"
|
| 37 |
+
)
|
| 38 |
|
| 39 |
final_text = chat_result.chat_history[-1]['content']
|
| 40 |
+
docx_path = export_to_docx(final_text)
|
| 41 |
|
| 42 |
+
return final_text, docx_path
|
| 43 |
except Exception as e:
|
| 44 |
+
return f"❌ خطأ أثناء الإنتاج: {str(e)}", None
|
| 45 |
|
| 46 |
+
# واجهة مستقرة (بدون SSR وبدون مسببات الخطأ)
|
| 47 |
+
with gr.Blocks(analytics_enabled=False) as demo:
|
| 48 |
+
gr.Markdown("# 🖋️ Ling-1T Novel Studio")
|
| 49 |
|
| 50 |
with gr.Row():
|
| 51 |
with gr.Column():
|
| 52 |
+
hf_token = gr.Textbox(label="Hugging Face Token", type="password")
|
| 53 |
+
# إزالة أي خصائص إضافية من الملف لضمان الاستقرار
|
| 54 |
+
file_input = gr.File(label="Upload DOCX/TXT")
|
| 55 |
+
instruction = gr.Textbox(label="Special Instructions", lines=3)
|
| 56 |
+
run_btn = gr.Button("🚀 Start Writing", variant="primary")
|
| 57 |
|
| 58 |
with gr.Column():
|
| 59 |
+
output_display = gr.Textbox(label="Manuscript Preview", lines=12)
|
| 60 |
+
download_link = gr.File(label="Download Final DOCX")
|
| 61 |
|
| 62 |
+
# القضاء على الخطأ عبر منع الـ API تمامًا لهذا الحدث
|
| 63 |
run_btn.click(
|
| 64 |
+
fn=start_novel_engine,
|
| 65 |
+
inputs=[file_input, instruction, hf_token],
|
| 66 |
+
outputs=[output_display, download_link],
|
| 67 |
+
api_name=False # هذا السطر يمنع Gradio من فحص المكونات برمجياً
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
# تشغيل مباشر وبسيط
|
| 72 |
+
demo.launch(show_api=False)
|