Elevi7 commited on
Commit
a603f6f
Β·
verified Β·
1 Parent(s): 59a2856

Update app.py

Browse files

Final working version with motivational quote dataset

Files changed (1) hide show
  1. app.py +19 -11
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.get("tags", "").split(", ") if "tags" in item else [] for item in dataset]
12
-
13
  all_tags = sorted(set(chain.from_iterable(tags_list)))
14
 
15
- def recommend_quote(selected_tag):
 
 
 
 
 
 
 
 
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 "πŸ˜” Sorry, no quotes found for that tag."
 
19
  quote, author = random.choice(matches)
20
- return f"{quote}\n– {author}"
21
 
22
- iface = gr.Interface(
23
  fn=recommend_quote,
24
- inputs=gr.Dropdown(choices=all_tags, label="Type a category", interactive=True),
25
  outputs="text",
26
  title="MoodMatch",
27
- description="Start typing a category like 'hope', 'courage', or 'mindfulness' to get an inspiring quote."
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()