Elevi7 commited on
Commit
9c75197
·
verified ·
1 Parent(s): e4468a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -29
app.py CHANGED
@@ -1,39 +1,58 @@
1
  import gradio as gr
 
2
  from sentence_transformers import SentenceTransformer, util
3
  import torch
4
 
5
- # Sample quote dataset
6
- quotes = [
7
- "You are stronger than you think.",
8
- "It's okay to take a break.",
9
- "Progress, not perfection.",
10
- "Breathe. You’re doing better than you think.",
11
- "Small steps still move you forward.",
12
- "This feeling is temporary.",
13
- "One day at a time.",
14
- "You got this.",
15
- "Your value is not measured by productivity.",
16
- "Kindness starts with yourself."
17
- ]
18
-
19
- # Load the embedding model
20
- model = SentenceTransformer('all-MiniLM-L6-v2')
21
  quote_embeddings = model.encode(quotes, convert_to_tensor=True)
22
 
23
- # Recommendation function
24
- def recommend_quote(user_input):
25
- user_embedding = model.encode(user_input, convert_to_tensor=True)
26
- similarities = util.pytorch_cos_sim(user_embedding, quote_embeddings)
27
- top_results = torch.topk(similarities, k=3)
28
- recommended = [quotes[i] for i in top_results.indices[0]]
29
- return "\n\n".join(recommended)
 
 
 
 
30
 
31
- # Gradio UI
32
- interface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
33
  fn=recommend_quote,
34
- inputs=gr.Textbox(placeholder="Type how you feel..."),
 
 
 
35
  outputs="text",
36
- title="MoodMatch",
37
- description="Type how you feel. Get 3 uplifting quotes powered by AI.",
38
  )
39
- interface.launch()
 
 
 
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
+