Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,42 @@ import gradio as gr
|
|
| 2 |
from product_recommender import DynamicRecommender
|
| 3 |
import asyncio
|
| 4 |
|
| 5 |
-
async def get_recommendations(text: str) ->
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
recommender = DynamicRecommender()
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
except Exception as e:
|
| 11 |
-
return
|
|
|
|
| 12 |
|
| 13 |
demo = gr.Interface(
|
| 14 |
fn=lambda x: asyncio.run(get_recommendations(x)),
|
|
@@ -16,13 +45,12 @@ demo = gr.Interface(
|
|
| 16 |
lines=3,
|
| 17 |
placeholder="Describe who you're buying a gift for (age, interests, etc.)"
|
| 18 |
),
|
| 19 |
-
outputs=gr.
|
| 20 |
title="π Smart Gift Recommender",
|
| 21 |
-
description="Get personalized gift suggestions with real-time price comparison
|
| 22 |
)
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|
| 25 |
-
# Run on all interfaces and port 7860 by default
|
| 26 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 27 |
else:
|
| 28 |
-
app = demo.app
|
|
|
|
| 2 |
from product_recommender import DynamicRecommender
|
| 3 |
import asyncio
|
| 4 |
|
| 5 |
+
async def get_recommendations(text: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Return the final recommendations as a markdown string
|
| 8 |
+
with product name, price, link, etc.
|
| 9 |
+
"""
|
| 10 |
try:
|
| 11 |
recommender = DynamicRecommender()
|
| 12 |
+
results = await recommender.get_recommendations(text)
|
| 13 |
+
|
| 14 |
+
if not results:
|
| 15 |
+
return "No recommendations found. Possibly the scraping returned nothing."
|
| 16 |
+
|
| 17 |
+
# Build a markdown output
|
| 18 |
+
# Each product: name, price, link
|
| 19 |
+
# e.g. "**Name**: iPhone 14\n**Price**: 80,000\n[Open Link](https://...)"
|
| 20 |
+
lines = []
|
| 21 |
+
for i, product in enumerate(results, start=1):
|
| 22 |
+
name = product.get("name", "Unknown")
|
| 23 |
+
price = product.get("price", "N/A")
|
| 24 |
+
url = product.get("url", "#")
|
| 25 |
+
source = product.get("source", "")
|
| 26 |
+
# Markdown formatting
|
| 27 |
+
lines.append(
|
| 28 |
+
f"**{i}. {name}**\n\n"
|
| 29 |
+
f"- **Price**: {price}\n"
|
| 30 |
+
f"- **Source**: {source}\n"
|
| 31 |
+
f"- **Link**: [View here]({url})\n"
|
| 32 |
+
f"---"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
markdown_output = "\n".join(lines)
|
| 36 |
+
return markdown_output
|
| 37 |
+
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"Error: {str(e)}"
|
| 40 |
+
|
| 41 |
|
| 42 |
demo = gr.Interface(
|
| 43 |
fn=lambda x: asyncio.run(get_recommendations(x)),
|
|
|
|
| 45 |
lines=3,
|
| 46 |
placeholder="Describe who you're buying a gift for (age, interests, etc.)"
|
| 47 |
),
|
| 48 |
+
outputs=gr.Markdown(), # or gr.HTML() if you prefer
|
| 49 |
title="π Smart Gift Recommender",
|
| 50 |
+
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...'"
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
|
|
|
| 54 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 55 |
else:
|
| 56 |
+
app = demo.app
|