noddysnots commited on
Commit
48bf064
Β·
verified Β·
1 Parent(s): b88bdc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -1,44 +1,57 @@
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-R1"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
 
 
9
  model = AutoModelForCausalLM.from_pretrained(
10
- model_name, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True
 
 
 
11
  )
12
 
 
 
 
 
13
  # 🎯 Function to extract interests from user input
14
  def extract_interests(text):
15
- prompt = f"Extract 3-5 relevant gift-related interests from: '{text}'"
16
 
17
- inputs = tokenizer(prompt, return_tensors="pt").to("cuda") # Use GPU if available
18
- outputs = model.generate(**inputs, max_length=100)
19
- interests = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
20
 
21
- return interests.split(", ") # Return as a list of keywords
22
 
23
  # 🎁 Web search for gift suggestions
24
  def search_gifts(interests):
25
  query = "+".join(interests)
26
  amazon_url = f"https://www.amazon.in/s?k={query}"
 
27
  igp_url = f"https://www.igp.com/search?q={query}"
28
  indiamart_url = f"https://dir.indiamart.com/search.mp?ss={query}"
29
 
30
  return {
31
- "Amazon": f"[Amazon]({amazon_url})",
32
- "IGP": f"[IGP]({igp_url})",
33
- "IndiaMart": f"[IndiaMart]({indiamart_url})"
 
34
  }
35
 
 
36
  # 🎯 Main function for gift recommendation
37
  def recommend_gifts(text):
38
  if not text:
39
  return "Please enter a description."
40
 
41
- interests = extract_interests(text) # Use DeepSeek R1
42
  links = search_gifts(interests) # Get shopping links
43
 
44
  return {
@@ -46,10 +59,11 @@ def recommend_gifts(text):
46
  "Gift Suggestions": links
47
  }
48
 
 
49
  # 🎨 Gradio UI for easy interaction
50
  demo = gr.Interface(
51
- fn=recommend_gifts,
52
- inputs="text",
53
  outputs="json",
54
  title="🎁 AI Gift Recommender",
55
  description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!",
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  import torch
4
  import requests
5
 
6
+ # Load DeepSeek-R1 model
7
  model_name = "deepseek-ai/DeepSeek-R1"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
9
+
10
+ # Ensure the model uses float16 instead of fp8
11
  model = AutoModelForCausalLM.from_pretrained(
12
+ model_name,
13
+ torch_dtype=torch.float16, # Forces float16 to prevent fp8 issue
14
+ device_map="auto",
15
+ trust_remote_code=True
16
  )
17
 
18
+ # Use a text-generation pipeline for better inference
19
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
20
+
21
+
22
  # 🎯 Function to extract interests from user input
23
  def extract_interests(text):
24
+ prompt = f"Extract 3-5 relevant interests from this request: '{text}'. Focus on hobbies and product preferences."
25
 
26
+ # Generate model output
27
+ response = generator(prompt, max_length=50, num_return_sequences=1)
28
+ interests = response[0]["generated_text"].replace(prompt, "").strip()
29
+
30
+ return interests.split(", ") # Convert to a list of keywords
31
 
 
32
 
33
  # 🎁 Web search for gift suggestions
34
  def search_gifts(interests):
35
  query = "+".join(interests)
36
  amazon_url = f"https://www.amazon.in/s?k={query}"
37
+ flipkart_url = f"https://www.flipkart.com/search?q={query}"
38
  igp_url = f"https://www.igp.com/search?q={query}"
39
  indiamart_url = f"https://dir.indiamart.com/search.mp?ss={query}"
40
 
41
  return {
42
+ "Amazon": amazon_url,
43
+ "Flipkart": flipkart_url,
44
+ "IGP": igp_url,
45
+ "IndiaMart": indiamart_url
46
  }
47
 
48
+
49
  # 🎯 Main function for gift recommendation
50
  def recommend_gifts(text):
51
  if not text:
52
  return "Please enter a description."
53
 
54
+ interests = extract_interests(text) # Extract interests using DeepSeek R1
55
  links = search_gifts(interests) # Get shopping links
56
 
57
  return {
 
59
  "Gift Suggestions": links
60
  }
61
 
62
+
63
  # 🎨 Gradio UI for easy interaction
64
  demo = gr.Interface(
65
+ fn=recommend_gifts,
66
+ inputs="text",
67
  outputs="json",
68
  title="🎁 AI Gift Recommender",
69
  description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!",