import os from docx import Document from huggingface_hub import HfApi def process_uploaded_file(file): """قراءة محتوى الملف المرفوع وتحويله إلى نص للمعالجة""" if file is None: return "" file_path = file.name if file_path.endswith('.txt'): with open(file_path, 'r', encoding='utf-8') as f: return f.read() elif file_path.endswith('.docx'): doc = Document(file_path) return "\n".join([para.text for para in doc.paragraphs]) return "" def export_to_docx(final_text, filename="Final_Novel_Draft.docx"): """تحويل مخرجات Ling-1T إلى ملف Word منسق واحترافي""" doc = Document() # إضافة عنوان الرواية بتنسيق مميز title = doc.add_heading('المخطوطة النهائية للرواية', 0) title.alignment = 1 # محاذاة للوسط # تنظيف النص وإضافة الفقرات paragraphs = final_text.split('\n') for p in paragraphs: if p.strip(): doc.add_paragraph(p) doc.save(filename) return filename