noddysnots commited on
Commit
91e1ca4
Β·
verified Β·
1 Parent(s): 5a9724d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -1,35 +1,53 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
 
4
 
5
  # Load DeepSeek-R1 model and tokenizer
6
  model_name = "deepseek-ai/DeepSeek-R1"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
9
 
10
- # Function to generate gift recommendations
 
 
 
 
 
 
 
 
 
 
11
  def recommend_gifts(text):
12
  if not text:
13
  return "Please enter a description."
14
 
15
  # Prepare input prompt for the model
16
- prompt = f"Based on the following description, suggest suitable gifts: '{text}'"
17
 
18
- # Tokenize input and generate response
19
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
20
  outputs = model.generate(inputs.input_ids, max_length=200, do_sample=True)
21
  recommendation = tokenizer.decode(outputs[0], skip_special_tokens=True)
22
 
23
- return {"Recommendation": recommendation}
 
 
 
 
 
 
24
 
25
- # Gradio interface
26
  demo = gr.Interface(
27
  fn=recommend_gifts,
28
  inputs="text",
29
  outputs="json",
30
- title="AI Gift Recommender",
31
- description="Enter details about the person you are buying a gift for, and get personalized suggestions!",
32
  )
33
 
 
34
  if __name__ == "__main__":
35
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
+ import requests
5
 
6
  # Load DeepSeek-R1 model and tokenizer
7
  model_name = "deepseek-ai/DeepSeek-R1"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True)
10
 
11
+ # 🎁 Web search function to get gift suggestions from various e-commerce sites
12
+ def search_gifts(query):
13
+ search_query = query.replace(" ", "+")
14
+ return {
15
+ "Amazon": f"https://www.amazon.in/s?k={search_query}",
16
+ "Flipkart": f"https://www.flipkart.com/search?q={search_query}",
17
+ "IGP": f"https://www.igp.com/search?q={search_query}",
18
+ "IndiaMart": f"https://dir.indiamart.com/search.mp?ss={search_query}",
19
+ }
20
+
21
+ # 🎯 Generate gift recommendations using AI
22
  def recommend_gifts(text):
23
  if not text:
24
  return "Please enter a description."
25
 
26
  # Prepare input prompt for the model
27
+ prompt = f"Suggest the best gifts for: '{text}'"
28
 
29
+ # Generate response using the model
30
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
31
  outputs = model.generate(inputs.input_ids, max_length=200, do_sample=True)
32
  recommendation = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
 
34
+ # Search for gifts on shopping websites
35
+ product_links = search_gifts(recommendation)
36
+
37
+ return {
38
+ "Predicted Gift": recommendation,
39
+ "Gift Suggestions": product_links
40
+ }
41
 
42
+ # 🎨 Gradio UI for easy interaction
43
  demo = gr.Interface(
44
  fn=recommend_gifts,
45
  inputs="text",
46
  outputs="json",
47
+ title="🎁 AI Gift Recommender",
48
+ description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!",
49
  )
50
 
51
+ # πŸš€ Launch Gradio App
52
  if __name__ == "__main__":
53
  demo.launch()