fine-tuned-chatbot / logic.py
LuisMBA's picture
Update logic.py
98f5046 verified
import pandas as pd
from rapidfuzz import process
# Load the dataset
df = pd.read_csv("./sci_fi_books.csv")
# Function to get author by book title
def get_author_by_title(title):
match = df[df["title"].str.lower() == title.lower()]
if not match.empty:
return match.iloc[0]["author"]
return f"Author not found for book '{title}'."
# Function to get year by book title
def get_year_by_title(title):
match = df[df["title"].str.lower() == title.lower()]
if not match.empty:
return int(match.iloc[0]["year"])
return f"Year not found for book '{title}'."
# Function to get summary by book title
def get_summary_by_title(title):
match = df[df["title"].str.lower() == title.lower()]
if not match.empty:
return match.iloc[0]["summary"]
return f"Summary not found for book '{title}'."
# Function to find a book by summary similarity
def find_book_by_description(description):
summaries = df["summary"].tolist()
match = process.extractOne(description, summaries)
if match and match[1] > 80: # Threshold for similarity
matched_title = df[df["summary"] == match[0]]["title"].iloc[0]
return f"The closest book based on your description is '{matched_title}'."
return "No book matches your description closely enough."