noddysnots commited on
Commit
c435330
Β·
verified Β·
1 Parent(s): 99aa120

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -37
app.py CHANGED
@@ -1,65 +1,56 @@
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
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+ import requests
5
 
6
+ # Load DeepSeek R1 model
7
+ model_name = "deepseek-ai/deepseek-moe-8b-chat" # DeepSeek R1 model
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
10
 
11
+ # 🎯 Function to extract interests from user input
12
  def extract_interests(text):
13
+ prompt = f"Extract the main interests from this request: '{text}'. Provide only 3-5 relevant words."
 
 
14
 
15
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda") # Run on GPU if available
16
+ outputs = model.generate(**inputs, max_length=100)
17
+ interests = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
18
 
19
+ return interests.split(", ") # Return as a list of keywords
 
 
20
 
21
+ # 🎁 Web search for gift suggestions
22
+ def search_gifts(interests):
23
+ query = "+".join(interests)
24
+ amazon_url = f"https://www.amazon.in/s?k={query}"
25
+ igp_url = f"https://www.igp.com/search?q={query}"
26
+ indiamart_url = f"https://dir.indiamart.com/search.mp?ss={query}"
27
 
28
+ return {
29
+ "Amazon": amazon_url,
30
+ "IGP": igp_url,
31
+ "IndiaMart": indiamart_url
32
+ }
33
 
34
  # 🎯 Main function for gift recommendation
35
  def recommend_gifts(text):
36
  if not text:
37
  return "Please enter a description."
38
 
39
+ interests = extract_interests(text) # Use DeepSeek R1
40
+ links = search_gifts(interests) # Get shopping links
 
 
 
 
 
 
41
 
42
  return {
43
  "Predicted Interests": interests,
44
  "Gift Suggestions": links
45
  }
46
 
47
+ # 🎨 Gradio UI for easy interaction
48
  demo = gr.Interface(
49
  fn=recommend_gifts,
50
  inputs="text",
51
  outputs="json",
52
  title="🎁 AI Gift Recommender",
53
+ description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!",
54
  )
55
 
56
  # πŸš€ Launch Gradio App