Update app.py
Browse filesFinal working version with motivational quote dataset
app.py
CHANGED
|
@@ -2,29 +2,37 @@ import gradio as gr
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
from itertools import chain
|
| 4 |
import random
|
|
|
|
| 5 |
|
| 6 |
raw_dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train")
|
| 7 |
dataset = list(raw_dataset)
|
| 8 |
|
| 9 |
quotes = [item["quote"] for item in dataset]
|
| 10 |
authors = [item["author"] for item in dataset]
|
| 11 |
-
tags_list = [item
|
| 12 |
-
|
| 13 |
all_tags = sorted(set(chain.from_iterable(tags_list)))
|
| 14 |
|
| 15 |
-
def recommend_quote(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
matches = [(q, a) for q, a, t in zip(quotes, authors, tags_list) if selected_tag in t]
|
|
|
|
| 17 |
if not matches:
|
| 18 |
-
return "π
|
|
|
|
| 19 |
quote, author = random.choice(matches)
|
| 20 |
-
return f"{quote}\n
|
| 21 |
|
| 22 |
-
|
| 23 |
fn=recommend_quote,
|
| 24 |
-
inputs=gr.
|
| 25 |
outputs="text",
|
| 26 |
title="MoodMatch",
|
| 27 |
-
description="
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
iface.launch()
|
|
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
from itertools import chain
|
| 4 |
import random
|
| 5 |
+
from difflib import get_close_matches
|
| 6 |
|
| 7 |
raw_dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train")
|
| 8 |
dataset = list(raw_dataset)
|
| 9 |
|
| 10 |
quotes = [item["quote"] for item in dataset]
|
| 11 |
authors = [item["author"] for item in dataset]
|
| 12 |
+
tags_list = [item["tags"].split(", ") if item["tags"] else [] for item in dataset]
|
|
|
|
| 13 |
all_tags = sorted(set(chain.from_iterable(tags_list)))
|
| 14 |
|
| 15 |
+
def recommend_quote(user_input):
|
| 16 |
+
if not user_input:
|
| 17 |
+
return "Please enter a category."
|
| 18 |
+
|
| 19 |
+
matched = get_close_matches(user_input, all_tags, n=1, cutoff=0.5)
|
| 20 |
+
if not matched:
|
| 21 |
+
return f"π No quotes found for a category like '{user_input}'. Try something else."
|
| 22 |
+
|
| 23 |
+
selected_tag = matched[0]
|
| 24 |
matches = [(q, a) for q, a, t in zip(quotes, authors, tags_list) if selected_tag in t]
|
| 25 |
+
|
| 26 |
if not matches:
|
| 27 |
+
return f"π No quotes found for the tag '{selected_tag}'."
|
| 28 |
+
|
| 29 |
quote, author = random.choice(matches)
|
| 30 |
+
return f"{quote}\n\nβ {author}"
|
| 31 |
|
| 32 |
+
gr.Interface(
|
| 33 |
fn=recommend_quote,
|
| 34 |
+
inputs=gr.Textbox(label="Type a category like 'hope', 'courage', 'love'...", placeholder="e.g. hope"),
|
| 35 |
outputs="text",
|
| 36 |
title="MoodMatch",
|
| 37 |
+
description="Get inspiring quotes by typing any theme or category β even partial matches work."
|
| 38 |
+
).launch()
|
|
|
|
|
|