mike23415 commited on
Commit
da07614
Β·
verified Β·
1 Parent(s): 63d018d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +184 -50
app.py CHANGED
@@ -19,26 +19,81 @@ import time
19
  app = Flask(__name__)
20
  CORS(app)
21
 
22
- # Initialize AI models
23
  print("Loading AI models...")
 
 
 
 
 
 
 
 
 
24
  try:
25
- # Image captioning for smart suggestions
26
- caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
27
- caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
28
 
29
- # Text generation for meme text
30
- text_generator = pipeline("text-generation",
31
- model="microsoft/DialoGPT-medium",
32
- tokenizer="microsoft/DialoGPT-medium")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Sentiment analysis for mood-based memes
35
- sentiment_analyzer = pipeline("sentiment-analysis",
36
- model="cardiffnlp/twitter-roberta-base-sentiment-latest")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- print("Models loaded successfully!")
39
  except Exception as e:
40
- print(f"Error loading models: {e}")
41
- # Fallback to mock responses if models fail to load
 
 
 
42
 
43
  # Meme templates and trending data
44
  MEME_TEMPLATES = {
@@ -71,27 +126,66 @@ class MemeAI:
71
 
72
  def analyze_image(self, image):
73
  """Analyze image and generate smart suggestions"""
74
- try:
75
- # Generate caption
76
- inputs = caption_processor(image, return_tensors="pt")
77
- out = caption_model.generate(**inputs, max_length=50)
78
- caption = caption_processor.decode(out[0], skip_special_tokens=True)
79
-
80
- # Extract key objects/concepts
81
- keywords = self.extract_keywords(caption)
82
-
83
- return {
84
- "caption": caption,
85
- "keywords": keywords,
86
- "suggestions": self.generate_text_suggestions(keywords)
87
- }
88
- except Exception as e:
89
- print(f"Image analysis error: {e}")
90
- return {
91
- "caption": "Image uploaded",
92
- "keywords": ["general"],
93
- "suggestions": self.get_generic_suggestions()
94
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  def extract_keywords(self, text):
97
  """Extract meaningful keywords from image caption"""
@@ -149,20 +243,55 @@ class MemeAI:
149
 
150
  def analyze_mood(self, text):
151
  """Analyze text mood for personalized suggestions"""
152
- try:
153
- result = sentiment_analyzer(text)[0]
154
- mood = result['label'].lower()
155
- confidence = result['score']
156
-
157
- mood_suggestions = {
158
- 'positive': ["You're killing it!", "Main character energy", "That's the spirit!"],
159
- 'negative': ["This is fine", "Why are we here?", "Everything is chaos"],
160
- 'neutral': ["It be like that sometimes", "Just vibing", "No thoughts, head empty"]
161
- }
162
-
163
- return mood_suggestions.get(mood, mood_suggestions['neutral'])
164
- except:
165
- return self.get_generic_suggestions()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  def get_trending_suggestions(self):
168
  """Generate suggestions based on trending topics"""
@@ -216,7 +345,12 @@ meme_ai = MemeAI()
216
 
217
  @app.route('/health', methods=['GET'])
218
  def health_check():
219
- return jsonify({"status": "healthy", "timestamp": datetime.now().isoformat()})
 
 
 
 
 
220
 
221
  @app.route('/analyze-image', methods=['POST'])
222
  def analyze_image():
 
19
  app = Flask(__name__)
20
  CORS(app)
21
 
22
+ # Initialize AI models with fallback options
23
  print("Loading AI models...")
24
+
25
+ # Global flags for model availability
26
+ MODELS_AVAILABLE = {
27
+ 'caption': False,
28
+ 'text_gen': False,
29
+ 'sentiment': False
30
+ }
31
+
32
+ # Try to load models with fallbacks
33
  try:
34
+ # Set cache directory with proper permissions
35
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/transformers_cache'
36
+ os.environ['HF_HOME'] = '/tmp/hf_cache'
37
 
38
+ # Create cache directories
39
+ os.makedirs('/tmp/transformers_cache', exist_ok=True)
40
+ os.makedirs('/tmp/hf_cache', exist_ok=True)
41
+
42
+ print("Attempting to load image captioning model...")
43
+ caption_processor = BlipProcessor.from_pretrained(
44
+ "Salesforce/blip-image-captioning-base",
45
+ cache_dir='/tmp/transformers_cache',
46
+ use_fast=False
47
+ )
48
+ caption_model = BlipForConditionalGeneration.from_pretrained(
49
+ "Salesforce/blip-image-captioning-base",
50
+ cache_dir='/tmp/transformers_cache'
51
+ )
52
+ MODELS_AVAILABLE['caption'] = True
53
+ print("βœ“ Image captioning model loaded")
54
+
55
+ except Exception as e:
56
+ print(f"⚠ Image captioning model failed: {e}")
57
+ caption_processor = None
58
+ caption_model = None
59
+
60
+ try:
61
+ print("Attempting to load sentiment analyzer...")
62
+ sentiment_analyzer = pipeline(
63
+ "sentiment-analysis",
64
+ model="cardiffnlp/twitter-roberta-base-sentiment-latest",
65
+ cache_dir='/tmp/transformers_cache'
66
+ )
67
+ MODELS_AVAILABLE['sentiment'] = True
68
+ print("βœ“ Sentiment analyzer loaded")
69
 
70
+ except Exception as e:
71
+ print(f"⚠ Sentiment analyzer failed: {e}")
72
+ # Use a lighter fallback model
73
+ try:
74
+ sentiment_analyzer = pipeline("sentiment-analysis", cache_dir='/tmp/transformers_cache')
75
+ MODELS_AVAILABLE['sentiment'] = True
76
+ print("βœ“ Fallback sentiment analyzer loaded")
77
+ except:
78
+ sentiment_analyzer = None
79
+ print("⚠ All sentiment models failed")
80
+
81
+ try:
82
+ print("Attempting to load text generator...")
83
+ text_generator = pipeline(
84
+ "text-generation",
85
+ model="gpt2", # Lighter model
86
+ cache_dir='/tmp/transformers_cache'
87
+ )
88
+ MODELS_AVAILABLE['text_gen'] = True
89
+ print("βœ“ Text generator loaded")
90
 
 
91
  except Exception as e:
92
+ print(f"⚠ Text generator failed: {e}")
93
+ text_generator = None
94
+
95
+ print(f"Models loaded: {sum(MODELS_AVAILABLE.values())}/3")
96
+ print("Fallback systems enabled for failed models")
97
 
98
  # Meme templates and trending data
99
  MEME_TEMPLATES = {
 
126
 
127
  def analyze_image(self, image):
128
  """Analyze image and generate smart suggestions"""
129
+ if MODELS_AVAILABLE['caption'] and caption_processor and caption_model:
130
+ try:
131
+ # Generate caption using AI model
132
+ inputs = caption_processor(image, return_tensors="pt")
133
+ out = caption_model.generate(**inputs, max_length=50)
134
+ caption = caption_processor.decode(out[0], skip_special_tokens=True)
135
+
136
+ # Extract key objects/concepts
137
+ keywords = self.extract_keywords(caption)
138
+
139
+ return {
140
+ "caption": caption,
141
+ "keywords": keywords,
142
+ "suggestions": self.generate_text_suggestions(keywords),
143
+ "ai_powered": True
144
+ }
145
+ except Exception as e:
146
+ print(f"AI image analysis error: {e}")
147
+
148
+ # Fallback to rule-based analysis
149
+ return self.analyze_image_fallback(image)
150
+
151
+ def analyze_image_fallback(self, image):
152
+ """Fallback image analysis when AI models aren't available"""
153
+ # Simple image property analysis
154
+ width, height = image.size
155
+ mode = image.mode
156
+
157
+ # Basic heuristics
158
+ keywords = []
159
+ caption = "Image uploaded"
160
+
161
+ # Aspect ratio heuristics
162
+ aspect_ratio = width / height
163
+ if aspect_ratio > 1.5:
164
+ keywords.extend(["landscape", "wide", "horizontal"])
165
+ caption = "Wide image uploaded"
166
+ elif aspect_ratio < 0.7:
167
+ keywords.extend(["portrait", "vertical", "tall"])
168
+ caption = "Tall image uploaded"
169
+ else:
170
+ keywords.extend(["square", "balanced"])
171
+ caption = "Square image uploaded"
172
+
173
+ # Color mode heuristics
174
+ if mode == "RGBA":
175
+ keywords.append("transparent")
176
+ elif mode == "L":
177
+ keywords.extend(["grayscale", "black and white"])
178
+
179
+ # Add generic meme-friendly keywords
180
+ keywords.extend(["meme", "funny", "relatable"])
181
+
182
+ return {
183
+ "caption": caption,
184
+ "keywords": keywords[:5],
185
+ "suggestions": self.generate_text_suggestions(keywords),
186
+ "ai_powered": False,
187
+ "note": "Using fallback analysis - upgrade for AI-powered insights"
188
+ }
189
 
190
  def extract_keywords(self, text):
191
  """Extract meaningful keywords from image caption"""
 
243
 
244
  def analyze_mood(self, text):
245
  """Analyze text mood for personalized suggestions"""
246
+ if MODELS_AVAILABLE['sentiment'] and sentiment_analyzer:
247
+ try:
248
+ result = sentiment_analyzer(text)[0]
249
+ # Handle different sentiment model outputs
250
+ if 'label' in result:
251
+ mood = result['label'].lower()
252
+ # Convert labels to standard format
253
+ if 'pos' in mood or mood == 'positive':
254
+ mood = 'positive'
255
+ elif 'neg' in mood or mood == 'negative':
256
+ mood = 'negative'
257
+ else:
258
+ mood = 'neutral'
259
+ else:
260
+ mood = 'neutral'
261
+
262
+ confidence = result.get('score', 0.5)
263
+
264
+ mood_suggestions = {
265
+ 'positive': ["You're killing it!", "Main character energy", "That's the spirit!", "Living your best life"],
266
+ 'negative': ["This is fine", "Why are we here?", "Everything is chaos", "Big oof energy"],
267
+ 'neutral': ["It be like that sometimes", "Just vibing", "No thoughts, head empty", "Existing peacefully"]
268
+ }
269
+
270
+ suggestions = mood_suggestions.get(mood, mood_suggestions['neutral'])
271
+ return suggestions + [f"When you're feeling {mood}"]
272
+
273
+ except Exception as e:
274
+ print(f"Sentiment analysis error: {e}")
275
+
276
+ # Fallback mood analysis
277
+ return self.analyze_mood_fallback(text)
278
+
279
+ def analyze_mood_fallback(self, text):
280
+ """Fallback mood analysis using keyword matching"""
281
+ text_lower = text.lower()
282
+
283
+ positive_words = ['good', 'great', 'awesome', 'happy', 'love', 'best', 'amazing', 'wonderful']
284
+ negative_words = ['bad', 'awful', 'hate', 'worst', 'terrible', 'sad', 'angry', 'frustrated']
285
+
286
+ pos_count = sum(1 for word in positive_words if word in text_lower)
287
+ neg_count = sum(1 for word in negative_words if word in text_lower)
288
+
289
+ if pos_count > neg_count:
290
+ return ["You're killing it!", "Positive vibes only", "That energy though"]
291
+ elif neg_count > pos_count:
292
+ return ["This is fine", "We've all been there", "Mood honestly"]
293
+ else:
294
+ return ["It be like that", "Just existing", "Neutral chaos energy"]
295
 
296
  def get_trending_suggestions(self):
297
  """Generate suggestions based on trending topics"""
 
345
 
346
  @app.route('/health', methods=['GET'])
347
  def health_check():
348
+ return jsonify({
349
+ "status": "healthy",
350
+ "timestamp": datetime.now().isoformat(),
351
+ "models_loaded": MODELS_AVAILABLE,
352
+ "ai_features": sum(MODELS_AVAILABLE.values())
353
+ })
354
 
355
  @app.route('/analyze-image', methods=['POST'])
356
  def analyze_image():