import gradio as gr from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter from langchain.vectorstores import DocArrayInMemorySearch from langchain.chains import RetrievalQA, ConversationalRetrievalChain from langchain.memory import ConversationBufferMemory from langchain.chat_models import ChatOpenAI from langchain.embeddings import HuggingFaceEmbeddings from langchain import HuggingFaceHub from langchain.llms import LlamaCpp from huggingface_hub import hf_hub_download from langchain.document_loaders import ( EverNoteLoader, TextLoader, UnstructuredEPubLoader, UnstructuredHTMLLoader, UnstructuredMarkdownLoader, UnstructuredODTLoader, UnstructuredPowerPointLoader, UnstructuredWordDocumentLoader, PyPDFLoader, ) import param import os import torch from conversadocs.bones import DocChat dc = DocChat() ##### GRADIO CONFIG #### if torch.cuda.is_available(): print("CUDA is available on this system.") os.system('CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python --force-reinstall --upgrade --no-cache-dir --verbose') else: print("CUDA is not available on this system.") os.system('pip install llama-cpp-python') css=""" #col-container {max-width: 700px; margin-left: auto; margin-right: auto;} """ title = """
Upload txt, pdf, doc, docx, enex, epub, html, md, odt, ptt, pttx; click the "Click to Upload Files" button,
Wait for the Status to show Loaded documents, start typing your questions.
The app is set to store chat-history
{txt}
{sc}
" return data_docs with gr.Blocks(theme=theme, css=css) as demo: with gr.Tab("Chat"): with gr.Column(elem_id="col-container"): gr.HTML(title) upload_button = gr.UploadButton("Click to Upload Files", file_types=["pdf"], file_count="multiple") file_output = gr.HTML() chatbot = gr.Chatbot([], elem_id="chatbot").style(height=300) msg = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ") with gr.Column(): sou = gr.HTML("") with gr.Tab("Chat Options"): max_docs = gr.inputs.Slider(1, 10, default=3, label="Maximum querys to the DB.", step=1) row_table = gr.HTML("