raksama19 commited on
Commit
95c40e3
Β·
verified Β·
1 Parent(s): 5a9132b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -4
app.py CHANGED
@@ -404,7 +404,7 @@ def initialize_gemini_model():
404
  print("Initializing Gemini API...")
405
  genai.configure(api_key=gemini_api_key)
406
  gemini_model = genai.GenerativeModel('gemma-3n-e4b-it')
407
- print("βœ… Gemini API model ready")
408
  return gemini_model
409
  except Exception as e:
410
  print(f"❌ Error initializing Gemini model: {e}")
@@ -419,6 +419,7 @@ def generate_alt_text_for_image(pil_image):
419
  # Initialize Gemini model
420
  model = initialize_gemini_model()
421
  if model is None:
 
422
  return "Image description unavailable"
423
 
424
  # Create a detailed prompt for alt text generation
@@ -432,9 +433,17 @@ Focus on:
432
 
433
  Provide a descriptive alt text in 1-2 sentences that is informative but not overly verbose. Start directly with the description without saying "This image shows" or similar phrases."""
434
 
435
- # Generate alt text using Gemini API
 
 
436
  response = model.generate_content([prompt, pil_image])
437
- alt_text = response.text.strip() if hasattr(response, 'text') else "Image description unavailable"
 
 
 
 
 
 
438
 
439
  # Clean up the alt text
440
  alt_text = alt_text.replace('\n', ' ').replace('\r', ' ')
@@ -448,7 +457,9 @@ Provide a descriptive alt text in 1-2 sentences that is informative but not over
448
  return alt_text if alt_text else "Image description unavailable"
449
 
450
  except Exception as e:
451
- print(f"Error generating alt text: {e}")
 
 
452
  return "Image description unavailable"
453
 
454
 
 
404
  print("Initializing Gemini API...")
405
  genai.configure(api_key=gemini_api_key)
406
  gemini_model = genai.GenerativeModel('gemma-3n-e4b-it')
407
+ print("βœ… Gemini API model ready (gemma-3n-e4b-it)")
408
  return gemini_model
409
  except Exception as e:
410
  print(f"❌ Error initializing Gemini model: {e}")
 
419
  # Initialize Gemini model
420
  model = initialize_gemini_model()
421
  if model is None:
422
+ print("❌ Gemini model not initialized for alt text generation")
423
  return "Image description unavailable"
424
 
425
  # Create a detailed prompt for alt text generation
 
433
 
434
  Provide a descriptive alt text in 1-2 sentences that is informative but not overly verbose. Start directly with the description without saying "This image shows" or similar phrases."""
435
 
436
+ print(f"πŸ” Generating alt text for image...")
437
+
438
+ # Generate alt text using Gemini API with proper multimodal input
439
  response = model.generate_content([prompt, pil_image])
440
+
441
+ if hasattr(response, 'text') and response.text:
442
+ alt_text = response.text.strip()
443
+ print(f"βœ… Alt text generated: {alt_text[:100]}...")
444
+ else:
445
+ print(f"❌ No text in response: {response}")
446
+ return "Image description unavailable"
447
 
448
  # Clean up the alt text
449
  alt_text = alt_text.replace('\n', ' ').replace('\r', ' ')
 
457
  return alt_text if alt_text else "Image description unavailable"
458
 
459
  except Exception as e:
460
+ print(f"❌ Error generating alt text: {e}")
461
+ import traceback
462
+ traceback.print_exc()
463
  return "Image description unavailable"
464
 
465