Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,11 @@ from difflib import get_close_matches
|
|
| 5 |
import random
|
| 6 |
|
| 7 |
# Load and clean dataset
|
| 8 |
-
dataset = load_dataset("
|
| 9 |
|
| 10 |
quotes = [item["quote"] for item in dataset if "quote" in item]
|
| 11 |
authors = [item["author"] if item.get("author") else "Unknown" for item in dataset]
|
| 12 |
tags_list = [item["tags"].split(", ") if item.get("tags") else [] for item in dataset]
|
| 13 |
-
|
| 14 |
all_tags = sorted(set(chain.from_iterable(tags_list)))
|
| 15 |
|
| 16 |
# Quote search logic
|
|
@@ -20,27 +19,29 @@ def recommend_quote(user_input):
|
|
| 20 |
|
| 21 |
matched = get_close_matches(user_input.lower(), all_tags, n=1, cutoff=0.4)
|
| 22 |
if not matched:
|
| 23 |
-
return f"No quotes found for a category like '{user_input}'. Try something else."
|
| 24 |
|
| 25 |
selected_tag = matched[0]
|
|
|
|
| 26 |
matches = [
|
| 27 |
-
(q, a) for q, a,
|
| 28 |
-
if selected_tag in [
|
| 29 |
]
|
| 30 |
|
| 31 |
if not matches:
|
| 32 |
-
return f"No quotes found for the tag '{selected_tag}'."
|
| 33 |
|
| 34 |
quote, author = random.choice(matches)
|
| 35 |
-
return f"
|
| 36 |
|
| 37 |
# UI
|
| 38 |
-
|
| 39 |
fn=recommend_quote,
|
| 40 |
-
inputs=gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 41 |
outputs="text",
|
| 42 |
title="MoodMatch",
|
| 43 |
description="Get inspiring quotes by typing any theme or category β even partial matches work."
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
interface.launch()
|
|
|
|
| 5 |
import random
|
| 6 |
|
| 7 |
# Load and clean dataset
|
| 8 |
+
dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train")
|
| 9 |
|
| 10 |
quotes = [item["quote"] for item in dataset if "quote" in item]
|
| 11 |
authors = [item["author"] if item.get("author") else "Unknown" for item in dataset]
|
| 12 |
tags_list = [item["tags"].split(", ") if item.get("tags") else [] for item in dataset]
|
|
|
|
| 13 |
all_tags = sorted(set(chain.from_iterable(tags_list)))
|
| 14 |
|
| 15 |
# Quote search logic
|
|
|
|
| 19 |
|
| 20 |
matched = get_close_matches(user_input.lower(), all_tags, n=1, cutoff=0.4)
|
| 21 |
if not matched:
|
| 22 |
+
return f"π No quotes found for a category like '{user_input}'. Try something else."
|
| 23 |
|
| 24 |
selected_tag = matched[0]
|
| 25 |
+
|
| 26 |
matches = [
|
| 27 |
+
(q, a) for q, a, t in zip(quotes, authors, tags_list)
|
| 28 |
+
if selected_tag in [tag.lower() for tag in t]
|
| 29 |
]
|
| 30 |
|
| 31 |
if not matches:
|
| 32 |
+
return f"π No quotes found for the tag '{selected_tag}'."
|
| 33 |
|
| 34 |
quote, author = random.choice(matches)
|
| 35 |
+
return f"β{quote}β\n\nβ {author}"
|
| 36 |
|
| 37 |
# UI
|
| 38 |
+
gr.Interface(
|
| 39 |
fn=recommend_quote,
|
| 40 |
+
inputs=gr.Textbox(
|
| 41 |
+
label="Type a category like 'hope', 'courage', 'resilience'...",
|
| 42 |
+
placeholder="e.g. hope"
|
| 43 |
+
),
|
| 44 |
outputs="text",
|
| 45 |
title="MoodMatch",
|
| 46 |
description="Get inspiring quotes by typing any theme or category β even partial matches work."
|
| 47 |
+
).launch()
|
|
|
|
|
|