Spaces:
Paused
Paused
Update app.py
#1
by Asem75 - opened
app.py
CHANGED
|
@@ -5,21 +5,21 @@ from PIL import Image
|
|
| 5 |
import numpy as np
|
| 6 |
from docx import Document
|
| 7 |
|
| 8 |
-
# إعداد محرك
|
| 9 |
try: reader = easyocr.Reader(['ar', 'en'], gpu=False)
|
| 10 |
except: reader = None
|
| 11 |
|
| 12 |
-
# دالة الربط
|
| 13 |
def ai_chat(message, history, extracted_text):
|
| 14 |
try:
|
| 15 |
client = Client("Asem75/AIArabicEngen")
|
| 16 |
-
full_msg = f"النص المرج
|
| 17 |
result = client.predict(message=full_msg, api_name="/chat")
|
| 18 |
return result
|
| 19 |
except Exception as e:
|
| 20 |
return f"❌ خطأ اتصال بالمحرك: {e}"
|
| 21 |
|
| 22 |
-
# دالة الاستخراج الشاملة (المستقر
|
| 23 |
def universal_extractor(files):
|
| 24 |
text = ""
|
| 25 |
if not files: return "الرجاء رفع ملف"
|
|
@@ -42,7 +42,7 @@ def universal_extractor(files):
|
|
| 42 |
text += " ".join([r[1] for r in res]) + "\n"
|
| 43 |
return text
|
| 44 |
|
| 45 |
-
# دالة الحفظ
|
| 46 |
def export_logic(chat_history, mode):
|
| 47 |
if not chat_history: return None
|
| 48 |
path = tempfile.mktemp(suffix=".docx")
|
|
@@ -51,34 +51,34 @@ def export_logic(chat_history, mode):
|
|
| 51 |
if mode == "آخر نتيجة فقط":
|
| 52 |
content = chat_history[-1][1]
|
| 53 |
p = doc.add_paragraph(content)
|
| 54 |
-
p.alignment = 1 # لليمين
|
| 55 |
else:
|
| 56 |
for u, b in chat_history:
|
| 57 |
doc.add_paragraph(f"المستخدم: {u}").bold = True
|
| 58 |
-
doc.add_paragraph(f"ال
|
| 59 |
doc.add_paragraph("-" * 15)
|
| 60 |
|
| 61 |
doc.save(path)
|
| 62 |
return path
|
| 63 |
|
| 64 |
-
#
|
| 65 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 66 |
-
gr.Markdown("# 💎 المنصة التعليمية ال
|
| 67 |
|
| 68 |
with gr.Tab("1️⃣ استخراج النصوص"):
|
| 69 |
-
in_files = gr.File(label="
|
| 70 |
-
btn_ex = gr.Button("🔍
|
| 71 |
-
out_raw = gr.Textbox(label="النص المستخرج (
|
| 72 |
btn_ex.click(universal_extractor, in_files, out_raw)
|
| 73 |
|
| 74 |
-
with gr.Tab("2️⃣ ال
|
| 75 |
-
chat = gr.Chatbot(label="
|
| 76 |
-
msg = gr.Textbox(label="اكتب أمرك هنا
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
-
save_mode = gr.Radio(["آخر نتيجة فقط", "المحادثة كاملة"], label="
|
| 80 |
btn_save = gr.Button("📄 إنشاء ملف Word")
|
| 81 |
-
download = gr.File(label="
|
| 82 |
|
| 83 |
def respond(message, history, extracted_text):
|
| 84 |
bot_res = ai_chat(message, history, extracted_text)
|
|
@@ -88,4 +88,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 88 |
msg.submit(respond, [msg, chat, out_raw], [msg, chat])
|
| 89 |
btn_save.click(export_logic, [chat, save_mode], download)
|
| 90 |
|
| 91 |
-
demo.launch()
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from docx import Document
|
| 7 |
|
| 8 |
+
# إعداد محرك الـ OCR
|
| 9 |
try: reader = easyocr.Reader(['ar', 'en'], gpu=False)
|
| 10 |
except: reader = None
|
| 11 |
|
| 12 |
+
# دالة الربط مع المحرك
|
| 13 |
def ai_chat(message, history, extracted_text):
|
| 14 |
try:
|
| 15 |
client = Client("Asem75/AIArabicEngen")
|
| 16 |
+
full_msg = f"النص المستخرج:\n{extracted_text}\n\nطلب المستخدم: {message}"
|
| 17 |
result = client.predict(message=full_msg, api_name="/chat")
|
| 18 |
return result
|
| 19 |
except Exception as e:
|
| 20 |
return f"❌ خطأ اتصال بالمحرك: {e}"
|
| 21 |
|
| 22 |
+
# دالة الاستخراج الشاملة (PDF المستقر، الصور، TXT)
|
| 23 |
def universal_extractor(files):
|
| 24 |
text = ""
|
| 25 |
if not files: return "الرجاء رفع ملف"
|
|
|
|
| 42 |
text += " ".join([r[1] for r in res]) + "\n"
|
| 43 |
return text
|
| 44 |
|
| 45 |
+
# دالة الحفظ (آخر نتيجة أو الكل)
|
| 46 |
def export_logic(chat_history, mode):
|
| 47 |
if not chat_history: return None
|
| 48 |
path = tempfile.mktemp(suffix=".docx")
|
|
|
|
| 51 |
if mode == "آخر نتيجة فقط":
|
| 52 |
content = chat_history[-1][1]
|
| 53 |
p = doc.add_paragraph(content)
|
| 54 |
+
p.alignment = 1 # محاذاة لليمين
|
| 55 |
else:
|
| 56 |
for u, b in chat_history:
|
| 57 |
doc.add_paragraph(f"المستخدم: {u}").bold = True
|
| 58 |
+
doc.add_paragraph(f"المساعد: {b}")
|
| 59 |
doc.add_paragraph("-" * 15)
|
| 60 |
|
| 61 |
doc.save(path)
|
| 62 |
return path
|
| 63 |
|
| 64 |
+
# بناء الواجهة
|
| 65 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 66 |
+
gr.Markdown("# 💎 المنصة التعليمية المتكاملة v8.8")
|
| 67 |
|
| 68 |
with gr.Tab("1️⃣ استخراج النصوص"):
|
| 69 |
+
in_files = gr.File(label="رفع الملفات (PDF, الصور, TXT)", file_count="multiple")
|
| 70 |
+
btn_ex = gr.Button("🔍 استخراج النص الشامل")
|
| 71 |
+
out_raw = gr.Textbox(label="النص المستخرج (قابل للتعديل)", lines=12)
|
| 72 |
btn_ex.click(universal_extractor, in_files, out_raw)
|
| 73 |
|
| 74 |
+
with gr.Tab("2️⃣ المحادثة والحفظ الذكي"):
|
| 75 |
+
chat = gr.Chatbot(label="سجل المحادثة")
|
| 76 |
+
msg = gr.Textbox(label="اكتب أمرك هنا")
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
+
save_mode = gr.Radio(["آخر نتيجة فقط", "المحادثة كاملة"], label="نوع الحفظ", value="آخر نتيجة فقط")
|
| 80 |
btn_save = gr.Button("📄 إنشاء ملف Word")
|
| 81 |
+
download = gr.File(label="تحميل الملف")
|
| 82 |
|
| 83 |
def respond(message, history, extracted_text):
|
| 84 |
bot_res = ai_chat(message, history, extracted_text)
|
|
|
|
| 88 |
msg.submit(respond, [msg, chat, out_raw], [msg, chat])
|
| 89 |
btn_save.click(export_logic, [chat, save_mode], download)
|
| 90 |
|
| 91 |
+
demo.launch()
|