File size: 1,538 Bytes
6902bea
9c75197
81d899d
a603f6f
dd41aff
e1979ec
dd41aff
c91a90a
6902bea
dd41aff
 
 
 
 
 
a603f6f
 
dd41aff
6be12e9
dd41aff
a603f6f
c91a90a
6be12e9
dd41aff
c91a90a
dd41aff
c91a90a
 
dd41aff
a603f6f
5b109c0
c91a90a
6be12e9
5b109c0
c91a90a
9c75197
dd41aff
c91a90a
5b109c0
c91a90a
 
 
 
6902bea
73861ff
a603f6f
c91a90a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from datasets import load_dataset
from itertools import chain
from difflib import get_close_matches
import random

# Load and clean dataset
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)))

# Quote search logic
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}"

# UI
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()