Ozgur Unlu commited on
Commit
d867642
1 Parent(s): 531606c

last few changes, switched to custom sentiments

Browse files
Files changed (1) hide show
  1. app.py +51 -9
app.py CHANGED
@@ -17,7 +17,37 @@ except LookupError:
17
  # Global variables to cache models
18
  CACHED_MODELS = {}
19
 
20
- # Initialize models and tokenizers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def load_models():
22
  global CACHED_MODELS
23
 
@@ -241,12 +271,12 @@ def generate_content(
241
 
242
  # Check sentiment and safety
243
  try:
244
- sentiment = sentiment_analyzer(post)[0]
245
  safety_check = content_checker(post)[0]
246
 
247
  filtered_content.append({
248
  'text': post,
249
- 'sentiment': sentiment['label'],
250
  'safety_score': f"{float(safety_check.get('score', 0)):.2f}"
251
  })
252
  except Exception as e:
@@ -255,7 +285,7 @@ def generate_content(
255
 
256
  return filtered_content if filtered_content else [{
257
  'text': create_post(),
258
- 'sentiment': 'positive',
259
  'safety_score': '1.00'
260
  }]
261
 
@@ -263,7 +293,7 @@ def generate_content(
263
  print(f"Error in content generation: {str(e)}")
264
  return [{
265
  'text': f"Introducing {product_name}: {product_description[:100]}... Learn more!",
266
- 'sentiment': 'neutral',
267
  'safety_score': '1.00'
268
  }]
269
 
@@ -297,15 +327,14 @@ Processing your request..."""
297
  "Analyzing product information...",
298
  "Generating content variations...",
299
  "Checking content safety...",
300
- "Finalizing output..."
301
  ]
302
 
303
  for i, step in enumerate(steps, 1):
304
- progress(i/len(steps))
305
  yield features_list + f"\n\n⏳ {step}"
306
  time.sleep(1)
307
 
308
- # Generate actual content
309
  try:
310
  results = generate_content(
311
  product_name,
@@ -321,8 +350,21 @@ Processing your request..."""
321
  content_checker
322
  )
323
 
 
 
324
  output = "🎯 Generated Marketing Content:\n\n"
 
325
  for i, content in enumerate(results, 1):
 
 
 
 
 
 
 
 
 
 
326
  output += f"Version {i}:\n"
327
  output += f"📝 Content: {content['text']}\n"
328
  output += f"😊 Sentiment: {content['sentiment']}\n"
@@ -364,7 +406,7 @@ def create_interface():
364
  "Fill the form with sample data",
365
  variant="primary",
366
  size="sm",
367
- scale=1
368
  )
369
 
370
  # Main content area with two columns
 
17
  # Global variables to cache models
18
  CACHED_MODELS = {}
19
 
20
+ def analyze_detailed_sentiment(text):
21
+ """Custom function to determine more specific sentiment based on content analysis"""
22
+ sentiment_indicators = {
23
+ 'excited': ['amazing', 'exciting', 'incredible', 'transform', 'revolutionary'],
24
+ 'happy': ['happy', 'joy', 'enjoy', 'perfect', 'wonderful'],
25
+ 'cheerful': ['bright', 'fun', 'delightful', 'cheerful', 'pleasant'],
26
+ 'proud': ['proud', 'achievement', 'excellence', 'premium', 'superior'],
27
+ 'elated': ['extraordinary', 'exceptional', 'outstanding', 'remarkable'],
28
+ 'inspired': ['innovative', 'creative', 'inspiring', 'groundbreaking'],
29
+ 'confident': ['guaranteed', 'proven', 'trusted', 'reliable', 'assured'],
30
+ 'loving': ['love', 'care', 'cherish', 'adore', 'treasure'],
31
+ 'enthusiastic': ['fantastic', 'awesome', 'brilliant', 'excellent'],
32
+ 'delighted': ['pleased', 'satisfied', 'gratified', 'overjoyed']
33
+ }
34
+
35
+ text_lower = text.lower()
36
+
37
+ # Count matches for each sentiment
38
+ sentiment_scores = {}
39
+ for sentiment, keywords in sentiment_indicators.items():
40
+ score = sum(1 for keyword in keywords if keyword in text_lower)
41
+ if score > 0:
42
+ sentiment_scores[sentiment] = score
43
+
44
+ # If no specific sentiment is detected, return a default
45
+ if not sentiment_scores:
46
+ return 'positive'
47
+
48
+ # Return the sentiment with the highest score
49
+ return max(sentiment_scores.items(), key=lambda x: x[1])[0]
50
+
51
  def load_models():
52
  global CACHED_MODELS
53
 
 
271
 
272
  # Check sentiment and safety
273
  try:
274
+ sentiment = analyze_detailed_sentiment(post) # Use our custom sentiment analysis
275
  safety_check = content_checker(post)[0]
276
 
277
  filtered_content.append({
278
  'text': post,
279
+ 'sentiment': sentiment.title(), # Capitalize the sentiment
280
  'safety_score': f"{float(safety_check.get('score', 0)):.2f}"
281
  })
282
  except Exception as e:
 
285
 
286
  return filtered_content if filtered_content else [{
287
  'text': create_post(),
288
+ 'sentiment': 'Positive',
289
  'safety_score': '1.00'
290
  }]
291
 
 
293
  print(f"Error in content generation: {str(e)}")
294
  return [{
295
  'text': f"Introducing {product_name}: {product_description[:100]}... Learn more!",
296
+ 'sentiment': 'Neutral',
297
  'safety_score': '1.00'
298
  }]
299
 
 
327
  "Analyzing product information...",
328
  "Generating content variations...",
329
  "Checking content safety...",
330
+ "Performing final adjustments..." # Changed the last step name
331
  ]
332
 
333
  for i, step in enumerate(steps, 1):
334
+ progress((i/len(steps)) * 0.99) # Modified to max out at 99%
335
  yield features_list + f"\n\n⏳ {step}"
336
  time.sleep(1)
337
 
 
338
  try:
339
  results = generate_content(
340
  product_name,
 
350
  content_checker
351
  )
352
 
353
+ # Ensure different sentiments for each version
354
+ used_sentiments = set()
355
  output = "🎯 Generated Marketing Content:\n\n"
356
+
357
  for i, content in enumerate(results, 1):
358
+ # Ensure different sentiment for each version
359
+ if content['sentiment'].lower() in used_sentiments:
360
+ alternative_sentiments = ['Confident', 'Enthusiastic', 'Inspired', 'Proud', 'Happy']
361
+ for alt_sentiment in alternative_sentiments:
362
+ if alt_sentiment.lower() not in used_sentiments:
363
+ content['sentiment'] = alt_sentiment
364
+ break
365
+
366
+ used_sentiments.add(content['sentiment'].lower())
367
+
368
  output += f"Version {i}:\n"
369
  output += f"📝 Content: {content['text']}\n"
370
  output += f"😊 Sentiment: {content['sentiment']}\n"
 
406
  "Fill the form with sample data",
407
  variant="primary",
408
  size="sm",
409
+ scale=0.2
410
  )
411
 
412
  # Main content area with two columns