|
|
|
|
|
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()) |
|
|
|
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
lines = text.split('\n') |
|
|
|
|
|
wrapped_lines = [textwrap.fill(line, width=width) for line in lines] |
|
|
|
|
|
wrapped_text = '\n'.join(wrapped_lines) |
|
|
return wrapped_text |
|
|
|
|
|
|
|
|
def text_split(ducuments): |
|
|
text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=20) |
|
|
docs = text_splitter.split_documents(documents) |
|
|
return docs |
|
|
|
|
|
|
|
|
def embeddings_f(): |
|
|
embeddings = HuggingFaceEmbeddings() |
|
|
return embeddings |
|
|
|
|
|
|
|
|
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(): |
|
|
|
|
|
uploaded_file = st.file_uploader("لطفا یک فایل txt را انتخاب کنید", type="txt") |
|
|
|
|
|
if uploaded_file is not None: |
|
|
|
|
|
with open(uploaded_file) as f: |
|
|
text = f.read() |
|
|
|
|
|
return text |
|
|
else: |
|
|
|
|
|
return "لطفا یک فایل txt را آپلود کنید" |
|
|
|
|
|
|
|
|
|
|
|
def main(): |
|
|
st.set_page_config(page_title="Multiple pdf chat", page_icon=":books:") |
|
|
|
|
|
|
|
|
|
|
|
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"]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
|
|
|
|