Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
# π―
|
12 |
def extract_interests(text):
|
13 |
-
"
|
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 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
search_links = {}
|
21 |
-
|
22 |
-
for interest in interests:
|
23 |
-
search_query = interest.replace(" ", "+") # Format search string
|
24 |
|
25 |
-
|
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 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
return
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# π― Main function for gift recommendation
|
38 |
def recommend_gifts(text):
|
39 |
if not text:
|
40 |
return "Please enter a description."
|
41 |
|
42 |
-
|
43 |
-
|
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
|
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
|