noddysnots commited on
Commit
47b24d6
Β·
verified Β·
1 Parent(s): aeeed0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -19
app.py CHANGED
@@ -1,45 +1,67 @@
1
  import gradio as gr
2
- import requests
3
  from transformers import pipeline
 
 
4
 
5
- # Load NLP model (lighter model for efficiency)
6
  zero_shot = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
 
 
 
 
 
 
 
 
7
 
8
  # 🎁 Web search for gift suggestions
9
- def search_gifts(query):
10
- amazon_url = f"[Amazon](https://www.amazon.in/s?k={query.replace(' ', '+')})"
11
- igp_url = f"[IGP](https://www.igp.com/search?q={query.replace(' ', '+')})"
12
- indiamart_url = f"[IndiaMart](https://dir.indiamart.com/search.mp?ss={query.replace(' ', '+')})"
 
 
 
 
 
 
13
 
14
- return f"πŸ”— **Amazon**: {amazon_url}\nπŸ”— **IGP**: {igp_url}\nπŸ”— **IndiaMart**: {indiamart_url}"
 
 
 
 
 
 
15
 
16
  # 🎯 Main function for gift recommendation
17
  def recommend_gifts(text):
18
  if not text:
19
  return "Please enter a description."
20
 
21
- # NLP Processing
22
- categories = ["art", "music", "tech", "travel", "books", "fashion", "fitness", "gaming"]
23
- results = zero_shot(text, categories)
24
 
25
- # Get top interest
26
- top_interest = results["labels"][0]
27
 
28
- # Get gift links
29
- links = search_gifts(top_interest)
30
 
31
- return f"🎯 **Predicted Interest**: `{top_interest}`\n\nπŸ›’ **Gift Suggestions:**\n{links}"
 
 
 
32
 
33
- # 🎨 Gradio UI for better display
34
  demo = gr.Interface(
35
  fn=recommend_gifts,
36
  inputs="text",
37
- outputs="markdown", # πŸ”Ή Changes output format to Markdown for better UI
38
  title="🎁 AI Gift Recommender",
39
- description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!",
40
  )
41
 
42
  # πŸš€ Launch Gradio App
43
  if __name__ == "__main__":
44
  demo.launch()
45
-
 
1
  import gradio as gr
 
2
  from transformers import pipeline
3
+ from sentence_transformers import SentenceTransformer
4
+ from keybert import KeyBERT
5
 
6
+ # πŸ”Ή Load NLP models
7
  zero_shot = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
8
+ bert_model = SentenceTransformer("all-MiniLM-L6-v2")
9
+ kw_model = KeyBERT(bert_model)
10
+
11
+ # 🎯 Extract interests dynamically from user input
12
+ def extract_interests(text):
13
+ """Extracts key interests from user input using NLP"""
14
+ keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english', top_n=3)
15
+ return [kw[0] for kw in keywords] # Extract only the keywords
16
 
17
  # 🎁 Web search for gift suggestions
18
+ def search_gifts(interests):
19
+ """Search for gifts dynamically based on extracted keywords"""
20
+ search_links = {}
21
+
22
+ for interest in interests:
23
+ search_query = interest.replace(" ", "+") # Format search string
24
+
25
+ amazon_url = f"https://www.amazon.in/s?k={search_query}"
26
+ igp_url = f"https://www.igp.com/search?q={search_query}"
27
+ indiamart_url = f"https://dir.indiamart.com/search.mp?ss={search_query}"
28
 
29
+ search_links[interest] = {
30
+ "Amazon": f"<a href='{amazon_url}' target='_blank'>Amazon</a>",
31
+ "IGP": f"<a href='{igp_url}' target='_blank'>IGP</a>",
32
+ "IndiaMart": f"<a href='{indiamart_url}' target='_blank'>IndiaMart</a>"
33
+ }
34
+
35
+ return search_links
36
 
37
  # 🎯 Main function for gift recommendation
38
  def recommend_gifts(text):
39
  if not text:
40
  return "Please enter a description."
41
 
42
+ # Extract keywords from the text
43
+ interests = extract_interests(text)
 
44
 
45
+ if not interests:
46
+ return {"Error": "Could not determine interests from the input."}
47
 
48
+ # Get gift suggestions
49
+ links = search_gifts(interests)
50
 
51
+ return {
52
+ "Predicted Interests": interests,
53
+ "Gift Suggestions": links
54
+ }
55
 
56
+ # 🎨 Gradio UI for user interaction
57
  demo = gr.Interface(
58
  fn=recommend_gifts,
59
  inputs="text",
60
+ outputs="json",
61
  title="🎁 AI Gift Recommender",
62
+ description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!"
63
  )
64
 
65
  # πŸš€ Launch Gradio App
66
  if __name__ == "__main__":
67
  demo.launch()