-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.py
126 lines (101 loc) · 4.78 KB
/
memory.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import tkinter as tk
from tkinter import scrolledtext
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from langchain.document_loaders import UnstructuredFileLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
import transformers
import threading
class ChatbotGUI:
def __init__(self, master):
self.master = master
master.title("Felix Chatbot GUI")
# Create a scrolled text area for the conversation
self.conversation_text = scrolledtext.ScrolledText(master, width=60, height=20, wrap=tk.WORD)
self.conversation_text.pack()
# Create an entry for user input
self.input_entry = tk.Entry(master, width=50)
self.input_entry.pack()
# Create a button to send user input
self.send_button = tk.Button(master, text="Send", command=self.send_user_input)
self.send_button.pack()
# Create a text widget to display conversation history
self.history_text = tk.Text(master, height=10, width=60)
self.history_text.pack()
# Initialize the chatbot components
self.initialize_chatbot()
def initialize_chatbot(self):
# Loading documents
loader = UnstructuredFileLoader("./data.txt")
documents = loader.load()
# Text splitting
text_splitter = CharacterTextSplitter(
chunk_size=1000,
chunk_overlap=0,
separator="\n\n",
length_function=len
)
text = text_splitter.split_documents(documents)
# Huggingface Embeddings and Vector Store
embedding = HuggingFaceEmbeddings()
vectordb = Chroma.from_documents(text, embedding=embedding)
# Model and Huggingface pipeline
model_name = 'declare-lab/flan-alpaca-base'
generate_text = transformers.pipeline(
model=model_name,
task='text2text-generation',
max_length=1100,
temperature=0.9,
repetition_penalty=1.1
)
llm = HuggingFacePipeline(pipeline=generate_text)
# Memory and Conversational Chain
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
self.chatbot = ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectordb.as_retriever(), memory=memory)
# Welcome Message
self.display_message("Felix Chatbot: Welcome! Ask me anything or type 'exit' to end the conversation.")
def send_user_input(self):
# Get user input
user_input = self.input_entry.get()
# Display user input in the conversation area
self.display_message(f"You: {user_input}", user_input=True)
# Check for exit command
if user_input.lower() == 'exit':
self.display_message("Felix Chatbot: Goodbye!", chatbot=True)
self.master.quit()
else:
# Create a thread to run the chatbot processing in the background
threading.Thread(target=self.process_user_input, args=(user_input,)).start()
# Clear the input entry
self.input_entry.delete(0, tk.END)
def process_user_input(self, user_input):
# Get and display chatbot response
response = self.chatbot.run(user_input)
self.display_message(f"Felix Chatbot: {response}", chatbot=True)
# Update conversation history
self.update_history(f"You: {user_input}\nFelix Chatbot: {response}\n\n")
def display_message(self, message, user_input=False, chatbot=False):
# Configure tag and insert the message into the conversation area
tag = "user" if user_input else "chatbot" if chatbot else None
self.conversation_text.insert(tk.END, f"\n{message}\n", tag)
# Apply formatting (color, font, etc.) based on the tag
if tag == "user":
self.conversation_text.tag_config(tag, foreground="blue", font=("Helvetica", 10, "bold"))
elif tag == "chatbot":
self.conversation_text.tag_config(tag, foreground="green", font=("Courier", 10, "italic"))
# Scroll to the end of the conversation area
self.conversation_text.see(tk.END)
def update_history(self, message):
# Insert the message into the conversation history
self.history_text.insert(tk.END, message)
# Scroll to the end of the conversation history
self.history_text.see(tk.END)
def main():
root = tk.Tk()
chatbot_gui = ChatbotGUI(root)
root.mainloop()
if __name__ == "__main__":
main()