noddysnots commited on
Commit
dc7e417
Β·
verified Β·
1 Parent(s): a201c14

Update product_recommender.py

Browse files
Files changed (1) hide show
  1. product_recommender.py +37 -57
product_recommender.py CHANGED
@@ -1,61 +1,41 @@
1
- import gradio as gr
2
- from typing import Dict, List
 
 
 
3
  import json
4
- import urllib.parse
5
- from product_recommender import ProductRecommender
6
 
7
- # Initialize the recommender
8
- recommender = ProductRecommender()
9
-
10
- # Sample product database - replace with your actual database
11
- product_database = [
12
- {
13
- "description": "FIFA 24 EA Sports Football Game",
14
- "category": "games",
15
- "features": ["sports", "multiplayer"],
16
- "price": 4999
17
- },
18
- # Add more products
19
- ]
20
-
21
- def get_recommendations(text: str) -> Dict:
22
- try:
23
- # Get recommendations using the multi-model system
24
- recommendations = recommender.get_recommendations(text, product_database)
25
 
26
- # Format recommendations with shopping links
27
- formatted_recommendations = []
28
- for rec in recommendations:
29
- query = urllib.parse.quote(rec['description'])
30
- formatted_rec = {
31
- "product": rec['description'],
32
- "price": f"β‚Ή{rec['price']}",
33
- "similarity_score": f"{rec.get('similarity', 0):.2f}",
34
- "shopping_links": {
35
- "Amazon": f"https://www.amazon.in/s?k={query}",
36
- "Flipkart": f"https://www.flipkart.com/search?q={query}",
37
- "IGP": f"https://www.igp.com/search?q={query}"
38
- }
39
- }
40
- formatted_recommendations.append(formatted_rec)
41
-
42
- return {
43
- "recommendations": formatted_recommendations
44
- }
45
-
46
- except Exception as e:
47
- return {"error": str(e)}
48
-
49
- # Create Gradio interface
50
- demo = gr.Interface(
51
- fn=get_recommendations,
52
- inputs=gr.Textbox(lines=3),
53
- outputs=gr.JSON(),
54
- title="🎁 Smart Gift Recommender",
55
- description="Get personalized gift suggestions with direct shopping links!"
56
- )
57
 
58
- if __name__ == "__main__":
59
- demo.launch(server_name="0.0.0.0", server_port=7860)
60
- else:
61
- app = demo.app
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoModelForTokenClassification
2
+ from sentence_transformers import SentenceTransformer
3
+ import torch
4
+ import numpy as np
5
+ from typing import Dict, List, Optional
6
  import json
 
 
7
 
8
+ class ProductRecommender:
9
+ def __init__(self):
10
+ self.model_analyzer = MultiModelAnalyzer()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ def get_recommendations(self, query: str, product_database: List[Dict]) -> List[Dict]:
13
+ try:
14
+ query_analysis = self.model_analyzer.analyze_text(query)
15
+
16
+ # For testing, return a simple recommendation
17
+ return [{
18
+ "name": "Test Product",
19
+ "price": "β‚Ή999",
20
+ "category": "test",
21
+ "similarity": 0.95
22
+ }]
23
+ except Exception as e:
24
+ print(f"Error in recommendations: {str(e)}")
25
+ return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ class MultiModelAnalyzer:
28
+ def __init__(self):
29
+ try:
30
+ self.category_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
31
+ self.category_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
32
+ self.semantic_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
33
+ except Exception as e:
34
+ print(f"Error initializing models: {str(e)}")
35
+
36
+ def analyze_text(self, text: str) -> Dict:
37
+ return {
38
+ "category": "test",
39
+ "embedding": np.zeros(10),
40
+ "features": ["test"]
41
+ }