utkarsh134 commited on
Commit
6879530
·
verified ·
1 Parent(s): 689187b

Update app/services/logo_service.py

Browse files
Files changed (1) hide show
  1. app/services/logo_service.py +31 -15
app/services/logo_service.py CHANGED
@@ -30,6 +30,7 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
30
  """
31
  api_key = settings.GEMINI_API_KEY
32
 
 
33
  client = genai.Client(api_key=api_key)
34
  prompts = get_logo_prompts(brand_name, domain)[:variation_count]
35
 
@@ -47,22 +48,37 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
47
 
48
  for part in response.candidates[0].content.parts:
49
  if part.inline_data is not None:
50
- image = Image.open(BytesIO(part.inline_data.data))
51
-
52
- buffer = BytesIO()
53
- image.save(buffer, format='PNG')
54
- encoded_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
55
-
56
- filename = f"{brand_name.replace(' ', '_')}_{idx}.png"
57
-
58
- logo_results.append({
59
- "id": f"logo_{idx}",
60
- "base64_data": encoded_string,
61
- "format": "png",
62
- "name": filename
63
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- time.sleep(3)
66
 
67
  except Exception as e:
68
  print(f"Error generating logo {idx}: {str(e)}")
 
30
  """
31
  api_key = settings.GEMINI_API_KEY
32
 
33
+ # Initialize the client
34
  client = genai.Client(api_key=api_key)
35
  prompts = get_logo_prompts(brand_name, domain)[:variation_count]
36
 
 
48
 
49
  for part in response.candidates[0].content.parts:
50
  if part.inline_data is not None:
51
+ try:
52
+ # Get the mime type and data
53
+ mime_type = part.inline_data.mime_type
54
+ image_data = part.inline_data.data
55
+
56
+ # Determine image format from mime type
57
+ image_format = "png" # Default
58
+ if "jpeg" in mime_type or "jpg" in mime_type:
59
+ image_format = "jpeg"
60
+ elif "png" in mime_type:
61
+ image_format = "png"
62
+ elif "webp" in mime_type:
63
+ image_format = "webp"
64
+
65
+ # Encode directly to base64 without attempting to open with PIL
66
+ encoded_string = base64.b64encode(image_data).decode('utf-8')
67
+
68
+ # Generate filename (for reference only)
69
+ filename = f"{brand_name.replace(' ', '_')}_{idx}.{image_format}"
70
+
71
+ # Add to results
72
+ logo_results.append({
73
+ "id": f"logo_{idx}",
74
+ "base64_data": encoded_string,
75
+ "format": image_format,
76
+ "name": filename
77
+ })
78
+ except Exception as e:
79
+ print(f"Error processing image {idx}: {str(e)}")
80
 
81
+ time.sleep(1) # Short pause between logo variations
82
 
83
  except Exception as e:
84
  print(f"Error generating logo {idx}: {str(e)}")