Spaces:
Runtime error
Runtime error
Commit ·
2fdb01d
1
Parent(s): 0e56a2a
Update app.py
Browse files8 unmodified lines9101112131415161718196 unmodified lines26272829303132333435364 unmodified lines414243444546474849505143 unmodified lines95969798991001011021031 unmodified line10510610710810911011111211311411511651 unmodified lines16816917017117217332 unmodified lines2062072082092102112122132142152162172182192202212222232242252262272288 unmodified linesfrom langchain.llms import CTransformersfrom langchain.chains import RetrievalQAimport base64import torch # Added for GPU support# Additional imports for CPU and GPU utilizationimport psutilimport GPUtilDATA_PATH = 'data/'DB_FAISS_PATH = 'vectorstore/db_faiss'6 unmodified linestext_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)texts = text_splitter.split_documents(documents)# Use GPU if availablemodel_device = 'cuda:0' if torch.cuda.is_available() else 'cpu'embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',model_kwargs={'device': model_device})db = FAISS.from_documents(texts, embeddings)4 unmodified linesif not os.path.exists(DB_FAISS_PATH):return 0# Use GPU if availablemodel_device = 'cuda:0' if torch.cuda.is_available() else 'cpu'embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',model_kwargs={'device': model_device})db = FAISS.load_local(DB_FAISS_PATH, embeddings)43 unmodified lineselif page == "Search Documents":st.header("Search Documents")query = st.text_input("Enter your question:")if st.button("Get Answer"):if not vector_db_exists:st.warning("Vector database not found. Please upload PDF files first.")1 unmodified linest.text("Loading... Please wait.")# Use GPU if availablemodel_device = 'cuda:0' if torch.cuda.is_available() else 'cpu'embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': model_device})db = FAISS.load_local(DB_FAISS_PATH, embeddings)llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGUF",model_type="llama",max_new_tokens=512,temperature=0.551 unmodified linessource_text += f" (Page {page_number})"else:page_number = 'Unknown Page'source_text += f":**\n{page_content}"32 unmodified linesdownload_link_html = f"""<a href="data:application/pdf;base64,{encoded_data}" download="{file_name}" target="_blank">{download_link_label}</a>"""# Display the download link using st.markdownst.markdown(download_link_html, unsafe_allow_html=True)else:st.write(f"Answer: {answer}")# Get CPU utilizationcpu_percent = psutil.cpu_percent(interval=1)# Display CPU utilization percentagest.text(f"CPU Utilization: {cpu_percent}%")# Get GPU utilizationgpus = GPUtil.getGPUs()gpu_utilization = [gpu.load * 100 for gpu in gpus]# Display GPU utilization percentage for each GPUfor i, gpu in enumerate(gpus):st.text(f"GPU {i + 1} Utilization: {gpu_utilization[i]}%")8 unmodified lines910111213146 unmodified lines2122232425262728294 unmodified lines34353637383940414243 unmodified lines8687888990919293941 unmodified line9697989910010110210310410551 unmodified lines15715815916016116216332 unmodified lines1961971981992002012022032048 unmodified linesfrom langchain.llms import CTransformersfrom langchain.chains import RetrievalQAimport base64DATA_PATH = 'data/'DB_FAISS_PATH = 'vectorstore/db_faiss'6 unmodified linestext_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)texts = text_splitter.split_documents(documents)embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',model_kwargs={'device': 'cpu'})db = FAISS.from_documents(texts, embeddings)4 unmodified linesif not os.path.exists(DB_FAISS_PATH):return 0embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',model_kwargs={'device': 'cpu'})db = FAISS.load_local(DB_FAISS_PATH, embeddings)43 unmodified lineselif page == "Search Documents":st.header("Search Documents")query = st.text_input("Enter your question:")if st.button("Get Answer"):if not vector_db_exists:st.warning("Vector database not found. Please upload PDF files first.")1 unmodified linest.text("Loading... Please wait.")embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': 'cpu'})db = FAISS.load_local(DB_FAISS_PATH, embeddings)llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML",model_type="llama",max_new_tokens=512,temperature=0.551 unmodified linessource_text += f" (Page {page_number})"else:page_number = 'Unknown Page'source_text += f":**\n{page_content}"32 unmodified linesdownload_link_html = f"""<a href="data:application/pdf;base64,{encoded_data}" download="{file_name}" target="_blank">{download_link_label}</a>"""# Display the download link using st.markdownst.markdown(download_link_html, unsafe_allow_html=True)else:st.write(f"Answer: {answer}")
app.py+8-32