Elevi7 commited on
Commit
6be12e9
Β·
verified Β·
1 Parent(s): a603f6f

Update app.py

Browse files

Final working version with motivational quote dataset

Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -4,31 +4,36 @@ 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"),
 
4
  import random
5
  from difflib import get_close_matches
6
 
7
+ # Load dataset
8
  raw_dataset = load_dataset("asuender/motivational-quotes", "quotes_extended", split="train")
9
  dataset = list(raw_dataset)
10
 
11
+ # Extract fields
12
  quotes = [item["quote"] for item in dataset]
13
  authors = [item["author"] for item in dataset]
14
+ # πŸ” Try changing "category" to the correct field name if needed
15
+ categories_list = [item["category"].split(", ") if "category" in item else [] for item in dataset]
16
+ all_categories = sorted(set(chain.from_iterable(categories_list)))
17
 
18
+ # Recommendation logic
19
  def recommend_quote(user_input):
20
  if not user_input:
21
+ return "Please enter a category or theme."
22
+
23
+ matched = get_close_matches(user_input, all_categories, n=1, cutoff=0.5)
24
  if not matched:
25
+ return f"πŸ˜• No quotes found for a category like '{user_input}'. Try something else."
26
+
27
+ selected_cat = matched[0]
28
+ matches = [(q, a) for q, a, c in zip(quotes, authors, categories_list) if selected_cat in c]
29
 
 
 
 
30
  if not matches:
31
+ return f"πŸ˜• No quotes found for the tag '{selected_cat}'."
32
+
33
  quote, author = random.choice(matches)
34
+ return f"β€œ{quote}”\nβ€” {author}"
35
 
36
+ # Launch UI
37
  gr.Interface(
38
  fn=recommend_quote,
39
  inputs=gr.Textbox(label="Type a category like 'hope', 'courage', 'love'...", placeholder="e.g. hope"),