import streamlit as st from PyPDF2 import PdfReader from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings import HuggingFaceEmbeddings from langchain.vectorstores import FAISS import os import tempfile ####################################################################################### def copy_file_content(src_file, dest_file): try: shutil.copyfile(src_file, dest_file) print(f"محتوای فایل {src_file} با موفقیت به {dest_file} کپی شد.") except FileNotFoundError: print(f"فایل {src_file} یافت نشد.") except Exception as e: print(f"خطا در کپی فایل: {e}") ######################################################################################## def show_path(): uploaded_file = st.file_uploader("فایل را آپلود کنید", type=["csv", "txt"]) if uploaded_file: # ایجاد یک پوشه موقت temp_dir = tempfile.mkdtemp() # ایجاد مسیر فایل آپلود شده در پوشه موقت path = os.path.join(temp_dir,uploaded_file.name) # ذخیره محتوای فایل آپلود شده در مسیر with open(path, "wb") as f: f.write(uploaded_file.getvalue()) # نمایش مسیر فایل آپلود شده #st.success(f"مسیر فایل آپلود شده: {path}") return path ######################################################### def get_pdf_text(pdf_docs): text = "" for pdf in pdf_docs: pdf_reader = PdfReader(pdf) for page in pdf_reader.pages: text += page.extract_text() return text ################################################################# def wrap_text_preserve_newlines(text, width=300): # Split the input text into lines based on newline characters lines = text.split('\n') # Wrap each line individually wrapped_lines = [textwrap.fill(line, width=width) for line in lines] # Join the wrapped lines back together using newline characters wrapped_text = '\n'.join(wrapped_lines) return wrapped_text ######################################################################## # Text Splitter def text_split(ducuments): text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=20) docs = text_splitter.split_documents(documents) return docs ###################################################################### # Embeddings def embeddings_f(): embeddings = HuggingFaceEmbeddings() return embeddings ########################################################################## #upload pdf def pdf_upload(): pdf_docs=st.file_uploader("Upload your PDF Files",accept_multiple_files=True) return pdf_docs ############################################################################## def show(text): text_show=st.write(text) return text_show ########################################################################### def upload_txt_file(): # ایجاد یک ورودی برای آپلود فایل txt uploaded_file = st.file_uploader("لطفا یک فایل txt را انتخاب کنید", type="txt") # بررسی اینکه آیا فایلی آپلود شده است یا خیر if uploaded_file is not None: # باز کردن فایل با انکد UTF-8 و قرار دادن محتوای آن در یک متغیر به نام text with open(uploaded_file) as f: text = f.read() # برگرداندن متغیر text return text else: # برگرداندن یک پیام خطا اگر فایلی آپلود نشده باشد return "لطفا یک فایل txt را آپلود کنید" ############################################################################### def main(): st.set_page_config(page_title="Multiple pdf chat", page_icon=":books:") #chunk_size=st.text_input("enter a chunk size:") #chunk_overlap=st.text_input("enter a chunk overlap:") if st.button("build model"): with st.spinner("waiting"): import app2 st.write("compelete build model") query=st.text_input("enter a question:") if st.button("answer your question"): with st.spinner("waiting"): from app2 import db from app2 import load_model from app2 import find_help out=load_model(query,db) st.write(out) st.write("-------------------------------------------------") find_help(out) ############### with st.sidebar: uploaded_file = st.file_uploader("فایل را آپلود کنید", type=["csv", "txt"]) #st.subheader("your document") #source_path=show_path() #if st.button("copy process"): #with st.spinner("processing"): #app1.copy_file(source_path) # Press the green button in the gutter to run the script. if __name__ == '__main__': main()