anikameshetty commited on
Commit
36a7e99
·
verified ·
1 Parent(s): 926e86a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -14,20 +14,23 @@ def search_books(ques, max_givenbooks = 5):
14
  else:
15
  return ""
16
  ques = str(ques).lower()
 
17
 
18
- text_be = (
19
- books["Title"].str.lower().str.contains(ques, na=False) |
20
- books["Genre"].str.lower().str.contains(ques, na=False) |
21
- books["Description"].str.lower().str.contains(ques, na=False)
22
- )
23
- matched_books = books[text_be].head(max_givenbooks)
 
 
24
  if matched_books.empty:
25
  return "No exact matches found in the catalog for this description."
26
- results_str = "Available books in our catalog matching your request:\n"
27
  for idx, row in matched_books.iterrows():
28
- results_str += f" - **Title** : {row['Title']} | **Author** : {row['Author']} | **Genre** : {row['Genre']} | **Age** : {row['Age_Range']}/n"
29
- results_str += f" *Description* : {row['Description']}/n/n"
30
- return results_str
31
  def respond(message, history):
32
  catalog_info = search_books(message)
33
 
@@ -46,6 +49,11 @@ def respond(message, history):
46
 
47
  return response.choices[0].message.content.strip()
48
 
49
- chatbot = gr.ChatInterface(respond)
 
 
 
 
 
50
 
51
  chatbot.launch()
 
14
  else:
15
  return ""
16
  ques = str(ques).lower()
17
+ matches = pd.Series(False, index=books.index)
18
 
19
+ for word in keywords:
20
+ matches = matches | (
21
+ books["Title"].str.lower().str.contains(word, na=False) |
22
+ books["Genre"].str.lower().str.contains(word, na=False) |
23
+ books["Description"].str.lower().str.contains(word, na=False)
24
+ )
25
+
26
+ matched_books = books[matches].head(max_givenbooks)
27
  if matched_books.empty:
28
  return "No exact matches found in the catalog for this description."
29
+ results_in = "Available books in our catalog matching your request:\n"
30
  for idx, row in matched_books.iterrows():
31
+ results_in += f" - **Title** : {row['Title']} | **Author** : {row['Author']} | **Genre** : {row['Genre']} | **Age** : {row['Age_Range']}\n"
32
+ results_in += f" *Description* : {row['Description']}\n\n"
33
+ return results_in
34
  def respond(message, history):
35
  catalog_info = search_books(message)
36
 
 
49
 
50
  return response.choices[0].message.content.strip()
51
 
52
+ chatbot = gr.ChatInterface(
53
+ fn=respond,
54
+ type="messages",
55
+ title="BookBuddy",
56
+ description="Ask me for book recommendation on genre, age range, or what kind of story you want"
57
+ )
58
 
59
  chatbot.launch()