|
|
import gradio as gr |
|
|
from datasets import load_dataset |
|
|
from itertools import chain |
|
|
from difflib import get_close_matches |
|
|
import random |
|
|
|
|
|
|
|
|
dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train") |
|
|
|
|
|
quotes = [item["quote"] for item in dataset if "quote" in item] |
|
|
authors = [item["author"] if item.get("author") else "Unknown" for item in dataset] |
|
|
tags_list = [item["tags"].split(", ") if item.get("tags") else [] for item in dataset] |
|
|
all_tags = sorted(set(chain.from_iterable(tags_list))) |
|
|
|
|
|
|
|
|
def recommend_quote(user_input): |
|
|
if not user_input: |
|
|
return "Please enter a category." |
|
|
|
|
|
matched = get_close_matches(user_input.lower(), all_tags, n=1, cutoff=0.4) |
|
|
if not matched: |
|
|
return f"π No quotes found for a category like '{user_input}'. Try something else." |
|
|
|
|
|
selected_tag = matched[0] |
|
|
|
|
|
matches = [ |
|
|
(q, a) for q, a, t in zip(quotes, authors, tags_list) |
|
|
if selected_tag in [tag.lower() for tag in t] |
|
|
] |
|
|
|
|
|
if not matches: |
|
|
return f"π No quotes found for the tag '{selected_tag}'." |
|
|
|
|
|
quote, author = random.choice(matches) |
|
|
return f"β{quote}β\n\nβ {author}" |
|
|
|
|
|
|
|
|
gr.Interface( |
|
|
fn=recommend_quote, |
|
|
inputs=gr.Textbox( |
|
|
label="Type a category like 'hope', 'courage', 'resilience'...", |
|
|
placeholder="e.g. hope" |
|
|
), |
|
|
outputs="text", |
|
|
title="MoodMatch", |
|
|
description="Get inspiring quotes by typing any theme or category β even partial matches work." |
|
|
).launch() |
|
|
|