Spaces:
Sleeping
Sleeping
File size: 3,700 Bytes
58d6553 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# recommender.py
from transformers import pipeline
zero_shot = pipeline(
"zero-shot-classification",
model="typeform/distilbert-base-uncased-mnli",
device=-1
)
def classify(text, labels):
"""Perform zero-shot classification with proper label mapping."""
result = zero_shot(text, labels, multi_label=True)
return [
{"label": res, "score": score}
for res, score in zip(result["labels"], result["scores"])
]
from .utils.text_processors import (
extract_age,
extract_gender,
extract_interests,
extract_dislikes
)
class GiftRecommender:
def __init__(self):
self.zero_shot = pipeline("zero-shot-classification")
self.sentiment = pipeline("sentiment-analysis")
# List of possible interest categories
self.interest_categories = [
"art", "music", "sports", "technology", "reading",
"travel", "cooking", "gaming", "fashion", "outdoor activities"
]
# Pre-defined gift suggestions for each category
self.gift_rules = {
"art": ["art supplies set", "digital drawing tablet", "museum membership"],
"music": ["wireless headphones", "concert tickets", "vinyl records"],
"sports": ["fitness tracker", "sports equipment", "team merchandise"],
"technology": ["smart devices", "electronics", "tech gadgets"],
"gaming": ["gaming console", "gaming accessories", "game subscription"],
"travel": ["travel gear", "language courses", "travel guides"],
"reading": ["e-reader", "book subscription", "rare books"],
"cooking": ["cooking classes", "kitchen gadgets", "recipe books"]
}
def get_gift_recommendations(self, text: str):
# Build the user's profile from the text
profile = {
'age': extract_age(text),
'gender': extract_gender(text),
'interests': extract_interests(text, self.interest_categories),
'dislikes': extract_dislikes(text)
}
# Match each extracted interest to possible gift ideas
recommendations = []
for interest in profile['interests']:
cat = interest['category']
if cat in self.gift_rules:
for gift in self.gift_rules[cat]:
recommendations.append({
'gift': gift,
'category': cat,
'reason': f"Based on interest in {interest['phrase']}"
})
# Limit to top 5 for demonstration
return {'profile': profile, 'recommendations': recommendations[:5]}
def format_recommendations(self, results: dict) -> str:
output = []
output.append("π Gift Recommendations\n")
profile = results['profile']
output.append("Profile Summary:")
output.append(f"Age: {profile['age'] or 'Unknown'}")
output.append(f"Gender: {profile['gender'].title()}")
if profile['interests']:
output.append("Interests: " + ", ".join(i['phrase'] for i in profile['interests']))
if profile['dislikes']:
output.append("Dislikes: " + ", ".join(profile['dislikes']))
if results['recommendations']:
output.append("\nTop Recommendations:")
for i, rec in enumerate(results['recommendations'], 1):
output.append(f"{i}. {rec['gift']}")
output.append(f" β’ {rec['reason']}")
return "\n".join(output)
|