Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,99 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def respond(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
|
|
|
|
| 10 |
if history:
|
| 11 |
messages.extend(history)
|
| 12 |
|
| 13 |
messages.append({"role": "user", "content": message})
|
| 14 |
|
| 15 |
response = client.chat_completion(
|
| 16 |
-
messages,
|
| 17 |
-
max_tokens=
|
| 18 |
)
|
| 19 |
|
| 20 |
return response.choices[0].message.content.strip()
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
| 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 |
|
| 7 |
+
#load the book knowledge base
|
| 8 |
+
books = pd.read_csv("knowledge_base.csv")
|
| 9 |
+
books = books[["Title", "Author", "Genre", "Age_Range", "Description"]]
|
| 10 |
+
books = books.dropna(subset=["Title", "Author", "Genre", "Description"])
|
| 11 |
+
|
| 12 |
+
def search_books(ques, max_givenbooks=5):
|
| 13 |
+
"""Searches the dataframe for rows matching keywords in the user's message. Looks across Title, Genre, and Description columns. """
|
| 14 |
+
if not ques:
|
| 15 |
+
return ""
|
| 16 |
+
|
| 17 |
+
ques = str(ques).lower()
|
| 18 |
+
keywords = ques.split()
|
| 19 |
+
matches = pd.Series(False, index=books.index)
|
| 20 |
+
|
| 21 |
+
for word in keywords:
|
| 22 |
+
matches = matches | (
|
| 23 |
+
books["Title"].str.lower().str.contains(word, na=False) |
|
| 24 |
+
books["Genre"].str.lower().str.contains(word, na=False) |
|
| 25 |
+
books["Description"].str.lower().str.contains(word, na=False)
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
matched_books = books[matches].head(max_givenbooks)
|
| 29 |
+
if matched_books.empty:
|
| 30 |
+
return "No exact matches found in the catalog for this description."
|
| 31 |
+
|
| 32 |
+
results_in = "Available books in our catalog matching your request:\n"
|
| 33 |
+
for idx, row in matched_books.iterrows():
|
| 34 |
+
results_in += f" - **Title** : {row['Title']} | **Author** : {row['Author']} | **Genre** : {row['Genre']} | **Age** : {row['Age_Range']}\n"
|
| 35 |
+
results_in += f" *Description* : {row['Description']}\n\n"
|
| 36 |
+
return results_in
|
| 37 |
def respond(message, history):
|
| 38 |
+
catalog_info = search_books(message)
|
| 39 |
+
|
| 40 |
+
instructions = f"""You are the BookBuddy, who is a specialized book recommendation chatbot.
|
| 41 |
+
Your goal is to recommend books to the user based on what they want. Use the catalog from our database to answer the user.
|
| 42 |
+
Prioritize the recommending books from this list if they match the user's perferences:
|
| 43 |
|
| 44 |
+
{catalog_info}"""
|
| 45 |
|
| 46 |
+
messages = [{"role": "system", "content": instructions}]
|
| 47 |
if history:
|
| 48 |
messages.extend(history)
|
| 49 |
|
| 50 |
messages.append({"role": "user", "content": message})
|
| 51 |
|
| 52 |
response = client.chat_completion(
|
| 53 |
+
messages=messages,
|
| 54 |
+
max_tokens=220
|
| 55 |
)
|
| 56 |
|
| 57 |
return response.choices[0].message.content.strip()
|
| 58 |
|
| 59 |
+
with gr.Blocks(
|
| 60 |
+
theme=gr.themes.Soft(primary_hue="pink"),
|
| 61 |
+
css="""
|
| 62 |
+
body {
|
| 63 |
+
background-color: #ffccc7;
|
| 64 |
+
}
|
| 65 |
+
"""
|
| 66 |
+
) as demo:
|
| 67 |
+
|
| 68 |
+
gr.Image("BookBuddy.png", show_label=False)
|
| 69 |
+
|
| 70 |
+
gr.Markdown(
|
| 71 |
+
"""
|
| 72 |
+
# π BookBuddy AI
|
| 73 |
+
### Your personal AI book recommender powered, BookBuddy. Ask for genres, moods, or books you love.
|
| 74 |
+
"""
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
chatbot = gr.ChatInterface(
|
| 78 |
+
fn=respond,
|
| 79 |
+
examples=[
|
| 80 |
+
"I want a sad romance book",
|
| 81 |
+
"Give me a fantasy book for teens",
|
| 82 |
+
"Something like The Hunger Games",
|
| 83 |
+
"A mystery with twists",
|
| 84 |
+
"I liked Harry Potter, what should I read?",
|
| 85 |
+
"I'm 15 and love contemporary books, what should I read?",
|
| 86 |
+
"Recommend a fast-paced dystopian story",
|
| 87 |
+
"Give me a book that will make me cry"
|
| 88 |
+
]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
gr.Markdown(
|
| 92 |
+
"""
|
| 93 |
+
<div style="text-align:center; padding-top:15px; color:#2d2d3b;">
|
| 94 |
+
βA reader lives a thousand lives before they die.β β George R.R. Martin βπ
|
| 95 |
+
</div>
|
| 96 |
+
"""
|
| 97 |
+
)
|
| 98 |
|
| 99 |
+
demo.launch(share=True)
|