Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,58 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
quotes =
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 21 |
quote_embeddings = model.encode(quotes, convert_to_tensor=True)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
def recommend_quote(
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
fn=recommend_quote,
|
| 34 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 35 |
outputs="text",
|
| 36 |
-
title="MoodMatch",
|
| 37 |
-
description="
|
| 38 |
)
|
| 39 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
from sentence_transformers import SentenceTransformer, util
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Load the dataset from Hugging Face
|
| 7 |
+
dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train")
|
| 8 |
+
|
| 9 |
+
# Extract quotes, authors, and tags
|
| 10 |
+
quotes = [item["quote"] for item in dataset]
|
| 11 |
+
authors = [item["author"] for item in dataset]
|
| 12 |
+
tags_list = [item["tags"] for item in dataset]
|
| 13 |
+
|
| 14 |
+
# Flatten tags and remove duplicates for dropdown
|
| 15 |
+
from itertools import chain
|
| 16 |
+
all_tags = list(set(chain.from_iterable(tags_list)))
|
| 17 |
+
all_tags.sort()
|
| 18 |
+
|
| 19 |
+
# Load embedding model
|
| 20 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
| 21 |
quote_embeddings = model.encode(quotes, convert_to_tensor=True)
|
| 22 |
|
| 23 |
+
# Main function
|
| 24 |
+
def recommend_quote(mood_input, selected_tag):
|
| 25 |
+
# Filter quotes by tag
|
| 26 |
+
filtered = [(q, a, i) for i, (q, a, t) in enumerate(zip(quotes, authors, tags_list)) if selected_tag in t]
|
| 27 |
+
if not filtered:
|
| 28 |
+
return "No quotes found for that category."
|
| 29 |
+
|
| 30 |
+
f_quotes = [q for q, a, _ in filtered]
|
| 31 |
+
f_authors = [a for _, a, _ in filtered]
|
| 32 |
+
f_indices = [i for _, _, i in filtered]
|
| 33 |
+
f_embeddings = quote_embeddings[f_indices]
|
| 34 |
|
| 35 |
+
input_embedding = model.encode(mood_input, convert_to_tensor=True)
|
| 36 |
+
similarities = util.pytorch_cos_sim(input_embedding, f_embeddings)
|
| 37 |
+
top_k = torch.topk(similarities, k=min(3, len(f_quotes)))
|
| 38 |
+
|
| 39 |
+
results = []
|
| 40 |
+
for i in top_k.indices[0]:
|
| 41 |
+
results.append(f"“{f_quotes[i]}” — {f_authors[i]}")
|
| 42 |
+
|
| 43 |
+
return "\n\n".join(results)
|
| 44 |
+
|
| 45 |
+
# Gradio interface
|
| 46 |
+
demo = gr.Interface(
|
| 47 |
fn=recommend_quote,
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.Textbox(label="How are you feeling?", placeholder="e.g. I feel overwhelmed with school"),
|
| 50 |
+
gr.Dropdown(choices=all_tags, label="Choose a Category")
|
| 51 |
+
],
|
| 52 |
outputs="text",
|
| 53 |
+
title="🧠 MoodMatch: Find Quotes that Fit Your Mood",
|
| 54 |
+
description="Get inspiring quotes based on how you feel and what kind of motivation you want."
|
| 55 |
)
|
| 56 |
+
|
| 57 |
+
demo.launch()
|
| 58 |
+
|