Spaces:
Sleeping
Sleeping
Update app/services/logo_service.py
Browse files- app/services/logo_service.py +34 -23
app/services/logo_service.py
CHANGED
|
@@ -8,17 +8,18 @@ import os
|
|
| 8 |
import uuid
|
| 9 |
from pathlib import Path
|
| 10 |
from typing import List, Dict, Tuple
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
TEMP_LOGO_DIR.mkdir(parents=True, exist_ok=True)
|
| 14 |
|
| 15 |
def get_logo_prompts(brand_name: str, domain: str) -> List[str]:
|
| 16 |
return [
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
]
|
| 23 |
|
| 24 |
def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5) -> List[Dict]:
|
|
@@ -35,12 +36,18 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
|
|
| 35 |
if not api_key:
|
| 36 |
raise ValueError("GEMINI_API_KEY environment variable not set")
|
| 37 |
|
|
|
|
| 38 |
client = genai.Client(api_key=api_key)
|
| 39 |
prompts = get_logo_prompts(brand_name, domain)[:variation_count]
|
| 40 |
|
|
|
|
| 41 |
session_id = str(uuid.uuid4())
|
| 42 |
-
session_dir = TEMP_LOGO_DIR / session_id
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
logo_results = []
|
| 46 |
|
|
@@ -57,15 +64,18 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
|
|
| 57 |
|
| 58 |
for part in response.candidates[0].content.parts:
|
| 59 |
if part.inline_data is not None:
|
|
|
|
| 60 |
image = Image.open(BytesIO(part.inline_data.data))
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
image.save(
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
|
|
|
| 69 |
logo_results.append({
|
| 70 |
"id": f"logo_{idx}",
|
| 71 |
"base64_data": encoded_string,
|
|
@@ -73,12 +83,13 @@ def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5
|
|
| 73 |
"name": filename
|
| 74 |
})
|
| 75 |
|
| 76 |
-
time.sleep(1)
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Error generating logo {idx}: {str(e)}")
|
| 80 |
|
| 81 |
finally:
|
|
|
|
| 82 |
cleanup_session(session_id)
|
| 83 |
|
| 84 |
return logo_results
|
|
@@ -87,14 +98,14 @@ def cleanup_session(session_id: str):
|
|
| 87 |
"""
|
| 88 |
Delete the temporary directory for a session.
|
| 89 |
"""
|
| 90 |
-
session_dir = TEMP_LOGO_DIR / session_id
|
| 91 |
-
if session_dir.exists():
|
| 92 |
-
for file in session_dir.iterdir():
|
| 93 |
-
try:
|
| 94 |
-
file.unlink()
|
| 95 |
-
except:
|
| 96 |
-
pass
|
| 97 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
session_dir.rmdir()
|
| 99 |
except:
|
| 100 |
-
pass
|
|
|
|
| 8 |
import uuid
|
| 9 |
from pathlib import Path
|
| 10 |
from typing import List, Dict, Tuple
|
| 11 |
+
from app.config.settings import settings
|
| 12 |
|
| 13 |
+
# Ensure the base temp directory exists
|
| 14 |
+
settings.TEMP_LOGO_DIR.mkdir(parents=True, exist_ok=True)
|
| 15 |
|
| 16 |
def get_logo_prompts(brand_name: str, domain: str) -> List[str]:
|
| 17 |
return [
|
| 18 |
+
f"Design a professional and elegant logo for {brand_name}, specializing in {domain}. Perhaps incorporate a subtle icon or abstract mark with a sophisticated color palette.",
|
| 19 |
+
f"Create a creative and unique logo for {brand_name}, focusing on {domain} solutions. Think outside the box with an unexpected symbol or a clever visual metaphor.",
|
| 20 |
+
f"Generate a vibrant and playful logo for {brand_name}, within the {domain} sector. Consider using bright colors and friendly shapes.",
|
| 21 |
+
f"Create a natural and organic logo for {brand_name}, operating in the {domain} space. Explore earthy tones and flowing lines or leaf-like elements.",
|
| 22 |
+
f"Design an abstract and conceptual logo for {brand_name}, related to {domain}. Focus on conveying the essence of the brand through non-literal imagery.",
|
| 23 |
]
|
| 24 |
|
| 25 |
def generate_logos_base64(brand_name: str, domain: str, variation_count: int = 5) -> List[Dict]:
|
|
|
|
| 36 |
if not api_key:
|
| 37 |
raise ValueError("GEMINI_API_KEY environment variable not set")
|
| 38 |
|
| 39 |
+
# Initialize the client
|
| 40 |
client = genai.Client(api_key=api_key)
|
| 41 |
prompts = get_logo_prompts(brand_name, domain)[:variation_count]
|
| 42 |
|
| 43 |
+
# Create a temporary session directory
|
| 44 |
session_id = str(uuid.uuid4())
|
| 45 |
+
session_dir = settings.TEMP_LOGO_DIR / session_id
|
| 46 |
+
try:
|
| 47 |
+
session_dir.mkdir(parents=True, exist_ok=True)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Warning: Could not create session directory: {e}")
|
| 50 |
+
session_dir = settings.TEMP_LOGO_DIR # Fallback to base temp directory
|
| 51 |
|
| 52 |
logo_results = []
|
| 53 |
|
|
|
|
| 64 |
|
| 65 |
for part in response.candidates[0].content.parts:
|
| 66 |
if part.inline_data is not None:
|
| 67 |
+
# Process the image directly in memory
|
| 68 |
image = Image.open(BytesIO(part.inline_data.data))
|
| 69 |
|
| 70 |
+
# Convert to base64 directly from memory
|
| 71 |
+
buffer = BytesIO()
|
| 72 |
+
image.save(buffer, format='PNG')
|
| 73 |
+
encoded_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 74 |
|
| 75 |
+
# Generate filename (even if we don't save it)
|
| 76 |
+
filename = f"{brand_name.replace(' ', '_')}_{idx}.png"
|
| 77 |
|
| 78 |
+
# Add to results
|
| 79 |
logo_results.append({
|
| 80 |
"id": f"logo_{idx}",
|
| 81 |
"base64_data": encoded_string,
|
|
|
|
| 83 |
"name": filename
|
| 84 |
})
|
| 85 |
|
| 86 |
+
time.sleep(1) # Short pause between logo variations
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Error generating logo {idx}: {str(e)}")
|
| 90 |
|
| 91 |
finally:
|
| 92 |
+
# Clean up the temporary directory if it was created
|
| 93 |
cleanup_session(session_id)
|
| 94 |
|
| 95 |
return logo_results
|
|
|
|
| 98 |
"""
|
| 99 |
Delete the temporary directory for a session.
|
| 100 |
"""
|
| 101 |
+
session_dir = settings.TEMP_LOGO_DIR / session_id
|
| 102 |
+
if session_dir.exists() and session_dir != settings.TEMP_LOGO_DIR: # Safety check
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
try:
|
| 104 |
+
for file in session_dir.iterdir():
|
| 105 |
+
try:
|
| 106 |
+
file.unlink()
|
| 107 |
+
except:
|
| 108 |
+
pass
|
| 109 |
session_dir.rmdir()
|
| 110 |
except:
|
| 111 |
+
pass # Ignore errors during cleanup
|