selvaonline commited on
Commit
aa20c05
·
verified ·
1 Parent(s): 0cf3ada

Upload demo.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. demo.py +86 -0
demo.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Demo script for using the Shopping Assistant model with the Hugging Face Inference API
4
+ """
5
+ import requests
6
+ import json
7
+ import argparse
8
+
9
+ def query_model(text, api_token=None, model_id="selvaonline/shopping-assistant"):
10
+ """
11
+ Query the model using the Hugging Face Inference API
12
+ """
13
+ api_url = f"https://api-inference.huggingface.co/models/{model_id}"
14
+
15
+ headers = {}
16
+ if api_token:
17
+ headers["Authorization"] = f"Bearer {api_token}"
18
+
19
+ payload = {
20
+ "inputs": text,
21
+ "options": {
22
+ "wait_for_model": True
23
+ }
24
+ }
25
+
26
+ response = requests.post(api_url, headers=headers, json=payload)
27
+
28
+ if response.status_code == 200:
29
+ return response.json()
30
+ else:
31
+ print(f"Error: {response.status_code}")
32
+ print(response.text)
33
+ return None
34
+
35
+ def process_results(results, text):
36
+ """
37
+ Process the results from the Inference API
38
+ """
39
+ if not results or not isinstance(results, list) or len(results) == 0:
40
+ return f"No results found for '{text}'"
41
+
42
+ # The API returns logits, we need to convert them to probabilities
43
+ import numpy as np
44
+
45
+ # Apply sigmoid to convert logits to probabilities
46
+ probabilities = 1 / (1 + np.exp(-np.array(results[0])))
47
+
48
+ # Define the categories (should match the model's categories)
49
+ categories = ["electronics", "clothing", "home", "kitchen", "toys", "other"]
50
+
51
+ # Get the top categories
52
+ top_categories = []
53
+ for i, score in enumerate(probabilities):
54
+ if score > 0.5: # Threshold for multi-label classification
55
+ top_categories.append((categories[i], float(score)))
56
+
57
+ # Sort by score
58
+ top_categories.sort(key=lambda x: x[1], reverse=True)
59
+
60
+ # Format the results
61
+ if top_categories:
62
+ result = f"Top categories for '{text}':\n"
63
+ for category, score in top_categories:
64
+ result += f" {category}: {score:.4f}\n"
65
+
66
+ result += f"\nBased on your query, I would recommend looking for deals in the {top_categories[0][0]} category."
67
+ else:
68
+ result = f"No categories found for '{text}'. Please try a different query."
69
+
70
+ return result
71
+
72
+ def main():
73
+ parser = argparse.ArgumentParser(description="Demo for using the Shopping Assistant model with the Hugging Face Inference API")
74
+ parser.add_argument("--text", type=str, required=True, help="Text to classify")
75
+ parser.add_argument("--token", type=str, help="Hugging Face API token")
76
+ parser.add_argument("--model-id", type=str, default="selvaonline/shopping-assistant", help="Hugging Face model ID")
77
+ args = parser.parse_args()
78
+
79
+ # Query the model
80
+ results = query_model(args.text, args.token, args.model_id)
81
+
82
+ # Process and print the results
83
+ print(process_results(results, args.text))
84
+
85
+ if __name__ == "__main__":
86
+ main()