Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,46 +3,54 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
3 |
import torch
|
4 |
import requests
|
5 |
|
6 |
-
# Load DeepSeek
|
7 |
-
model_name = "deepseek-ai/
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
9 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
return {
|
15 |
-
"Amazon": f"https://www.amazon.in/s?k={
|
16 |
-
"Flipkart": f"https://www.flipkart.com/search?q={
|
17 |
-
"IGP": f"https://www.igp.com/search?q={
|
18 |
-
"IndiaMart": f"https://dir.indiamart.com/search.mp?ss={
|
19 |
}
|
20 |
|
21 |
-
# π―
|
22 |
def recommend_gifts(text):
|
23 |
if not text:
|
24 |
return "Please enter a description."
|
25 |
|
26 |
-
|
27 |
-
|
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
|
39 |
-
"Gift Suggestions":
|
40 |
}
|
41 |
|
42 |
-
# π¨ Gradio
|
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!",
|
|
|
3 |
import torch
|
4 |
import requests
|
5 |
|
6 |
+
# Load DeepSeek R1 model with Accelerate enabled
|
7 |
+
model_name = "deepseek-ai/deepseek-moe-8b-chat"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name,
|
11 |
+
torch_dtype=torch.float16,
|
12 |
+
device_map="auto",
|
13 |
+
trust_remote_code=True,
|
14 |
+
low_cpu_mem_usage=True # Helps reduce memory consumption
|
15 |
+
)
|
16 |
+
|
17 |
+
# π― Extract interests using DeepSeek R1
|
18 |
+
def extract_interests(text):
|
19 |
+
prompt = f"Extract key interests from this request: '{text}'. Give 3-5 relevant words only."
|
20 |
+
|
21 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda") # Runs on GPU if available
|
22 |
+
outputs = model.generate(**inputs, max_length=100)
|
23 |
+
interests = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
|
25 |
+
return interests.split(", ") # Return as a list of keywords
|
26 |
+
|
27 |
+
# π Search for gift suggestions from various e-commerce sites
|
28 |
+
def search_gifts(interests):
|
29 |
+
query = "+".join(interests)
|
30 |
return {
|
31 |
+
"Amazon": f"https://www.amazon.in/s?k={query}",
|
32 |
+
"Flipkart": f"https://www.flipkart.com/search?q={query}",
|
33 |
+
"IGP": f"https://www.igp.com/search?q={query}",
|
34 |
+
"IndiaMart": f"https://dir.indiamart.com/search.mp?ss={query}"
|
35 |
}
|
36 |
|
37 |
+
# π― Main function for generating gift recommendations
|
38 |
def recommend_gifts(text):
|
39 |
if not text:
|
40 |
return "Please enter a description."
|
41 |
|
42 |
+
interests = extract_interests(text) # Extract relevant interests
|
43 |
+
links = search_gifts(interests) # Generate shopping links
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
return {
|
46 |
+
"Predicted Interests": interests,
|
47 |
+
"Gift Suggestions": links
|
48 |
}
|
49 |
|
50 |
+
# π¨ Gradio Interface
|
51 |
demo = gr.Interface(
|
52 |
+
fn=recommend_gifts,
|
53 |
+
inputs="text",
|
54 |
outputs="json",
|
55 |
title="π AI Gift Recommender",
|
56 |
description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!",
|