Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,68 +11,67 @@ def extract_keywords(text: str, nlp_pipeline) -> list:
|
|
11 |
|
12 |
def generate_search_urls(keywords: list) -> dict:
|
13 |
"""Generate search URLs for various e-commerce platforms"""
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
igp_query = '+'.join(keywords)
|
18 |
-
indiamart_query = ' '.join(keywords)
|
19 |
-
|
20 |
-
# Properly encode the queries
|
21 |
-
amazon_query = urllib.parse.quote(amazon_query)
|
22 |
-
flipkart_query = urllib.parse.quote(flipkart_query)
|
23 |
-
igp_query = urllib.parse.quote(igp_query)
|
24 |
-
indiamart_query = urllib.parse.quote(indiamart_query)
|
25 |
-
|
26 |
return {
|
27 |
-
"Amazon India": f"https://www.amazon.in/s?k={
|
28 |
-
"Flipkart": f"https://www.flipkart.com/search?q={
|
29 |
-
"IGP Gifts": f"https://www.igp.com/search?q={
|
30 |
-
"IndiaMart": f"https://www.indiamart.com/find?q={
|
31 |
}
|
32 |
|
33 |
def recommend_gifts(text: str):
|
34 |
"""Main function to generate gift recommendations"""
|
35 |
if not text:
|
36 |
-
return
|
37 |
-
|
38 |
try:
|
39 |
-
#
|
40 |
nlp = pipeline(
|
41 |
"text-generation",
|
42 |
model="gpt2",
|
43 |
device_map="auto"
|
44 |
)
|
45 |
-
|
46 |
# Extract relevant keywords
|
47 |
keywords = extract_keywords(text, nlp)
|
48 |
-
|
49 |
# Generate search URLs
|
50 |
search_links = generate_search_urls(keywords)
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
except Exception as e:
|
58 |
-
return
|
59 |
|
60 |
-
# Create Gradio interface
|
61 |
demo = gr.Interface(
|
62 |
fn=recommend_gifts,
|
63 |
inputs=gr.Textbox(
|
64 |
lines=3,
|
65 |
placeholder="Describe who you're buying a gift for (age, interests, occasion, etc.)"
|
66 |
),
|
67 |
-
outputs=gr.
|
68 |
title="π Smart Gift Recommender",
|
69 |
description="Get personalized gift suggestions with direct shopping links!",
|
70 |
examples=[
|
71 |
-
["a small kid of age 3 want him to have something like toy that teaches
|
72 |
["age is 25 and he loves puzzle and online FPS games"],
|
73 |
["Looking for a gift for my mom who enjoys gardening and cooking"]
|
74 |
]
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
-
demo.launch()
|
|
|
11 |
|
12 |
def generate_search_urls(keywords: list) -> dict:
|
13 |
"""Generate search URLs for various e-commerce platforms"""
|
14 |
+
# Encode queries properly for URLs
|
15 |
+
query = urllib.parse.quote(" ".join(keywords))
|
16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
return {
|
18 |
+
"Amazon India": f'<a href="https://www.amazon.in/s?k={query}" target="_blank">Amazon</a>',
|
19 |
+
"Flipkart": f'<a href="https://www.flipkart.com/search?q={query}" target="_blank">Flipkart</a>',
|
20 |
+
"IGP Gifts": f'<a href="https://www.igp.com/search?q={query}" target="_blank">IGP</a>',
|
21 |
+
"IndiaMart": f'<a href="https://www.indiamart.com/find?q={query}" target="_blank">IndiaMart</a>',
|
22 |
}
|
23 |
|
24 |
def recommend_gifts(text: str):
|
25 |
"""Main function to generate gift recommendations"""
|
26 |
if not text:
|
27 |
+
return "β οΈ Please provide a description."
|
28 |
+
|
29 |
try:
|
30 |
+
# Load GPT-2 as a text-generation model
|
31 |
nlp = pipeline(
|
32 |
"text-generation",
|
33 |
model="gpt2",
|
34 |
device_map="auto"
|
35 |
)
|
36 |
+
|
37 |
# Extract relevant keywords
|
38 |
keywords = extract_keywords(text, nlp)
|
39 |
+
|
40 |
# Generate search URLs
|
41 |
search_links = generate_search_urls(keywords)
|
42 |
+
|
43 |
+
# Format the output as clickable links
|
44 |
+
formatted_output = f"""
|
45 |
+
<h3>π Predicted Interests: {", ".join(keywords)}</h3>
|
46 |
+
<h3>π Gift Suggestions:</h3>
|
47 |
+
<ul>
|
48 |
+
<li>{search_links["Amazon India"]}</li>
|
49 |
+
<li>{search_links["Flipkart"]}</li>
|
50 |
+
<li>{search_links["IGP Gifts"]}</li>
|
51 |
+
<li>{search_links["IndiaMart"]}</li>
|
52 |
+
</ul>
|
53 |
+
"""
|
54 |
+
return formatted_output
|
55 |
+
|
56 |
except Exception as e:
|
57 |
+
return f"β Error: {str(e)}"
|
58 |
|
59 |
+
# Create Gradio interface with HTML output
|
60 |
demo = gr.Interface(
|
61 |
fn=recommend_gifts,
|
62 |
inputs=gr.Textbox(
|
63 |
lines=3,
|
64 |
placeholder="Describe who you're buying a gift for (age, interests, occasion, etc.)"
|
65 |
),
|
66 |
+
outputs=gr.HTML(), # Change output type to HTML
|
67 |
title="π Smart Gift Recommender",
|
68 |
description="Get personalized gift suggestions with direct shopping links!",
|
69 |
examples=[
|
70 |
+
["a small kid of age 3 want him to have something like a toy that teaches alphabets"],
|
71 |
["age is 25 and he loves puzzle and online FPS games"],
|
72 |
["Looking for a gift for my mom who enjoys gardening and cooking"]
|
73 |
]
|
74 |
)
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
+
demo.launch()
|