Spaces:
Runtime error
Runtime error
Nouvelle app
Browse files- app.py +79 -42
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,48 +1,85 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from langchain.document_loaders import PyPDFLoader
|
| 3 |
-
from langchain_community.llms import Ollama
|
| 4 |
-
from langchain.chains import RetrievalQA
|
| 5 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 7 |
from langchain.vectorstores import Chroma
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
-
from
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
loader = PyPDFLoader("content/China.pdf")
|
| 12 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 13 |
-
chunk_size = 500,
|
| 14 |
-
chunk_overlap = 20,
|
| 15 |
-
length_function = len,
|
| 16 |
-
)
|
| 17 |
-
pages = loader.load_and_split(text_splitter)
|
| 18 |
-
db = Chroma.from_documents(pages, HuggingFaceEmbeddings(), persist_directory = 'content/db')
|
| 19 |
retriever = db.as_retriever()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 2 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
from langchain.vectorstores import Chroma
|
| 4 |
+
from langchain.document_loaders import PyPDFLoader
|
| 5 |
+
from langchain import PromptTemplate
|
| 6 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 8 |
import gradio as gr
|
| 9 |
+
from langchain_community.llms import Ollama
|
| 10 |
+
|
| 11 |
+
db = Chroma(persist_directory='content/Sugar cane/db', embedding_function=HuggingFaceEmbeddings())
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
retriever = db.as_retriever()
|
| 14 |
|
| 15 |
+
B_INST, E_INST = "[INST]", "[/INST]"
|
| 16 |
+
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
| 17 |
+
|
| 18 |
+
instruction = "Given the context that has been provided. \n {context}, Answer the following question - \n{question}"
|
| 19 |
+
|
| 20 |
+
system_prompt = """You are an expert in sugar cane cultivation.
|
| 21 |
+
Be precise in your answers and give answers as concise as possible."""
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def get_prompt(instruction, system_prompt):
|
| 25 |
+
SYSTEM_PROMPT = B_SYS + system_prompt + E_SYS
|
| 26 |
+
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
|
| 27 |
+
return prompt_template
|
| 28 |
+
|
| 29 |
+
template = get_prompt(instruction, system_prompt)
|
| 30 |
+
|
| 31 |
+
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
| 32 |
+
|
| 33 |
+
memory = ConversationBufferWindowMemory(
|
| 34 |
+
memory_key="chat_history", k=5,
|
| 35 |
+
return_messages=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
class ChatBot:
|
| 39 |
+
def __init__(self, memory, prompt, retriever = retriever):
|
| 40 |
+
self.memory = memory
|
| 41 |
+
self.prompt = prompt
|
| 42 |
+
self.retriever = retriever
|
| 43 |
+
|
| 44 |
+
def create_chat_bot(self):
|
| 45 |
+
llm = Ollama(model="llama2")
|
| 46 |
+
qa = ConversationalRetrievalChain.from_llm(
|
| 47 |
+
llm=llm,
|
| 48 |
+
retriever=self.retriever,
|
| 49 |
+
memory=self.memory,
|
| 50 |
+
combine_docs_chain_kwargs={"prompt": self.prompt}
|
| 51 |
+
)
|
| 52 |
+
return qa
|
| 53 |
+
|
| 54 |
+
chatbot = ChatBot(memory = memory, prompt = prompt)
|
| 55 |
+
|
| 56 |
+
bot = chatbot.create_chat_bot()
|
| 57 |
+
|
| 58 |
+
def clear_llm_memory():
|
| 59 |
+
bot.memory.clear()
|
| 60 |
+
|
| 61 |
+
def update_prompt(sys_prompt):
|
| 62 |
+
if sys_prompt == "":
|
| 63 |
+
sys_prompt = system_prompt
|
| 64 |
+
template = get_prompt(instruction, sys_prompt)
|
| 65 |
+
|
| 66 |
+
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
| 67 |
+
|
| 68 |
+
bot.combine_docs_chain.llm_chain.prompt = prompt
|
| 69 |
+
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
chatbot = gr.Chatbot(label="Chat Bot", height = 300)
|
| 72 |
+
msg = gr.Textbox(label = "Question")
|
| 73 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 74 |
+
clear_memory = gr.Button(value = "Clear LLM Memory")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def respond(message, chat_history):
|
| 78 |
+
bot_message = bot({"question": message})['answer']
|
| 79 |
+
chat_history.append((message, bot_message))
|
| 80 |
+
return "", chat_history
|
| 81 |
+
|
| 82 |
+
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 83 |
+
clear_memory.click(clear_llm_memory)
|
| 84 |
+
|
| 85 |
+
demo.launch(share=False, debug=True)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
langchain
|
| 2 |
-
transformers
|
|
|
|
|
|
| 1 |
langchain
|
| 2 |
+
transformers
|
| 3 |
+
pypdf
|