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

Update app.py

Browse files

Final working version with motivational quote dataset

Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -1,43 +1,46 @@
1
  import gradio as gr
2
  from datasets import load_dataset
3
  from itertools import chain
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"),
40
  outputs="text",
41
  title="MoodMatch",
42
  description="Get inspiring quotes by typing any theme or category β€” even partial matches work."
43
- ).launch()
 
 
 
1
  import gradio as gr
2
  from datasets import load_dataset
3
  from itertools import chain
 
4
  from difflib import get_close_matches
5
+ import random
6
 
7
+ # Load and clean dataset
8
+ dataset = load_dataset("asudener/motivational-quotes", "quotes_extended", split="train")
 
9
 
10
+ quotes = [item["quote"] for item in dataset if "quote" in item]
11
+ authors = [item["author"] if item.get("author") else "Unknown" for item in dataset]
12
+ tags_list = [item["tags"].split(", ") if item.get("tags") else [] for item in dataset]
 
 
 
13
 
14
+ all_tags = sorted(set(chain.from_iterable(tags_list)))
15
+
16
+ # Quote search logic
17
  def recommend_quote(user_input):
18
  if not user_input:
19
+ return "Please enter a category."
20
 
21
+ matched = get_close_matches(user_input.lower(), all_tags, n=1, cutoff=0.4)
22
  if not matched:
23
+ return f"No quotes found for a category like '{user_input}'. Try something else."
24
 
25
+ selected_tag = matched[0]
26
+ matches = [
27
+ (q, a) for q, a, tags in zip(quotes, authors, tags_list)
28
+ if selected_tag in [t.lower() for t in tags]
29
+ ]
30
 
31
  if not matches:
32
+ return f"No quotes found for the tag '{selected_tag}'."
33
 
34
  quote, author = random.choice(matches)
35
+ return f"\"{quote}\"\n\nβ€” {author}"
36
 
37
+ # UI
38
+ interface = gr.Interface(
39
  fn=recommend_quote,
40
+ inputs=gr.Textbox(label="Type a category like 'hope', 'courage', 'resilience'...", placeholder="e.g. hope"),
41
  outputs="text",
42
  title="MoodMatch",
43
  description="Get inspiring quotes by typing any theme or category β€” even partial matches work."
44
+ )
45
+
46
+ interface.launch()