Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,67 @@
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
from transformers import pipeline
|
|
|
|
|
4 |
|
5 |
-
# Load NLP
|
6 |
zero_shot = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# π Web search for gift suggestions
|
9 |
-
def search_gifts(
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# π― Main function for gift recommendation
|
17 |
def recommend_gifts(text):
|
18 |
if not text:
|
19 |
return "Please enter a description."
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
results = zero_shot(text, categories)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
# Get gift
|
29 |
-
links = search_gifts(
|
30 |
|
31 |
-
return
|
|
|
|
|
|
|
32 |
|
33 |
-
# π¨ Gradio UI for
|
34 |
demo = gr.Interface(
|
35 |
fn=recommend_gifts,
|
36 |
inputs="text",
|
37 |
-
outputs="
|
38 |
title="π AI Gift Recommender",
|
39 |
-
description="Enter details about the person you are buying a gift for, and get personalized suggestions with shopping links!"
|
40 |
)
|
41 |
|
42 |
# π Launch Gradio App
|
43 |
if __name__ == "__main__":
|
44 |
demo.launch()
|
45 |
-
|
|
|
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
|
66 |
if __name__ == "__main__":
|
67 |
demo.launch()
|
|