selvaonline commited on
Commit
3e43710
·
verified ·
1 Parent(s): 378c8b0

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +144 -17
app.py CHANGED
@@ -3,6 +3,85 @@ import torch
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import json
5
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Load the model and tokenizer
8
  model_path = os.path.dirname(os.path.abspath(__file__))
@@ -17,11 +96,16 @@ except Exception as e:
17
  print(f"Error loading categories: {str(e)}")
18
  categories = ["electronics", "clothing", "home", "kitchen", "toys", "other"]
19
 
20
- def classify_text(text):
 
 
 
21
  """
22
- Classify the text using the model
23
  """
24
- # Prepare the input
 
 
25
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
26
 
27
  # Get the model prediction
@@ -38,31 +122,73 @@ def classify_text(text):
38
  # Sort by score
39
  top_categories.sort(key=lambda x: x[1], reverse=True)
40
 
41
- # Format the results
42
  if top_categories:
43
  result = f"Top categories for '{text}':\n\n"
44
  for category, score in top_categories:
45
  result += f"- {category}: {score:.4f}\n"
46
 
47
- result += f"\nBased on your query, I would recommend looking for deals in the **{top_categories[0][0]}** category."
48
  else:
49
- result = f"No categories found for '{text}'. Please try a different query."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  return result
52
 
53
  # Create the Gradio interface
54
  demo = gr.Interface(
55
  fn=classify_text,
56
- inputs=gr.Textbox(
57
- lines=2,
58
- placeholder="Enter your shopping query here...",
59
- label="Shopping Query"
60
- ),
 
 
 
 
 
 
 
61
  outputs=gr.Markdown(label="Results"),
62
  title="Shopping Assistant",
63
  description="""
64
- This demo shows how to use the Shopping Assistant model to classify shopping queries into categories.
65
- Enter a shopping query below to see which categories it belongs to.
66
 
67
  Examples:
68
  - "I'm looking for headphones"
@@ -71,10 +197,11 @@ demo = gr.Interface(
71
  - "I need a new smart TV"
72
  """,
73
  examples=[
74
- ["I'm looking for headphones"],
75
- ["Do you have any kitchen appliance deals?"],
76
- ["Show me the best laptop deals"],
77
- ["I need a new smart TV"]
 
78
  ],
79
  theme=gr.themes.Soft()
80
  )
 
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import json
5
  import os
6
+ import requests
7
+ import re
8
+
9
+ # Function to extract text from HTML (from shopping_assistant.py)
10
+ def extract_text_from_html(html):
11
+ """
12
+ Extract text from HTML without using BeautifulSoup
13
+ """
14
+ # Remove HTML tags
15
+ text = re.sub(r'<[^>]+>', ' ', html)
16
+ # Remove extra whitespace
17
+ text = re.sub(r'\s+', ' ', text)
18
+ # Decode HTML entities
19
+ text = text.replace('&nbsp;', ' ').replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>')
20
+ return text.strip()
21
+
22
+ # Function to fetch deals from DealsFinders.com (from shopping_assistant.py)
23
+ def fetch_deals_data(url="https://www.dealsfinders.com/wp-json/wp/v2/posts", num_pages=2, per_page=100):
24
+ """
25
+ Fetch deals data exclusively from the DealsFinders API
26
+ """
27
+ all_deals = []
28
+
29
+ # Fetch from the DealsFinders API
30
+ for page in range(1, num_pages + 1):
31
+ try:
32
+ # Add a user agent to avoid being blocked
33
+ headers = {
34
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
35
+ }
36
+ response = requests.get(f"{url}?page={page}&per_page={per_page}", headers=headers)
37
+
38
+ if response.status_code == 200:
39
+ deals = response.json()
40
+ all_deals.extend(deals)
41
+ print(f"Fetched page {page} with {len(deals)} deals from DealsFinders API")
42
+
43
+ # If we get fewer deals than requested, we've reached the end
44
+ if len(deals) < per_page:
45
+ print(f"Reached the end of available deals at page {page}")
46
+ break
47
+ else:
48
+ print(f"Failed to fetch page {page} from DealsFinders API: {response.status_code}")
49
+ break
50
+ except Exception as e:
51
+ print(f"Error fetching page {page} from DealsFinders API: {str(e)}")
52
+ break
53
+
54
+ return all_deals
55
+
56
+ # Function to process deals data (from shopping_assistant.py)
57
+ def process_deals_data(deals_data):
58
+ """
59
+ Process the deals data into a structured format
60
+ """
61
+ processed_deals = []
62
+
63
+ for deal in deals_data:
64
+ try:
65
+ # Extract relevant information using our HTML text extractor
66
+ content_html = deal.get('content', {}).get('rendered', '')
67
+ excerpt_html = deal.get('excerpt', {}).get('rendered', '')
68
+
69
+ clean_content = extract_text_from_html(content_html)
70
+ clean_excerpt = extract_text_from_html(excerpt_html)
71
+
72
+ processed_deal = {
73
+ 'id': deal.get('id'),
74
+ 'title': deal.get('title', {}).get('rendered', ''),
75
+ 'link': deal.get('link', ''),
76
+ 'date': deal.get('date', ''),
77
+ 'content': clean_content,
78
+ 'excerpt': clean_excerpt
79
+ }
80
+ processed_deals.append(processed_deal)
81
+ except Exception as e:
82
+ print(f"Error processing deal: {str(e)}")
83
+
84
+ return processed_deals
85
 
86
  # Load the model and tokenizer
87
  model_path = os.path.dirname(os.path.abspath(__file__))
 
96
  print(f"Error loading categories: {str(e)}")
97
  categories = ["electronics", "clothing", "home", "kitchen", "toys", "other"]
98
 
99
+ # Global variable to store deals data
100
+ deals_cache = None
101
+
102
+ def classify_text(text, fetch_deals=True):
103
  """
104
+ Classify the text using the model and fetch relevant deals
105
  """
106
+ global deals_cache
107
+
108
+ # Prepare the input for classification
109
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
110
 
111
  # Get the model prediction
 
122
  # Sort by score
123
  top_categories.sort(key=lambda x: x[1], reverse=True)
124
 
125
+ # Format the classification results
126
  if top_categories:
127
  result = f"Top categories for '{text}':\n\n"
128
  for category, score in top_categories:
129
  result += f"- {category}: {score:.4f}\n"
130
 
131
+ result += f"\nBased on your query, I would recommend looking for deals in the **{top_categories[0][0]}** category.\n\n"
132
  else:
133
+ result = f"No categories found for '{text}'. Please try a different query.\n\n"
134
+
135
+ # Fetch and display deals if requested
136
+ if fetch_deals:
137
+ result += "## Relevant Deals from DealsFinders.com\n\n"
138
+
139
+ try:
140
+ # Fetch deals data if not already cached
141
+ if deals_cache is None:
142
+ deals_data = fetch_deals_data(num_pages=2) # Limit to 2 pages for faster response
143
+ deals_cache = process_deals_data(deals_data)
144
+
145
+ # Search for relevant deals
146
+ query_terms = text.lower().split()
147
+ relevant_deals = []
148
+
149
+ for deal in deals_cache:
150
+ title = deal['title'].lower()
151
+ content = deal['content'].lower()
152
+ excerpt = deal['excerpt'].lower()
153
+
154
+ # Check if any query term is in the deal information
155
+ if any(term in title or term in content or term in excerpt for term in query_terms):
156
+ relevant_deals.append(deal)
157
+
158
+ # Limit to top 5 most relevant deals
159
+ relevant_deals = relevant_deals[:5]
160
+
161
+ if relevant_deals:
162
+ for i, deal in enumerate(relevant_deals, 1):
163
+ result += f"{i}. [{deal['title']}]({deal['link']})\n\n"
164
+ else:
165
+ result += "No specific deals found for your query. Try a different search term or browse the recommended category.\n\n"
166
+
167
+ except Exception as e:
168
+ result += f"Error fetching deals: {str(e)}\n\n"
169
 
170
  return result
171
 
172
  # Create the Gradio interface
173
  demo = gr.Interface(
174
  fn=classify_text,
175
+ inputs=[
176
+ gr.Textbox(
177
+ lines=2,
178
+ placeholder="Enter your shopping query here...",
179
+ label="Shopping Query"
180
+ ),
181
+ gr.Checkbox(
182
+ label="Fetch Deals",
183
+ value=True,
184
+ info="Check to fetch and display deals from DealsFinders.com"
185
+ )
186
+ ],
187
  outputs=gr.Markdown(label="Results"),
188
  title="Shopping Assistant",
189
  description="""
190
+ This demo shows how to use the Shopping Assistant model to classify shopping queries into categories and find relevant deals.
191
+ Enter a shopping query below to see which categories it belongs to and find deals from DealsFinders.com.
192
 
193
  Examples:
194
  - "I'm looking for headphones"
 
197
  - "I need a new smart TV"
198
  """,
199
  examples=[
200
+ ["I'm looking for headphones", True],
201
+ ["Do you have any kitchen appliance deals?", True],
202
+ ["Show me the best laptop deals", True],
203
+ ["I need a new smart TV", True],
204
+ ["headphone deals", True]
205
  ],
206
  theme=gr.themes.Soft()
207
  )