Spaces:
Sleeping
Sleeping
Update app/services/logo_service.py
Browse files- app/services/logo_service.py +15 -18
app/services/logo_service.py
CHANGED
|
@@ -5,7 +5,9 @@ from io import BytesIO
|
|
| 5 |
import base64
|
| 6 |
import time
|
| 7 |
import os
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def get_logo_prompts(brand_name: str, domain: str) -> List[str]:
|
| 11 |
return [
|
|
@@ -19,24 +21,23 @@ def get_logo_prompts(brand_name: str, domain: str) -> List[str]:
|
|
| 19 |
def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5) -> List[Dict]:
|
| 20 |
"""
|
| 21 |
Generate logos and return them as base64-encoded strings.
|
| 22 |
-
|
| 23 |
Returns:
|
| 24 |
A list of dictionaries containing:
|
| 25 |
- id: A unique ID for the logo
|
| 26 |
- base64_data: The base64-encoded image data
|
| 27 |
- format: The image format (e.g., 'png')
|
| 28 |
-
- name: A filename for the logo
|
| 29 |
"""
|
|
|
|
| 30 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 31 |
if not api_key:
|
| 32 |
raise ValueError("GEMINI_API_KEY environment variable not set")
|
| 33 |
-
|
| 34 |
-
# Initialize the client
|
| 35 |
client = genai.Client(api_key=api_key)
|
| 36 |
prompts = get_logo_prompts(brand_name, domain)[:variation_count]
|
| 37 |
-
|
| 38 |
logo_results = []
|
| 39 |
-
|
| 40 |
for idx, prompt in enumerate(prompts, 1):
|
| 41 |
try:
|
| 42 |
response = client.models.generate_content(
|
|
@@ -46,31 +47,27 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
|
|
| 46 |
response_modalities=['TEXT', 'IMAGE']
|
| 47 |
)
|
| 48 |
)
|
| 49 |
-
|
| 50 |
for part in response.candidates[0].content.parts:
|
| 51 |
if part.inline_data is not None:
|
| 52 |
-
# Process the image directly in memory
|
| 53 |
image = Image.open(BytesIO(part.inline_data.data))
|
| 54 |
-
|
| 55 |
-
# Convert to base64 directly from memory
|
| 56 |
buffer = BytesIO()
|
| 57 |
image.save(buffer, format='PNG')
|
| 58 |
encoded_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 59 |
-
|
| 60 |
-
# Generate filename (for reference only, not saving)
|
| 61 |
filename = f"{brand_name.replace(' ', '_')}_{idx}.png"
|
| 62 |
-
|
| 63 |
-
# Add to results
|
| 64 |
logo_results.append({
|
| 65 |
"id": f"logo_{idx}",
|
| 66 |
"base64_data": encoded_string,
|
| 67 |
"format": "png",
|
| 68 |
"name": filename
|
| 69 |
})
|
| 70 |
-
|
| 71 |
time.sleep(1) # Short pause between logo variations
|
| 72 |
-
|
| 73 |
except Exception as e:
|
| 74 |
print(f"Error generating logo {idx}: {str(e)}")
|
| 75 |
-
|
| 76 |
return logo_results
|
|
|
|
| 5 |
import base64
|
| 6 |
import time
|
| 7 |
import os
|
| 8 |
+
import uuid
|
| 9 |
+
from typing import List, Dict, Tuple
|
| 10 |
+
from app.config.settings import settings
|
| 11 |
|
| 12 |
def get_logo_prompts(brand_name: str, domain: str) -> List[str]:
|
| 13 |
return [
|
|
|
|
| 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 |
+
|
| 32 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 33 |
if not api_key:
|
| 34 |
raise ValueError("GEMINI_API_KEY environment variable not set")
|
| 35 |
+
|
|
|
|
| 36 |
client = genai.Client(api_key=api_key)
|
| 37 |
prompts = get_logo_prompts(brand_name, domain)[:variation_count]
|
| 38 |
+
|
| 39 |
logo_results = []
|
| 40 |
+
|
| 41 |
for idx, prompt in enumerate(prompts, 1):
|
| 42 |
try:
|
| 43 |
response = client.models.generate_content(
|
|
|
|
| 47 |
response_modalities=['TEXT', 'IMAGE']
|
| 48 |
)
|
| 49 |
)
|
| 50 |
+
|
| 51 |
for part in response.candidates[0].content.parts:
|
| 52 |
if part.inline_data is not None:
|
|
|
|
| 53 |
image = Image.open(BytesIO(part.inline_data.data))
|
| 54 |
+
|
|
|
|
| 55 |
buffer = BytesIO()
|
| 56 |
image.save(buffer, format='PNG')
|
| 57 |
encoded_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 58 |
+
|
|
|
|
| 59 |
filename = f"{brand_name.replace(' ', '_')}_{idx}.png"
|
| 60 |
+
|
|
|
|
| 61 |
logo_results.append({
|
| 62 |
"id": f"logo_{idx}",
|
| 63 |
"base64_data": encoded_string,
|
| 64 |
"format": "png",
|
| 65 |
"name": filename
|
| 66 |
})
|
| 67 |
+
|
| 68 |
time.sleep(1) # Short pause between logo variations
|
| 69 |
+
|
| 70 |
except Exception as e:
|
| 71 |
print(f"Error generating logo {idx}: {str(e)}")
|
| 72 |
+
|
| 73 |
return logo_results
|