Spaces:
Sleeping
Sleeping
Commit
Β·
75ef6a8
1
Parent(s):
cef11d8
chore: super me
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from node import LibrarySystem
|
3 |
+
from helper import response_generator
|
4 |
+
from helper import send_message
|
5 |
+
|
6 |
+
library = LibrarySystem()
|
7 |
+
|
8 |
+
# Streamed response emulator
|
9 |
+
|
10 |
+
st.set_page_config(
|
11 |
+
page_title="π€π¬ Ana",
|
12 |
+
layout="wide"
|
13 |
+
|
14 |
+
)
|
15 |
+
|
16 |
+
st.warning('This is a prototype of a chatbot for Group 28 main algorithm. It is still under development even though the main logic has been completed and run inside terminal', icon="β οΈ")
|
17 |
+
|
18 |
+
|
19 |
+
# Initialize chat history
|
20 |
+
if "messages" not in st.session_state:
|
21 |
+
st.session_state.messages = [{"role": "assistant", "content": "Let's start chatting! π. You can start with hello or hi"}]
|
22 |
+
|
23 |
+
if 'app_state' not in st.session_state:
|
24 |
+
st.session_state['app_state'] = 'initial'
|
25 |
+
|
26 |
+
if 'book' not in st.session_state:
|
27 |
+
st.session_state['book'] = None
|
28 |
+
|
29 |
+
if 'user' not in st.session_state:
|
30 |
+
st.session_state['user'] = ''
|
31 |
+
|
32 |
+
|
33 |
+
with st.sidebar:
|
34 |
+
st.title('π€π¬ Ana')
|
35 |
+
st.markdown('π Ana is a library management and book recommendation app designed to enhance accessibility and convenience.')
|
36 |
+
st.markdown('Convenient Access β Users can interact with Ana anytime, anywhere, to access library services from their comfort zones.')
|
37 |
+
st.markdown('Smart Book Borrowing β Users can reserve books and pick them up at their convenience, eliminating scheduling conflicts.')
|
38 |
+
st.markdown('Personalized Recommendations β The system suggests books based on user preferences, enhancing the reading experience.')
|
39 |
+
|
40 |
+
# Display chat messages from history on app rerun
|
41 |
+
for message in st.session_state.messages:
|
42 |
+
with st.chat_message(message["role"]):
|
43 |
+
st.markdown(message["content"])
|
44 |
+
|
45 |
+
# Accept user input
|
46 |
+
if prompt := st.chat_input("What is up?"):
|
47 |
+
#initialize library system object
|
48 |
+
# Add user message to chat history
|
49 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
50 |
+
# Display user message in chat message container
|
51 |
+
with st.chat_message("user"):
|
52 |
+
st.markdown(prompt)
|
53 |
+
|
54 |
+
# Display assistant response in chat message container
|
55 |
+
with st.chat_message("assistant"):
|
56 |
+
# with st.spinner("Thinking..."):
|
57 |
+
if prompt in ["hi", "hello", "hey", "/start"]:
|
58 |
+
data = library.startMessage()
|
59 |
+
elif st.session_state['app_state'] == 'welcome' :
|
60 |
+
data = library.login_or_register(prompt)
|
61 |
+
elif st.session_state['app_state'] == 'login_search':
|
62 |
+
data = library.loginSearch(prompt)
|
63 |
+
elif st.session_state['app_state'] == 'waiting_for_name' or st.session_state['app_state'] == 'waiting_for_id' :
|
64 |
+
data = library.handle_registration_input(prompt)
|
65 |
+
elif st.session_state['app_state'] == 'default':
|
66 |
+
data = library.main_menu(prompt)
|
67 |
+
elif st.session_state['app_state'] == 'search':
|
68 |
+
data = library.fetchbook(prompt)
|
69 |
+
elif st.session_state['app_state'] == 'borrow':
|
70 |
+
data = library.hasborrowed(st.session_state['book'], prompt)
|
71 |
+
elif st.session_state['app_state'] == 'borrowing':
|
72 |
+
library.borrow_book()
|
73 |
+
else:
|
74 |
+
send_message("Invalid input. Please try again.")
|
75 |
+
|
76 |
+
# Add assistant response to chat history
|
helper.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
|
5 |
+
# def send_message(message):
|
6 |
+
# response = st.write_stream(response_generator(message))
|
7 |
+
# st.session_state.messages.append({"role": "assistant", "content": response})
|
8 |
+
|
9 |
+
def send_message(message):
|
10 |
+
message_placeholder = st.empty()
|
11 |
+
full_response = ""
|
12 |
+
# Simulate stream of response with milliseconds delay
|
13 |
+
for chunk in message.split():
|
14 |
+
full_response += chunk + " "
|
15 |
+
time.sleep(0.05)
|
16 |
+
# Add a blinking cursor to simulate typing
|
17 |
+
message_placeholder.markdown(full_response + "β")
|
18 |
+
message_placeholder.markdown(full_response, unsafe_allow_html=True)
|
19 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
20 |
+
|
21 |
+
|
22 |
+
def response_generator(response):
|
23 |
+
for word in response.split():
|
24 |
+
yield word + " "
|
25 |
+
time.sleep(0.05)
|
node.py
ADDED
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import date
|
2 |
+
from enum import Enum
|
3 |
+
import pandas as pd
|
4 |
+
from fuzzywuzzy import fuzz
|
5 |
+
from tinydb import TinyDB, Query
|
6 |
+
from enum import Enum
|
7 |
+
from helper import send_message, response_generator
|
8 |
+
import streamlit as st
|
9 |
+
|
10 |
+
|
11 |
+
class Database:
|
12 |
+
def __init__(self, db_name):
|
13 |
+
self.db = TinyDB(db_name)
|
14 |
+
self.students = self.db.table("students")
|
15 |
+
self.User = Query()
|
16 |
+
self.trackBook = self.db.table("trackBook")
|
17 |
+
|
18 |
+
def createUser(self, data):
|
19 |
+
if not self.students.contains(self.User.id == data['id']):
|
20 |
+
self.students.insert(data)
|
21 |
+
else:
|
22 |
+
print("User with this ID already exists.")
|
23 |
+
|
24 |
+
def deleteUser(self, id, user_type):
|
25 |
+
self.students.remove(self.User.id == id)
|
26 |
+
|
27 |
+
def searchUser(self, id):
|
28 |
+
search = Query()
|
29 |
+
item = self.students.search(search.id == id )
|
30 |
+
return item[0] if item else None
|
31 |
+
|
32 |
+
def addBorrowedBook(self, data):
|
33 |
+
self.trackBook.insert(data)
|
34 |
+
|
35 |
+
def updateBorrowedBook(self, data):
|
36 |
+
self.trackBook.update(data)
|
37 |
+
|
38 |
+
def listBorrowedBooks(self):
|
39 |
+
print(self.trackBook.all())
|
40 |
+
|
41 |
+
def listBorrowedBooks(self, studentId):
|
42 |
+
search = Query()
|
43 |
+
items = self.trackBook.search(search.studentId == studentId )
|
44 |
+
return items
|
45 |
+
|
46 |
+
def searchBorrowedBookById(self, id):
|
47 |
+
search = Query()
|
48 |
+
item = self.trackBook.search(search.isbn == id )
|
49 |
+
return item[0] if item else None
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
class RecommendationSystem:
|
54 |
+
def __init__(self):
|
55 |
+
self.books = pd.read_csv("books-2.csv")
|
56 |
+
self.books = self.books.drop(columns=['isbn10', 'subtitle', 'published_year', 'num_pages'])
|
57 |
+
|
58 |
+
def getBookByTitle(self, title):
|
59 |
+
return self.books[self.books['title'].str.contains(title, case=False)]
|
60 |
+
|
61 |
+
# def recommend_by_titles(self,book_titles, num_recommendations=5):
|
62 |
+
# reader = Reader(rating_scale = (1, 10))
|
63 |
+
# data = Dataset.load_from_df(books[["title","categories","average_rating"]], reader)
|
64 |
+
# trainset = data.build_full_trainset()
|
65 |
+
|
66 |
+
# model_svd = SVD()
|
67 |
+
# model_svd.fit(trainset)
|
68 |
+
# books['categories'] = books['categories'].astype(str)
|
69 |
+
|
70 |
+
# similar_books = self.books[self.books['title'].apply(lambda x: any(fuzz.ratio(x, book_title) > 80 for book_title in book_titles))]
|
71 |
+
|
72 |
+
# if similar_books.empty:
|
73 |
+
# print("No books found with similar titles. Recommending books.")
|
74 |
+
# return books.sort_values(by='average_rating', ascending=False).head(num_recommendations)
|
75 |
+
|
76 |
+
# similar_categories = set()
|
77 |
+
# for categories in similar_books['categories']:
|
78 |
+
# similar_categories.update(categories.split(', '))
|
79 |
+
|
80 |
+
# recommended_books = books[books['categories'].apply(lambda x: any(cat in similar_categories for cat in x.split(', ')))]
|
81 |
+
|
82 |
+
# return recommended_books.head(num_recommendations)
|
83 |
+
|
84 |
+
|
85 |
+
class LibrarySystem:
|
86 |
+
def __init__(self):
|
87 |
+
self.user = None
|
88 |
+
self.database = Database('db.json')
|
89 |
+
self.recommendation = RecommendationSystem()
|
90 |
+
|
91 |
+
|
92 |
+
def display_menu(self, options):
|
93 |
+
for i, option in enumerate(options, 1):
|
94 |
+
return f"{i}. {option}"
|
95 |
+
|
96 |
+
def get_choice(self, options, incoming_msg=None):
|
97 |
+
while True:
|
98 |
+
try:
|
99 |
+
choice = int(incoming_msg)
|
100 |
+
if 1 <= choice <= len(options):
|
101 |
+
return choice
|
102 |
+
print("Invalid choice. Try again.")
|
103 |
+
except ValueError:
|
104 |
+
print("Invalid input. Enter a number.")
|
105 |
+
|
106 |
+
def startMessage(self):
|
107 |
+
message = '''
|
108 |
+
Welcome to our chatbot! I'm here to help you with your needs.
|
109 |
+
You can interact with me to learn about our services, get support, or provide feedback.
|
110 |
+
Please choose one of the following options:\n\n
|
111 |
+
|
112 |
+
**1. Login**
|
113 |
+
|
114 |
+
**2. Register**
|
115 |
+
'''
|
116 |
+
st.session_state.app_state = 'welcome'
|
117 |
+
send_message(message)
|
118 |
+
return message
|
119 |
+
|
120 |
+
|
121 |
+
def login_or_register(self, incoming_msg):
|
122 |
+
if self.get_choice(["Login", "Register"], incoming_msg) == 1:
|
123 |
+
st.session_state.app_state = 'login'
|
124 |
+
self.login(incoming_msg)
|
125 |
+
else:
|
126 |
+
self.register()
|
127 |
+
|
128 |
+
def login(self, incoming_msg=None):
|
129 |
+
if st.session_state.app_state == "login":
|
130 |
+
message = "Hi there! π Please enter your ID to continue(eg. 10944444)"
|
131 |
+
response = st.write_stream(response_generator(message))
|
132 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
133 |
+
st.session_state.app_state = 'login_search'
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
def loginSearch(self, incoming_msg):
|
138 |
+
self.user = self.database.searchUser(incoming_msg)
|
139 |
+
print(self.user)
|
140 |
+
if self.user != None:
|
141 |
+
st.session_state.app_state = "default"
|
142 |
+
st.session_state.user = self.user["name"]
|
143 |
+
self.run_main_app()
|
144 |
+
else:
|
145 |
+
response = st.write_stream(response_generator("User not found"))
|
146 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
147 |
+
|
148 |
+
def register(self):
|
149 |
+
send_message("Enter your name: ")
|
150 |
+
# response = st.write_stream(response_generator("enter your name: "))
|
151 |
+
# st.session_state.messages.append({"role": "assistant", "content": response})
|
152 |
+
# we need to set the state here to wait for the user to provide a username
|
153 |
+
st.session_state.app_state = "waiting_for_name"
|
154 |
+
|
155 |
+
|
156 |
+
# new function to handle the user input for registration
|
157 |
+
def handle_registration_input(self, incoming_msg):
|
158 |
+
if st.session_state.app_state == "waiting_for_name":
|
159 |
+
self.user = incoming_msg
|
160 |
+
st.session_state.user = self.user
|
161 |
+
st.session_state.app_state = "waiting_for_id"
|
162 |
+
send_message("Enter your ID: ")
|
163 |
+
elif st.session_state.app_state == "waiting_for_id":
|
164 |
+
user_id = incoming_msg
|
165 |
+
self.user = st.session_state.user
|
166 |
+
self.database.createUser({"name": self.user, "id": user_id})
|
167 |
+
st.session_state.app_state = "default" # we set to default here when registration is done
|
168 |
+
self.user = self.database.searchUser(user_id)
|
169 |
+
self.run_main_app()
|
170 |
+
|
171 |
+
|
172 |
+
def run_main_app(self):
|
173 |
+
message = f"""Welcome {st.session_state.user.title()}! π\nPlease choose an option:
|
174 |
+
1οΈβ£ Search Book
|
175 |
+
2οΈβ£ Borrow Book
|
176 |
+
3οΈβ£ Return Book
|
177 |
+
4οΈβ£ List Borrowed Books
|
178 |
+
5οΈβ£ Recommend Book
|
179 |
+
6οΈβ£ Exit
|
180 |
+
|
181 |
+
Reply with the number of your choice.
|
182 |
+
"""
|
183 |
+
send_message(message)
|
184 |
+
|
185 |
+
def main_menu(self, incoming_msg):
|
186 |
+
if incoming_msg in ["1", "2", "3", "4", "5", "6"]:
|
187 |
+
self.handle_student_choice(int(incoming_msg), incoming_msg)
|
188 |
+
else:
|
189 |
+
print(f"Invalid input. Enter a number. {incoming_msg}")
|
190 |
+
send_message("Please enter a valid number")
|
191 |
+
|
192 |
+
|
193 |
+
def handle_student_choice(self, choice, incoming_msg):
|
194 |
+
if choice == 1:
|
195 |
+
self.search_book(incoming_msg)
|
196 |
+
else:
|
197 |
+
send_message("We are still working on this feature. Please try again later.")
|
198 |
+
# elif choice == 2:
|
199 |
+
# self.borrow_book()
|
200 |
+
# elif choice == 3:
|
201 |
+
# self.return_book()
|
202 |
+
# elif choice == 4:
|
203 |
+
# self.list_borrowed_books()
|
204 |
+
# elif choice == 5:
|
205 |
+
# self.recommendBook()
|
206 |
+
|
207 |
+
def search_book(self, incoming_msg):
|
208 |
+
message = f"π , please enter the title of the book:"
|
209 |
+
st.session_state.app_state = "search" # we set to default here when
|
210 |
+
send_message(message)
|
211 |
+
|
212 |
+
|
213 |
+
def fetchbook(self, incoming_msg):
|
214 |
+
book = self.recommendation.getBookByTitle(incoming_msg)
|
215 |
+
st.session_state.book = book
|
216 |
+
if book.empty:
|
217 |
+
message = "β Book not found. Please check the title and try again."
|
218 |
+
send_message(message)
|
219 |
+
return
|
220 |
+
message = f'''
|
221 |
+
π Title: {book.iloc[0]["title"]}
|
222 |
+
π ISBN: {book.iloc[0]["isbn13"]}
|
223 |
+
|
224 |
+
π Borrow this book? (yes/no):
|
225 |
+
'''
|
226 |
+
send_message(message)
|
227 |
+
st.session_state.app_state = "borrow" # we set to default here when
|
228 |
+
|
229 |
+
def hasborrowed(self, book, incoming_msg):
|
230 |
+
if incoming_msg.strip().lower() == "yes":
|
231 |
+
self.borrow_book_by_isbn(book.iloc[0]["isbn13"], book.iloc[0]["title"])
|
232 |
+
|
233 |
+
def borrow_book_by_isbn(self, isbn, title):
|
234 |
+
data = {
|
235 |
+
"title": title,
|
236 |
+
"isbn": str(isbn),
|
237 |
+
"studentId": st.session_state.user["id"],
|
238 |
+
"returned": False,
|
239 |
+
"borrowedDate": date.today().isoformat(),
|
240 |
+
"returnDate": ""
|
241 |
+
}
|
242 |
+
self.database.addBorrowedBook(data)
|
243 |
+
message = "β
Book borrowed successfully!"
|
244 |
+
send_message(message)
|
245 |
+
|
246 |
+
def borrow_book(self):
|
247 |
+
send_message("Enter the title of the book to borrow: ")
|
248 |
+
if st.session_state.state == "borrowing":
|
249 |
+
isbn = self.incoming_msg
|
250 |
+
books = self.recommendation.getBookByTitle(isbn)
|
251 |
+
if books.empty: # Check if the DataFrame is empty
|
252 |
+
send_message("Book not found.")
|
253 |
+
return
|
254 |
+
book_titles = books["title"].tolist()
|
255 |
+
send_message(book_titles)
|
256 |
+
|
257 |
+
choice = self.get_choice(book_titles)
|
258 |
+
book = books.iloc[choice - 1:choice]
|
259 |
+
|
260 |
+
self.borrow_book_by_isbn(book.iloc[0]["isbn13"], book.iloc[0]["title"])
|
261 |
+
|
262 |
+
def recommendBook(self):
|
263 |
+
borrowedBookByUser = self.database.listBorrowedBooks(self.user["id"])
|
264 |
+
print(borrowedBookByUser)
|
265 |
+
if not borrowedBookByUser:
|
266 |
+
print("No books borrowed, cannot recommend.")
|
267 |
+
return
|
268 |
+
book_titles = [book['title'] for book in borrowedBookByUser]
|
269 |
+
recommendations = self.recommendation.recommend_by_titles(book_titles)
|
270 |
+
|
271 |
+
print("Recommended Books:")
|
272 |
+
self.display_menu(recommendations['title'])
|
273 |
+
choice = self.get_choice(recommendations['title'])
|
274 |
+
|
275 |
+
shouldBorrow = input("Do you want to borrow this book? (yes/no): ")
|
276 |
+
if shouldBorrow == 'yes':
|
277 |
+
book = recommendations.iloc[choice - 1:choice]
|
278 |
+
self.borrow_book_by_isbn(book.iloc[0]["isbn13"], book.iloc[0]["title"])
|
279 |
+
|
280 |
+
def return_book(self):
|
281 |
+
isbn = input("Enter the ISBN of the book to return: ")
|
282 |
+
borrowed_book = self.database.searchBorrowedBookById(isbn)
|
283 |
+
if borrowed_book:
|
284 |
+
borrowed_book["returned"] = True
|
285 |
+
borrowed_book["actualReturnDate"] = date.today().isoformat()
|
286 |
+
self.database.updateBorrowedBook(borrowed_book)
|
287 |
+
print("Book returned successfully!")
|
288 |
+
else:
|
289 |
+
print("Book not found in borrowed list.")
|
290 |
+
|
291 |
+
def list_borrowed_books(self):
|
292 |
+
books = self.database.listBorrowedBooks(self.user["id"])
|
293 |
+
if books:
|
294 |
+
for book in books:
|
295 |
+
print(book)
|
296 |
+
else:
|
297 |
+
print("No books borrowed.")
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
python-decouple
|
3 |
+
sqlalchemy
|
4 |
+
psycopg2-binary
|
5 |
+
numpy
|
6 |
+
pandas
|
7 |
+
python-multipart
|
8 |
+
pyngrok
|
9 |
+
# scikit-surprise
|
10 |
+
fuzzywuzzy
|
11 |
+
tinydb
|
12 |
+
python-dotenv
|
13 |
+
logging
|