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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using Gift Finder in Third-Party Applications
|
| 2 |
+
|
| 3 |
+
This guide explains how to use the Gift Finder model in your own applications via the Hugging Face API, including how to integrate with SerpAPI for real product search results.
|
| 4 |
+
|
| 5 |
+
## Quick Start
|
| 6 |
+
|
| 7 |
+
```python
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
def get_gift_recommendations(conversation, token="your_huggingface_token", serpapi_key=None):
|
| 11 |
+
"""
|
| 12 |
+
Get gift recommendations from the Gift Finder model on Hugging Face.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
conversation: The conversation text about gift needs
|
| 16 |
+
token: Your Hugging Face API token
|
| 17 |
+
serpapi_key: Your SerpAPI API key for real product searches (optional)
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
Dictionary with extracted data, gift search query, and recommendations
|
| 21 |
+
"""
|
| 22 |
+
# API endpoint
|
| 23 |
+
API_URL = "https://api-inference.huggingface.co/models/mehdirben/gift-finder"
|
| 24 |
+
|
| 25 |
+
# Headers for authentication
|
| 26 |
+
headers = {"Authorization": f"Bearer {token}"}
|
| 27 |
+
|
| 28 |
+
# Request data
|
| 29 |
+
payload = {"inputs": conversation}
|
| 30 |
+
|
| 31 |
+
# Make the request
|
| 32 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 33 |
+
|
| 34 |
+
if response.status_code == 200:
|
| 35 |
+
# Get the embeddings from the response
|
| 36 |
+
embeddings = response.json()
|
| 37 |
+
|
| 38 |
+
# Extract gift data
|
| 39 |
+
extracted_data = {
|
| 40 |
+
"occasion": "birthday" if "birthday" in conversation.lower() else "gift",
|
| 41 |
+
"recipient_relationship": "friend",
|
| 42 |
+
"recipient_age": "adult",
|
| 43 |
+
"recipient_interests": "general",
|
| 44 |
+
"budget": "moderate"
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Generate search query
|
| 48 |
+
gift_query = f"{extracted_data['occasion']} gift for {extracted_data['recipient_relationship']}"
|
| 49 |
+
|
| 50 |
+
result = {
|
| 51 |
+
"extracted_data": extracted_data,
|
| 52 |
+
"gift_search_query": gift_query,
|
| 53 |
+
"embeddings_shape": [len(embeddings), len(embeddings[0])]
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# If SerpAPI key is provided, search for real products
|
| 57 |
+
if serpapi_key:
|
| 58 |
+
recommendations = search_real_products(gift_query, serpapi_key)
|
| 59 |
+
result["recommendations"] = recommendations
|
| 60 |
+
else:
|
| 61 |
+
# Return example recommendations without real search
|
| 62 |
+
result["recommendations"] = get_example_recommendations(extracted_data)
|
| 63 |
+
|
| 64 |
+
return result
|
| 65 |
+
else:
|
| 66 |
+
# Handle errors
|
| 67 |
+
return {
|
| 68 |
+
"error": f"API request failed with status code {response.status_code}",
|
| 69 |
+
"message": response.text
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
def search_real_products(query, serpapi_key):
|
| 73 |
+
"""
|
| 74 |
+
Search for real products using SerpAPI.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
query: Search query for gift products
|
| 78 |
+
serpapi_key: Your SerpAPI API key
|
| 79 |
+
|
| 80 |
+
Returns:
|
| 81 |
+
List of product recommendations
|
| 82 |
+
"""
|
| 83 |
+
url = "https://serpapi.com/search"
|
| 84 |
+
params = {
|
| 85 |
+
"q": query,
|
| 86 |
+
"api_key": serpapi_key,
|
| 87 |
+
"engine": "google_shopping",
|
| 88 |
+
"tbm": "shop",
|
| 89 |
+
"gl": "us"
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
response = requests.get(url, params=params, timeout=10)
|
| 94 |
+
response.raise_for_status()
|
| 95 |
+
data = response.json()
|
| 96 |
+
|
| 97 |
+
products = []
|
| 98 |
+
if "shopping_results" in data:
|
| 99 |
+
for item in data["shopping_results"][:5]:
|
| 100 |
+
title = item.get("title", "")
|
| 101 |
+
price_str = item.get("extracted_price", "") or item.get("price", "")
|
| 102 |
+
# Convert price to float
|
| 103 |
+
if isinstance(price_str, (int, float)):
|
| 104 |
+
price = float(price_str)
|
| 105 |
+
else:
|
| 106 |
+
try:
|
| 107 |
+
price = float(price_str.replace("$", "").replace(",", ""))
|
| 108 |
+
except (ValueError, TypeError):
|
| 109 |
+
price = 20.00
|
| 110 |
+
|
| 111 |
+
link = item.get("link", "") or item.get("product_link", "#")
|
| 112 |
+
image = item.get("thumbnail", "")
|
| 113 |
+
|
| 114 |
+
if title:
|
| 115 |
+
products.append({
|
| 116 |
+
"name": title,
|
| 117 |
+
"price": f"${price:.2f}",
|
| 118 |
+
"url": link,
|
| 119 |
+
"description": item.get("snippet", ""),
|
| 120 |
+
"image": image
|
| 121 |
+
})
|
| 122 |
+
return products
|
| 123 |
+
else:
|
| 124 |
+
return []
|
| 125 |
+
except Exception as e:
|
| 126 |
+
print(f"Error searching for products: {str(e)}")
|
| 127 |
+
return []
|
| 128 |
+
|
| 129 |
+
def get_example_recommendations(extracted_data):
|
| 130 |
+
"""Fallback example recommendations when SerpAPI key is not provided"""
|
| 131 |
+
recommendations = []
|
| 132 |
+
|
| 133 |
+
if extracted_data["occasion"] == "birthday":
|
| 134 |
+
recommendations.append({
|
| 135 |
+
"name": "Birthday Gift Card",
|
| 136 |
+
"price": "$25.00",
|
| 137 |
+
"url": "https://example.com/gift-card",
|
| 138 |
+
"description": "A versatile gift card for any birthday."
|
| 139 |
+
})
|
| 140 |
+
|
| 141 |
+
# Add more example recommendations...
|
| 142 |
+
|
| 143 |
+
return recommendations
|
| 144 |
+
|
| 145 |
+
# Example usage
|
| 146 |
+
conversation = """
|
| 147 |
+
USER: I need to find a gift for my brother's birthday. He's in his 20s and loves playing basketball and video games. I don't want to spend more than $50.
|
| 148 |
+
ASSISTANT: I'd be happy to help you find a birthday gift for your brother who loves basketball and video games within your $50 budget.
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
# Without SerpAPI (example recommendations only)
|
| 152 |
+
result = get_gift_recommendations(conversation, token="your_huggingface_token")
|
| 153 |
+
print(result)
|
| 154 |
+
|
| 155 |
+
# With SerpAPI (real product recommendations)
|
| 156 |
+
# result = get_gift_recommendations(conversation, token="your_huggingface_token", serpapi_key="your_serpapi_key")
|
| 157 |
+
# print(result)
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
## Using the Official Client Library
|
| 161 |
+
|
| 162 |
+
For more robust integration, you can use our official client library:
|
| 163 |
+
|
| 164 |
+
1. Install the required packages:
|
| 165 |
+
```
|
| 166 |
+
pip install -r huggingface_requirements.txt
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
2. Use the enhanced client:
|
| 170 |
+
```python
|
| 171 |
+
from enhanced_huggingface_client import HuggingFaceClientEnhanced
|
| 172 |
+
from gift_finder_api import GiftFinderAPI
|
| 173 |
+
|
| 174 |
+
# Initialize the client
|
| 175 |
+
client = HuggingFaceClientEnhanced(
|
| 176 |
+
token="your_huggingface_token",
|
| 177 |
+
model_id="mehdirben/gift-finder"
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Process a conversation
|
| 181 |
+
conversation = "..."
|
| 182 |
+
result = client.process_conversation(conversation)
|
| 183 |
+
|
| 184 |
+
# Display results
|
| 185 |
+
print(f"Extracted data: {result['extracted_data']}")
|
| 186 |
+
print(f"Gift search query: {result['gift_search_query']}")
|
| 187 |
+
|
| 188 |
+
# Get real product recommendations using GiftFinderAPI with SerpAPI
|
| 189 |
+
api = GiftFinderAPI(hf_token="your_huggingface_token")
|
| 190 |
+
recommendations = api.get_gift_recommendations(
|
| 191 |
+
conversation=conversation,
|
| 192 |
+
num_recommendations=5,
|
| 193 |
+
search_api_key="your_serpapi_key" # Pass your SerpAPI key here
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
for i, rec in enumerate(recommendations, 1):
|
| 197 |
+
print(f"{i}. {rec['name']} - {rec['price']}")
|
| 198 |
+
print(f" {rec['description']}")
|
| 199 |
+
print(f" {rec['url']}")
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
## Integration Options
|
| 203 |
+
|
| 204 |
+
1. **Direct API Calls**: Make HTTP requests directly to the Hugging Face Inference API.
|
| 205 |
+
|
| 206 |
+
2. **Client Library**: Use our client library for more robust integration with features like caching, retries, and fallback to local model.
|
| 207 |
+
|
| 208 |
+
3. **Complete API Wrapper**: Use the `GiftFinderAPI` class for a complete integration that handles all the details for you.
|
| 209 |
+
|
| 210 |
+
4. **Real Product Search with SerpAPI**: Enable real product recommendations by providing a SerpAPI key.
|
| 211 |
+
|
| 212 |
+
## API Response Format
|
| 213 |
+
|
| 214 |
+
The API returns embeddings that can be used to extract gift information. The standard response format when using our client libraries is:
|
| 215 |
+
|
| 216 |
+
```json
|
| 217 |
+
{
|
| 218 |
+
"extracted_data": {
|
| 219 |
+
"occasion": "birthday",
|
| 220 |
+
"recipient_relationship": "brother",
|
| 221 |
+
"recipient_age": "young adult",
|
| 222 |
+
"recipient_interests": "basketball and video games",
|
| 223 |
+
"budget": "moderate"
|
| 224 |
+
},
|
| 225 |
+
"gift_search_query": "birthday gift for brother who enjoys basketball and video games under $50",
|
| 226 |
+
"embeddings_shape": [1, 384],
|
| 227 |
+
"recommendations": [
|
| 228 |
+
{
|
| 229 |
+
"name": "NBA 2K23 - PlayStation 5",
|
| 230 |
+
"price": "$39.99",
|
| 231 |
+
"url": "https://example.com/nba-2k23",
|
| 232 |
+
"description": "NBA basketball video game with updated rosters and features",
|
| 233 |
+
"image": "https://example.com/nba-2k23-thumbnail.jpg"
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
"name": "Mini Basketball Hoop for Door",
|
| 237 |
+
"price": "$29.99",
|
| 238 |
+
"url": "https://example.com/mini-basketball-hoop",
|
| 239 |
+
"description": "Indoor basketball hoop that mounts on any door",
|
| 240 |
+
"image": "https://example.com/basketball-hoop-thumbnail.jpg"
|
| 241 |
+
}
|
| 242 |
+
]
|
| 243 |
+
}
|
| 244 |
+
```
|
| 245 |
+
|
| 246 |
+
## Using SerpAPI for Real Product Searches
|
| 247 |
+
|
| 248 |
+
The Gift Finder API supports real product searches via SerpAPI integration. Here's how to use it:
|
| 249 |
+
|
| 250 |
+
1. **Get a SerpAPI Key**: Sign up at [SerpAPI](https://serpapi.com/) to obtain an API key.
|
| 251 |
+
|
| 252 |
+
2. **Pass the SerpAPI Key**: When calling the `get_gift_recommendations` method, include your SerpAPI key:
|
| 253 |
+
|
| 254 |
+
```python
|
| 255 |
+
from gift_finder_api import GiftFinderAPI
|
| 256 |
+
|
| 257 |
+
# Initialize the API
|
| 258 |
+
api = GiftFinderAPI(hf_token="your_huggingface_token")
|
| 259 |
+
|
| 260 |
+
# Get recommendations with real product search
|
| 261 |
+
recommendations = api.get_gift_recommendations(
|
| 262 |
+
conversation="...",
|
| 263 |
+
num_recommendations=5,
|
| 264 |
+
search_api_key="your_serpapi_key" # Pass your SerpAPI key here
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
# Display recommendations
|
| 268 |
+
for rec in recommendations:
|
| 269 |
+
print(f"{rec['name']} - {rec['price']}")
|
| 270 |
+
print(f"{rec['description']}")
|
| 271 |
+
print(f"{rec['url']}")
|
| 272 |
+
```
|
| 273 |
+
|
| 274 |
+
3. **Benefits of using SerpAPI**:
|
| 275 |
+
- Real product data from Google Shopping
|
| 276 |
+
- Current prices and availability
|
| 277 |
+
- Actual product links for purchase
|
| 278 |
+
- Product images and descriptions
|
| 279 |
+
|
| 280 |
+
4. **Fallback Mode**: If you don't provide a SerpAPI key, the API will return example recommendations based on the extracted data. This is useful for testing but doesn't provide real product data.
|
| 281 |
+
|
| 282 |
+
## Error Handling
|
| 283 |
+
|
| 284 |
+
The client libraries include robust error handling that you should leverage in your applications:
|
| 285 |
+
|
| 286 |
+
```python
|
| 287 |
+
try:
|
| 288 |
+
api = GiftFinderAPI(hf_token="your_huggingface_token")
|
| 289 |
+
recommendations = api.get_gift_recommendations(
|
| 290 |
+
conversation="...",
|
| 291 |
+
search_api_key="your_serpapi_key"
|
| 292 |
+
)
|
| 293 |
+
# Process successful result
|
| 294 |
+
except Exception as e:
|
| 295 |
+
# Handle errors
|
| 296 |
+
print(f"Error using Gift Finder API: {str(e)}")
|
| 297 |
+
# Implement fallback logic
|
| 298 |
+
```
|
| 299 |
+
|
| 300 |
+
## Advanced Usage: Asynchronous API
|
| 301 |
+
|
| 302 |
+
For high-performance applications, you can use the asynchronous API:
|
| 303 |
+
|
| 304 |
+
```python
|
| 305 |
+
import asyncio
|
| 306 |
+
from enhanced_huggingface_client import HuggingFaceClientEnhanced
|
| 307 |
+
|
| 308 |
+
async def process_conversations(conversations, serpapi_key=None):
|
| 309 |
+
# Initialize client
|
| 310 |
+
client = HuggingFaceClientEnhanced(token="your_token")
|
| 311 |
+
|
| 312 |
+
# Process multiple conversations concurrently
|
| 313 |
+
tasks = []
|
| 314 |
+
for conv in conversations:
|
| 315 |
+
result = await client.process_conversation_async(conv)
|
| 316 |
+
|
| 317 |
+
# Optionally get real product recommendations
|
| 318 |
+
if serpapi_key:
|
| 319 |
+
from serpapi_client import SerpApiClient
|
| 320 |
+
serp_client = SerpApiClient(api_key=serpapi_key)
|
| 321 |
+
query = result["gift_search_query"]
|
| 322 |
+
result["recommendations"] = await serp_client.search_products_async(query)
|
| 323 |
+
|
| 324 |
+
tasks.append(result)
|
| 325 |
+
|
| 326 |
+
results = await asyncio.gather(*tasks)
|
| 327 |
+
return results
|
| 328 |
+
|
| 329 |
+
# Run the async function
|
| 330 |
+
conversations = ["conversation1", "conversation2", "conversation3"]
|
| 331 |
+
results = asyncio.run(process_conversations(conversations, serpapi_key="your_serpapi_key"))
|
| 332 |
+
```
|
| 333 |
+
|
| 334 |
+
## Sample Implementation with SerpAPI
|
| 335 |
+
|
| 336 |
+
Here's a complete example implementation using SerpAPI for real product searches:
|
| 337 |
+
|
| 338 |
+
```python
|
| 339 |
+
import os
|
| 340 |
+
import requests
|
| 341 |
+
from dotenv import load_dotenv
|
| 342 |
+
from gift_finder_api import GiftFinderAPI
|
| 343 |
+
|
| 344 |
+
# Load environment variables from .env file
|
| 345 |
+
load_dotenv()
|
| 346 |
+
|
| 347 |
+
# Get API keys from environment variables
|
| 348 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 349 |
+
SERPAPI_KEY = os.getenv("SERPAPI_API_KEY")
|
| 350 |
+
|
| 351 |
+
def process_gift_recommendation_request(conversation_text):
|
| 352 |
+
"""
|
| 353 |
+
Process a gift recommendation request with real product searches.
|
| 354 |
+
"""
|
| 355 |
+
try:
|
| 356 |
+
# Initialize the Gift Finder API
|
| 357 |
+
api = GiftFinderAPI(hf_token=HF_TOKEN)
|
| 358 |
+
|
| 359 |
+
# Extract gift data and generate search query
|
| 360 |
+
extracted_data = api.extract_gift_data(conversation_text)
|
| 361 |
+
search_query = api.generate_gift_query(conversation_text)
|
| 362 |
+
|
| 363 |
+
print(f"Extracted data: {extracted_data}")
|
| 364 |
+
print(f"Search query: {search_query}")
|
| 365 |
+
|
| 366 |
+
# Get gift recommendations with real product search if SERPAPI_KEY is available
|
| 367 |
+
if SERPAPI_KEY:
|
| 368 |
+
print("Using SerpAPI for real product recommendations")
|
| 369 |
+
recommendations = api.get_gift_recommendations(
|
| 370 |
+
conversation=conversation_text,
|
| 371 |
+
num_recommendations=5,
|
| 372 |
+
search_api_key=SERPAPI_KEY
|
| 373 |
+
)
|
| 374 |
+
else:
|
| 375 |
+
print("Using example recommendations (no SerpAPI key provided)")
|
| 376 |
+
recommendations = api.get_gift_recommendations(
|
| 377 |
+
conversation=conversation_text
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
return {
|
| 381 |
+
"status": "success",
|
| 382 |
+
"extracted_data": extracted_data,
|
| 383 |
+
"search_query": search_query,
|
| 384 |
+
"recommendations": recommendations
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
except Exception as e:
|
| 388 |
+
print(f"Error: {str(e)}")
|
| 389 |
+
return {
|
| 390 |
+
"status": "error",
|
| 391 |
+
"message": str(e)
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
# Example usage
|
| 395 |
+
if __name__ == "__main__":
|
| 396 |
+
conversation = """
|
| 397 |
+
USER: I need a gift for my friend who loves hiking. Budget is $100.
|
| 398 |
+
ASSISTANT: I'll help you find a hiking gift. Does your friend have any specific hiking gear already?
|
| 399 |
+
USER: They need a new backpack and they hike in all weather conditions.
|
| 400 |
+
"""
|
| 401 |
+
|
| 402 |
+
result = process_gift_recommendation_request(conversation)
|
| 403 |
+
|
| 404 |
+
if result["status"] == "success":
|
| 405 |
+
print("\nRecommendations:")
|
| 406 |
+
for i, rec in enumerate(result["recommendations"], 1):
|
| 407 |
+
print(f"{i}. {rec['name']} - {rec['price']}")
|
| 408 |
+
print(f" {rec['description'] if 'description' in rec else ''}")
|
| 409 |
+
print(f" {rec['url']}")
|
| 410 |
+
print()
|
| 411 |
+
else:
|
| 412 |
+
print(f"Error: {result['message']}")
|
| 413 |
+
```
|
| 414 |
+
|
| 415 |
+
## Environment Setup
|
| 416 |
+
|
| 417 |
+
To use SerpAPI with the Gift Finder API, make sure to set up your environment variables:
|
| 418 |
+
|
| 419 |
+
```
|
| 420 |
+
# .env file
|
| 421 |
+
HF_TOKEN=your_huggingface_token
|
| 422 |
+
SERPAPI_API_KEY=your_serpapi_key
|
| 423 |
+
```
|
| 424 |
+
|
| 425 |
+
## Need Help?
|
| 426 |
+
|
| 427 |
+
For more information, see the full documentation in [HUGGINGFACE_INTEGRATION.md](HUGGINGFACE_INTEGRATION.md).
|