""", unsafe_allow_html=True)
else:
st.markdown("")
# استایلها برای چرخش و پیام در حال فکر کردن
st.markdown("""
""", unsafe_allow_html=True)
import os
import re
import docx
import streamlit as st
import concurrent.futures
from hazm import Normalizer
from rapidfuzz import fuzz
from langchain.schema import SystemMessage, HumanMessage
folder_path = '46'
normalizer = Normalizer()
@st.cache_data(show_spinner="در حال پردازش اسناد... لطفاً صبور باشید.")
def load_and_process_documents(path):
def process_docx(filename):
try:
full_path = os.path.join(path, filename)
doc = docx.Document(full_path)
text = "\n".join([para.text for para in doc.paragraphs])
normalized = normalizer.normalize(text)
return filename, normalized
except Exception as e:
print(f"Error processing {filename}: {e}")
return filename, ""
filenames = [f for f in os.listdir(path) if f.endswith(".docx")]
doc_texts = {}
with concurrent.futures.ThreadPoolExecutor() as executor:
for filename, content in executor.map(process_docx, filenames):
doc_texts[filename] = content
return doc_texts
doc_texts = load_and_process_documents(folder_path)
def clean_text(text):
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
def find_closest_filename(query, filenames):
scores = [(f, fuzz.partial_ratio(query, f)) for f in filenames]
scores.sort(key=lambda x: x[1], reverse=True)
return scores[0][0] if scores else None
# فرض بر این است که متغیر query توسط کاربر مشخص شده است
if query:
closest_file = find_closest_filename(query, list(doc_texts.keys()))
if closest_file:
matched_text = doc_texts[closest_file]
st.markdown(scores[0][0])
st.markdown(matched_text)
prompt = f"""
لطفاً با توجه به سؤال زیر و محتوای سند موجود، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن سند استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
سوال:
{query}
محتوای سند:
{matched_text}
پاسخ نهایی:
"""
response = llm([
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content=prompt)
])
rewritten = clean_text(response.content.strip())
st.markdown(f'
{rewritten}
', unsafe_allow_html=True)
think.empty()
else:
st.warning("هیچ سند مرتبطی پیدا نشد.")
think.empty()