Update app.py
Browse filesFinal working version with motivational quote dataset
app.py
CHANGED
|
@@ -1,43 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset
|
| 3 |
from itertools import chain
|
| 4 |
-
import random
|
| 5 |
from difflib import get_close_matches
|
|
|
|
| 6 |
|
| 7 |
-
# Load dataset
|
| 8 |
-
|
| 9 |
-
dataset = list(raw_dataset)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# π Try changing "category" to the correct field name if needed
|
| 15 |
-
categories_list = [item["category"].split(", ") if "category" in item else [] for item in dataset]
|
| 16 |
-
all_categories = sorted(set(chain.from_iterable(categories_list)))
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
def recommend_quote(user_input):
|
| 20 |
if not user_input:
|
| 21 |
-
return "Please enter a category
|
| 22 |
|
| 23 |
-
matched = get_close_matches(user_input,
|
| 24 |
if not matched:
|
| 25 |
-
return f"
|
| 26 |
|
| 27 |
-
|
| 28 |
-
matches = [
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if not matches:
|
| 31 |
-
return f"
|
| 32 |
|
| 33 |
quote, author = random.choice(matches)
|
| 34 |
-
return f"
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
gr.Interface(
|
| 38 |
fn=recommend_quote,
|
| 39 |
-
inputs=gr.Textbox(label="Type a category like 'hope', 'courage', '
|
| 40 |
outputs="text",
|
| 41 |
title="MoodMatch",
|
| 42 |
description="Get inspiring quotes by typing any theme or category β even partial matches work."
|
| 43 |
-
)
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset
|
| 3 |
from itertools import chain
|
|
|
|
| 4 |
from difflib import get_close_matches
|
| 5 |
+
import random
|
| 6 |
|
| 7 |
+
# Load and clean dataset
|
| 8 |
+
dataset = load_dataset("asudener/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 |
|
| 14 |
+
all_tags = sorted(set(chain.from_iterable(tags_list)))
|
| 15 |
+
|
| 16 |
+
# Quote search logic
|
| 17 |
def recommend_quote(user_input):
|
| 18 |
if not user_input:
|
| 19 |
+
return "Please enter a category."
|
| 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, tags in zip(quotes, authors, tags_list)
|
| 28 |
+
if selected_tag in [t.lower() for t in tags]
|
| 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 |
+
interface = gr.Interface(
|
| 39 |
fn=recommend_quote,
|
| 40 |
+
inputs=gr.Textbox(label="Type a category like 'hope', 'courage', 'resilience'...", placeholder="e.g. hope"),
|
| 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()
|