Spaces:
Sleeping
Sleeping
import gradio as gr | |
from product_recommender import DynamicRecommender | |
import asyncio | |
async def get_recommendations(text: str) -> str: | |
""" | |
Return the final recommendations as a markdown string | |
with product name, price, link, etc. | |
""" | |
try: | |
recommender = DynamicRecommender() | |
results = await recommender.get_recommendations(text) | |
if not results: | |
return "No recommendations found. Possibly the scraping returned nothing." | |
# Build a markdown output | |
# Each product: name, price, link | |
# e.g. "**Name**: iPhone 14\n**Price**: 80,000\n[Open Link](https://...)" | |
lines = [] | |
for i, product in enumerate(results, start=1): | |
name = product.get("name", "Unknown") | |
price = product.get("price", "N/A") | |
url = product.get("url", "#") | |
source = product.get("source", "") | |
# Markdown formatting | |
lines.append( | |
f"**{i}. {name}**\n\n" | |
f"- **Price**: {price}\n" | |
f"- **Source**: {source}\n" | |
f"- **Link**: [View here]({url})\n" | |
f"---" | |
) | |
markdown_output = "\n".join(lines) | |
return markdown_output | |
except Exception as e: | |
return f"Error: {str(e)}" | |
demo = gr.Interface( | |
fn=lambda x: asyncio.run(get_recommendations(x)), | |
inputs=gr.Textbox( | |
lines=3, | |
placeholder="Describe who you're buying a gift for (age, interests, etc.)" | |
), | |
outputs=gr.Markdown(), # or gr.HTML() if you prefer | |
title="π Smart Gift Recommender", | |
description="Get personalized gift suggestions with real-time price comparison!\n\nType something like: 'I need a thoughtful and creative gift for a 25-year-old who loves art...'" | |
) | |
if __name__ == "__main__": | |
demo.launch(server_name="0.0.0.0", server_port=7860) | |
else: | |
app = demo.app |