NTO-TCP-HF / src /components /title_des_gen.py
ishworrsubedii's picture
add: del temp image file
1dcfbb3
"""
project @ NTO-TCP-HF
created @ 2024-10-29
author @ github.com/ishworrsubedii
"""
import os
import google.generativeai as genai
from tempfile import NamedTemporaryFile
class NecklaceProductListing:
def __init__(self, api_key):
genai.configure(api_key=api_key)
self.model = genai.GenerativeModel(model_name="gemini-1.5-flash")
self.prompt = """Analyze this necklace image and create an e-commerce product listing with:
1. An SEO-friendly, compelling product title (3-8 words)
2. A persuasive product description for online shoppers (80-120 words)
Format your response exactly like this:
Title: [Product Title]
Description: [Product Description]
For the description, include:
- Opening hook about the piece's beauty or uniqueness
- Key materials and specifications (metal type, gemstones, length, closure type)
- Highlight 2-3 standout design features
- Styling suggestions (what to wear it with, occasions)
- Quality/craftsmanship mentions
- One emotional benefit (how it makes the wearer feel)
Write in a professional yet warm tone that appeals to online jewelry shoppers. Focus on benefits and value proposition."""
def save_image_tempfile(self, pil_image):
with NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
pil_image.save(temp_file, format="PNG")
return temp_file.name
def image_upload(self, image_path):
sample_file = genai.upload_file(path=image_path, display_name="necklace_image")
return sample_file
def gen_title_desc(self, image):
image_path = self.save_image_tempfile(image)
sample_file = self.image_upload(image_path)
os.remove(image_path)
response = self.model.generate_content([sample_file, self.prompt])
return response.text