File size: 1,908 Bytes
d47196a
9b7633c
 
1e69485
63cf444
 
 
 
 
d47196a
9b7633c
63cf444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d47196a
63cf444
 
e7b9fde
d47196a
9b7633c
 
 
 
 
63cf444
d47196a
63cf444
d47196a
a205c3f
d47196a
 
 
63cf444
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
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