Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -106,32 +106,27 @@ def save_faiss_and_metadata(embeddings, sections, base_name):
|
|
106 |
print(f"✅ ذخیره شد:\n - {faiss_path}\n - {metadata_path}")
|
107 |
|
108 |
# ---------- اجرای کامل ----------
|
109 |
-
def build_from_docx(
|
110 |
-
|
111 |
-
|
112 |
-
temp_file.write(docx_file.read())
|
113 |
-
temp_file_path = temp_file.name
|
114 |
-
|
115 |
-
print(f"📄 پردازش فایل: {temp_file_path}")
|
116 |
-
sections = docx_to_sections(temp_file_path)
|
117 |
print(f"🧩 {len(sections)} بخش استخراج شد.")
|
118 |
|
119 |
model = SentenceTransformer(EMBEDDING_MODEL_NAME)
|
120 |
embeddings = generate_embeddings(sections, model)
|
121 |
|
122 |
-
base_name = os.path.splitext(os.path.basename(
|
123 |
save_faiss_and_metadata(embeddings, sections, base_name)
|
124 |
|
125 |
return f"فایلهای FAISS و متادیتا ایجاد شد و در مسیر {OUTPUT_DIR} ذخیره شدند."
|
126 |
|
127 |
# ---------- رابط کاربری Gradio ----------
|
128 |
def process_docx(file):
|
129 |
-
result = build_from_docx(file)
|
130 |
return result
|
131 |
|
132 |
iface = gr.Interface(
|
133 |
fn=process_docx,
|
134 |
-
inputs=gr.File(file_count="single", type="
|
135 |
outputs=gr.Textbox(label="Output"),
|
136 |
title="Docx to FAISS & Metadata Generator",
|
137 |
description="Upload a DOCX file, and it will process the contents to generate FAISS index and metadata."
|
|
|
106 |
print(f"✅ ذخیره شد:\n - {faiss_path}\n - {metadata_path}")
|
107 |
|
108 |
# ---------- اجرای کامل ----------
|
109 |
+
def build_from_docx(docx_file_path):
|
110 |
+
print(f"📄 پردازش فایل: {docx_file_path}")
|
111 |
+
sections = docx_to_sections(docx_file_path)
|
|
|
|
|
|
|
|
|
|
|
112 |
print(f"🧩 {len(sections)} بخش استخراج شد.")
|
113 |
|
114 |
model = SentenceTransformer(EMBEDDING_MODEL_NAME)
|
115 |
embeddings = generate_embeddings(sections, model)
|
116 |
|
117 |
+
base_name = os.path.splitext(os.path.basename(docx_file_path))[0].lower()
|
118 |
save_faiss_and_metadata(embeddings, sections, base_name)
|
119 |
|
120 |
return f"فایلهای FAISS و متادیتا ایجاد شد و در مسیر {OUTPUT_DIR} ذخیره شدند."
|
121 |
|
122 |
# ---------- رابط کاربری Gradio ----------
|
123 |
def process_docx(file):
|
124 |
+
result = build_from_docx(file.name) # استفاده از file.name برای دریافت مسیر فایل
|
125 |
return result
|
126 |
|
127 |
iface = gr.Interface(
|
128 |
fn=process_docx,
|
129 |
+
inputs=gr.File(file_count="single", type="filepath", label="Upload DOCX File"),
|
130 |
outputs=gr.Textbox(label="Output"),
|
131 |
title="Docx to FAISS & Metadata Generator",
|
132 |
description="Upload a DOCX file, and it will process the contents to generate FAISS index and metadata."
|