Update app.py
Browse filesFinal working version with motivational quote dataset
app.py
CHANGED
|
@@ -2,48 +2,66 @@ import gradio as gr
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
from sentence_transformers import SentenceTransformer, util
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load dataset
|
| 7 |
raw_dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train")
|
| 8 |
dataset = list(raw_dataset)
|
| 9 |
|
| 10 |
-
# Extract quotes, authors, and tags
|
| 11 |
quotes = [item["quote"] for item in dataset]
|
| 12 |
authors = [item["author"] for item in dataset]
|
| 13 |
-
tags_list = [
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
| 19 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
def recommend_quote(mood_input, selected_tag):
|
| 22 |
-
# Filter by
|
| 23 |
-
filtered = [(q, a) for (q, a, t) in zip(quotes, authors, tags_list) if selected_tag in t]
|
|
|
|
| 24 |
if not filtered:
|
| 25 |
return "π Sorry, no quotes found for that category."
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
input_embedding = model.encode(mood_input, convert_to_tensor=True)
|
| 32 |
f_embeddings = model.encode(f_quotes, convert_to_tensor=True)
|
| 33 |
similarities = util.pytorch_cos_sim(input_embedding, f_embeddings)
|
| 34 |
-
top_k = torch.topk(similarities, k=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
result = ""
|
| 38 |
-
for i in top_k.indices:
|
| 39 |
-
result += f"{f_quotes[i]}\nβ {f_authors[i]}\n\n"
|
| 40 |
-
return result.strip()
|
| 41 |
|
|
|
|
| 42 |
iface = gr.Interface(
|
| 43 |
fn=recommend_quote,
|
| 44 |
inputs=[
|
| 45 |
gr.Textbox(lines=2, label="What's your current mood or thought?"),
|
| 46 |
-
gr.Radio(choices=
|
| 47 |
],
|
| 48 |
outputs="text",
|
| 49 |
title="MoodMatch β AI Quote Recommender",
|
|
|
|
| 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 |
+
for item in dataset:
|
| 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 category."
|
| 41 |
|
| 42 |
+
# Filter out used quotes if possible
|
| 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=recommend_quote,
|
| 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 β AI Quote Recommender",
|