Update app.py
Browse filesFinal working version with motivational quote dataset
app.py
CHANGED
|
@@ -1,71 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset
|
| 3 |
-
from sentence_transformers import SentenceTransformer, util
|
| 4 |
-
import torch
|
| 5 |
from itertools import chain
|
| 6 |
import random
|
| 7 |
|
| 8 |
-
# Load dataset
|
| 9 |
raw_dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train")
|
| 10 |
dataset = list(raw_dataset)
|
| 11 |
|
| 12 |
-
# Extract quotes, authors, and tags safely
|
| 13 |
quotes = [item["quote"] for item in dataset]
|
| 14 |
authors = [item["author"] for item in dataset]
|
| 15 |
-
tags_list = []
|
| 16 |
|
| 17 |
-
|
| 18 |
-
tags = item.get("tags")
|
| 19 |
-
if tags:
|
| 20 |
-
tags_list.append(tags.split(", "))
|
| 21 |
-
else:
|
| 22 |
-
tags_list.append([])
|
| 23 |
-
|
| 24 |
-
# Define 5 fixed categories that actually exist in dataset
|
| 25 |
-
fixed_categories = ["inspiration", "success", "life", "love", "courage"]
|
| 26 |
-
|
| 27 |
-
# Load transformer model for semantic similarity
|
| 28 |
-
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 29 |
-
quote_embeddings = model.encode(quotes, convert_to_tensor=True)
|
| 30 |
-
|
| 31 |
-
# Keep track of used quotes to avoid repetition
|
| 32 |
-
used_quote_indices = set()
|
| 33 |
-
|
| 34 |
-
# Recommendation function
|
| 35 |
-
def recommend_quote(mood_input, selected_tag):
|
| 36 |
-
# Filter quotes by tag
|
| 37 |
-
filtered = [(i, q, a) for i, (q, a, t) in enumerate(zip(quotes, authors, tags_list)) if selected_tag in t]
|
| 38 |
|
|
|
|
|
|
|
| 39 |
if not filtered:
|
| 40 |
-
return "π Sorry, no quotes found for that
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
unused = [item for item in filtered if item[0] not in used_quote_indices]
|
| 44 |
-
pool = unused if unused else filtered # fallback to all if all used
|
| 45 |
-
f_indices, f_quotes, f_authors = zip(*pool)
|
| 46 |
-
|
| 47 |
-
# Embed input and compute similarity
|
| 48 |
-
input_embedding = model.encode(mood_input, convert_to_tensor=True)
|
| 49 |
-
f_embeddings = model.encode(f_quotes, convert_to_tensor=True)
|
| 50 |
-
similarities = util.pytorch_cos_sim(input_embedding, f_embeddings)
|
| 51 |
-
top_k = torch.topk(similarities, k=1)
|
| 52 |
-
|
| 53 |
-
top_index = top_k.indices[0][0].item()
|
| 54 |
-
quote_index = f_indices[top_index]
|
| 55 |
-
used_quote_indices.add(quote_index)
|
| 56 |
-
|
| 57 |
-
return f"β{f_quotes[top_index]}β\nβ {f_authors[top_index]}"
|
| 58 |
|
| 59 |
-
# Gradio interface
|
| 60 |
iface = gr.Interface(
|
| 61 |
-
fn=
|
| 62 |
-
inputs=
|
| 63 |
-
gr.Textbox(lines=2, label="What's your current mood or thought?"),
|
| 64 |
-
gr.Radio(choices=fixed_categories, label="Select a category")
|
| 65 |
-
],
|
| 66 |
outputs="text",
|
| 67 |
-
title="MoodMatch
|
| 68 |
-
description="
|
| 69 |
)
|
| 70 |
|
| 71 |
iface.launch()
|
|
|
|
| 1 |
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["tags"].split(", ") if item["tags"] else [] for item in dataset]
|
| 12 |
|
| 13 |
+
all_tags = sorted(set(chain.from_iterable(tags_list)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def recommend_by_category(selected_tag):
|
| 16 |
+
filtered = [(q, a) for (q, a, t) in zip(quotes, authors, tags_list) if selected_tag in t]
|
| 17 |
if not filtered:
|
| 18 |
+
return "π Sorry, no quotes found for that tag."
|
| 19 |
+
quote, author = random.choice(filtered)
|
| 20 |
+
return f"β{quote}β\nβ {author}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 22 |
iface = gr.Interface(
|
| 23 |
+
fn=recommend_by_category,
|
| 24 |
+
inputs=gr.Dropdown(choices=all_tags, label="Start typing a category", filterable=True),
|
|
|
|
|
|
|
|
|
|
| 25 |
outputs="text",
|
| 26 |
+
title="MoodMatch",
|
| 27 |
+
description="Start typing a category like 'wisdom', 'success', or 'love' to receive an inspiring quote."
|
| 28 |
)
|
| 29 |
|
| 30 |
iface.launch()
|