Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 6 |
+
#load the book knowledge base
|
| 7 |
+
books = pd.read_csv("knowledge_base.csv")
|
| 8 |
+
books = books[["Title", "Author", "Genre", "Age_Range", "Description"]]
|
| 9 |
+
books = books.dropna(subset=["Title", "Author", "Genre", "Description"])
|
| 10 |
+
|
| 11 |
+
def search_books(ques, max_givenbooks=5):
|
| 12 |
+
"""Searches the dataframe for rows matching keywords in the user's message. Looks across Title, Genre, and Description columns. """
|
| 13 |
+
if not ques:
|
| 14 |
+
return ""
|
| 15 |
+
|
| 16 |
+
ques = str(ques).lower()
|
| 17 |
+
keywords = ques.split()
|
| 18 |
+
matches = pd.Series(False, index=books.index)
|
| 19 |
+
|
| 20 |
+
for word in keywords:
|
| 21 |
+
matches = matches | (
|
| 22 |
+
books["Title"].str.lower().str.contains(word, na=False) |
|
| 23 |
+
books["Genre"].str.lower().str.contains(word, na=False) |
|
| 24 |
+
books["Description"].str.lower().str.contains(word, na=False)
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
matched_books = books[matches].head(max_givenbooks)
|
| 28 |
+
if matched_books.empty:
|
| 29 |
+
return "No exact matches found in the catalog for this description."
|
| 30 |
+
|
| 31 |
+
results_in = "Available books in our catalog matching your request:\n"
|
| 32 |
+
for idx, row in matched_books.iterrows():
|
| 33 |
+
results_in += f" - **Title** : {row['Title']} | **Author** : {row['Author']} | **Genre** : {row['Genre']} | **Age** : {row['Age_Range']}\n"
|
| 34 |
+
results_in += f" *Description* : {row['Description']}\n\n"
|
| 35 |
+
return results_in
|
| 36 |
+
def respond(message, history):
|
| 37 |
+
catalog_info = search_books(message)
|
| 38 |
+
|
| 39 |
+
instructions = f"""You are the BookBuddy, who is a specialized book recommendation chatbot.
|
| 40 |
+
Your goal is to recommend books to the user based on what they want. Use the catalog from our database to answer the user.
|
| 41 |
+
Prioritize the recommending books from this list if they match the user's perferences:
|
| 42 |
+
|
| 43 |
+
{catalog_info}"""
|
| 44 |
+
|
| 45 |
+
messages = [{"role": "system", "content": instructions}]
|
| 46 |
+
if history:
|
| 47 |
+
messages.extend(history)
|
| 48 |
+
|
| 49 |
+
messages.append({"role": "user", "content": message})
|
| 50 |
+
|
| 51 |
+
response = client.chat_completion(
|
| 52 |
+
messages=messages,
|
| 53 |
+
max_tokens=220
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
return response.choices[0].message.content.strip()
|
| 57 |
+
|
| 58 |
+
chatbot = gr.ChatInterface(
|
| 59 |
+
fn=respond,
|
| 60 |
+
title="BookBuddy",
|
| 61 |
+
description="Ask me for book recommendation on genre, age range, or what kind of story you want"
|
| 62 |
+
)
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
chatbot.launch()
|