utkarsh134 commited on
Commit
225d32b
·
verified ·
1 Parent(s): 6879530

Update app/services/logo_service.py

Browse files
Files changed (1) hide show
  1. app/services/logo_service.py +5 -17
app/services/logo_service.py CHANGED
@@ -19,18 +19,10 @@ def get_logo_prompts(brand_name: str, domain: str) -> List[str]:
19
  ]
20
 
21
  def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5) -> List[Dict]:
22
- """
23
- Generate logos and return them as base64-encoded strings.
24
-
25
- Returns:
26
- A list of dictionaries containing:
27
- - id: A unique ID for the logo
28
- - base64_data: The base64-encoded image data
29
- - format: The image format (e.g., 'png')
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
 
@@ -49,12 +41,11 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
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:
@@ -62,13 +53,10 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
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,
@@ -78,7 +66,7 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
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)}")
 
19
  ]
20
 
21
  def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5) -> List[Dict]:
22
+
 
 
 
 
 
 
 
 
23
  api_key = settings.GEMINI_API_KEY
24
 
25
+
26
  client = genai.Client(api_key=api_key)
27
  prompts = get_logo_prompts(brand_name, domain)[:variation_count]
28
 
 
41
  for part in response.candidates[0].content.parts:
42
  if part.inline_data is not None:
43
  try:
44
+
45
  mime_type = part.inline_data.mime_type
46
  image_data = part.inline_data.data
47
 
48
+ image_format = "png"
 
49
  if "jpeg" in mime_type or "jpg" in mime_type:
50
  image_format = "jpeg"
51
  elif "png" in mime_type:
 
53
  elif "webp" in mime_type:
54
  image_format = "webp"
55
 
 
56
  encoded_string = base64.b64encode(image_data).decode('utf-8')
57
 
 
58
  filename = f"{brand_name.replace(' ', '_')}_{idx}.{image_format}"
59
 
 
60
  logo_results.append({
61
  "id": f"logo_{idx}",
62
  "base64_data": encoded_string,
 
66
  except Exception as e:
67
  print(f"Error processing image {idx}: {str(e)}")
68
 
69
+ time.sleep(1)
70
 
71
  except Exception as e:
72
  print(f"Error generating logo {idx}: {str(e)}")