File size: 1,898 Bytes
f6f8006
 
 
 
 
1dcfbb3
 
f6f8006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dcfbb3
f6f8006
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
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