Spaces:
Sleeping
Sleeping
import gradio as gr | |
from typing import Dict, List | |
import json | |
import urllib.parse | |
from product_recommender import ProductRecommender | |
# Initialize the recommender | |
recommender = ProductRecommender() | |
# Sample product database - replace with your actual database | |
product_database = [ | |
{ | |
"description": "FIFA 24 EA Sports Football Game", | |
"category": "games", | |
"features": ["sports", "multiplayer"], | |
"price": 4999 | |
}, | |
# Add more products | |
] | |
def get_recommendations(text: str) -> Dict: | |
try: | |
# Get recommendations using the multi-model system | |
recommendations = recommender.get_recommendations(text, product_database) | |
# Format recommendations with shopping links | |
formatted_recommendations = [] | |
for rec in recommendations: | |
query = urllib.parse.quote(rec['description']) | |
formatted_rec = { | |
"product": rec['description'], | |
"price": f"βΉ{rec['price']}", | |
"similarity_score": f"{rec.get('similarity', 0):.2f}", | |
"shopping_links": { | |
"Amazon": f"https://www.amazon.in/s?k={query}", | |
"Flipkart": f"https://www.flipkart.com/search?q={query}", | |
"IGP": f"https://www.igp.com/search?q={query}" | |
} | |
} | |
formatted_recommendations.append(formatted_rec) | |
return { | |
"recommendations": formatted_recommendations | |
} | |
except Exception as e: | |
return {"error": str(e)} | |
# Create Gradio interface | |
demo = gr.Interface( | |
fn=get_recommendations, | |
inputs=gr.Textbox(lines=3), | |
outputs=gr.JSON(), | |
title="π Smart Gift Recommender", | |
description="Get personalized gift suggestions with direct shopping links!" | |
) | |
if __name__ == "__main__": | |
demo.launch(server_name="0.0.0.0", server_port=7860) | |
else: | |
app = demo.app |