File size: 3,234 Bytes
2cc4bd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import gradio as gr
import os

os.environ["OPENAI_API_KEY"] = "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"

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.document_loaders import DirectoryLoader




txt_loader = DirectoryLoader('.\', glob="**/*.txt")
pdf_loader = DirectoryLoader('.\', glob="**/*.pdf")
doc_loader = DirectoryLoader('.\', glob="**/*.docx")
loaders = [pdf_loader, txt_loader, doc_loader]
documents = []

for loader in loaders:
    documents.extend(loader.load())
    
print(f"Total # of documents: {len(documents)}")

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(documents, embeddings)

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever(), memory=memory)

chat_history = []

def submit_callback(user_message):
    default_prompt = " Please format your response in the following way: Each statement should be in a newline . "
    prompt = default_prompt + user_message
    
    # Process user input and generate chatbot response
    response = qa({"question": prompt, "chat_history": chat_history})
    chat_history.append((prompt, response["answer"]))
    return response["answer"]

iface = gr.Interface(
    fn=submit_callback,
    inputs=gr.inputs.Textbox(lines=2, label="Enter your query"),
    outputs=gr.outputs.Textbox(label="Chatbot Response"),
    #outputs=gr.outputs.HTML(label="Chatbot Response"),
    title="LVE Torpedoes Chatbot",
    layout="vertical",
    description="Enter your query to chat with the LVET chatbot",
    examples=[
        ["What are the practice times for each age group ?"],
        ["What are the eligibility criteria for the Mini Torpedoes program?"],
        ["What is the eligibility to participate in the LVET Swim Team?"],
        ["How many volunteer hours are required per family during the swim season?"],
        ["What strokes can swimmers participate in at swim meets?"],
        ["How are swimmers grouped for practice?"],
        ["When do evaluations take place for new swimmers?"],
        ["Who are LVET's Board Members"],
        ["How can I read swim meet results ?"],
        ["How can I contact LVET's Board Members?"],
        ["What is the penalty for not meeting the required volunteer hours?"],
        ["Volunteer Hours?"],
        ["Registration info?"],
        ["How do I sign up for volunteer jobs to fulfill my volunteer hours?"],
        ["Volunteer jobs that do not require certification or prior experience"],
        ["What are the responsibilities of an Age Group Coordinator?"],
        ["How do I commit my swimmer for meets/events?"],
        ["What age groups and races does the LVET Swim Team participate in?"]
    ],
    theme="default"
    
)
iface.launch(share=True)
while True:
    pass