ishworrsubedii commited on
Commit
f6f8006
·
1 Parent(s): 5754201

add: title and description generator

Browse files
requirements.txt CHANGED
@@ -78,4 +78,5 @@ uvloop==0.19.0
78
  watchfiles==0.22.0
79
  websockets==12.0
80
  replicate
 
81
  -e .
 
78
  watchfiles==0.22.0
79
  websockets==12.0
80
  replicate
81
+ google-generativeai
82
  -e .
src/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ """
2
+ project @ NTO-TCP-HF
3
+ created @ 2024-10-28
4
+ author @ github/ishworrsubedii
5
+ """
src/api/image_prep_api.py CHANGED
@@ -17,13 +17,17 @@ from fastapi.responses import JSONResponse
17
 
18
  from src.components.auto_crop import crop_transparent_image
19
  from src.components.color_extraction import ColorExtractionRMBG
 
20
 
21
  preprocessing_router = APIRouter()
22
 
23
  rmbg: str = os.getenv("RMBG")
24
 
25
  enhancer: str = os.getenv("ENHANCER")
 
 
26
  color_extraction_rmbg = ColorExtractionRMBG()
 
27
 
28
 
29
  def replicate_bg(input):
@@ -187,3 +191,16 @@ async def remove_background_color_extraction(image: UploadFile = File(...), hex_
187
 
188
  except Exception as e:
189
  raise HTTPException(status_code=500, detail=f"Failed to process image: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  from src.components.auto_crop import crop_transparent_image
19
  from src.components.color_extraction import ColorExtractionRMBG
20
+ from src.components.title_des_gen import NecklaceProductListing
21
 
22
  preprocessing_router = APIRouter()
23
 
24
  rmbg: str = os.getenv("RMBG")
25
 
26
  enhancer: str = os.getenv("ENHANCER")
27
+ prod_listing_api_key: str = os.getenv("PROD_LISTING_API_KEY")
28
+
29
  color_extraction_rmbg = ColorExtractionRMBG()
30
+ product_listing_obj = NecklaceProductListing(prod_listing_api_key)
31
 
32
 
33
  def replicate_bg(input):
 
191
 
192
  except Exception as e:
193
  raise HTTPException(status_code=500, detail=f"Failed to process image: {e}")
194
+
195
+
196
+ @preprocessing_router.post("/title_description_generator")
197
+ async def product_title_description_generator(image: UploadFile = File(...)):
198
+ image_bytes = await image.read()
199
+ image = Image.open(BytesIO(image_bytes)).convert("RGB")
200
+ try:
201
+ result = product_listing_obj.gen_title_desc(image=image)
202
+
203
+ return JSONResponse(content=result, status_code=200)
204
+
205
+ except Exception as e:
206
+ raise HTTPException(status_code=500, detail=f"Failed to process image: {e}")
src/components/title_des_gen.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ project @ NTO-TCP-HF
3
+ created @ 2024-10-29
4
+ author @ github.com/ishworrsubedii
5
+ """
6
+ import google.generativeai as genai
7
+ from tempfile import NamedTemporaryFile
8
+
9
+
10
+ class NecklaceProductListing:
11
+ def __init__(self, api_key):
12
+ genai.configure(api_key=api_key)
13
+ self.model = genai.GenerativeModel(model_name="gemini-1.5-flash")
14
+ self.prompt = """Analyze this necklace image and create an e-commerce product listing with:
15
+ 1. An SEO-friendly, compelling product title (3-8 words)
16
+ 2. A persuasive product description for online shoppers (80-120 words)
17
+ Format your response exactly like this:
18
+ Title: [Product Title]
19
+ Description: [Product Description]
20
+ For the description, include:
21
+ - Opening hook about the piece's beauty or uniqueness
22
+ - Key materials and specifications (metal type, gemstones, length, closure type)
23
+ - Highlight 2-3 standout design features
24
+ - Styling suggestions (what to wear it with, occasions)
25
+ - Quality/craftsmanship mentions
26
+ - One emotional benefit (how it makes the wearer feel)
27
+ Write in a professional yet warm tone that appeals to online jewelry shoppers. Focus on benefits and value proposition."""
28
+
29
+ def save_image_tempfile(self, pil_image):
30
+ with NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
31
+ pil_image.save(temp_file, format="PNG")
32
+ return temp_file.name
33
+
34
+ def image_upload(self, image_path):
35
+ sample_file = genai.upload_file(path=image_path, display_name="necklace_image")
36
+ return sample_file
37
+
38
+ def gen_title_desc(self, image):
39
+ image_path = self.save_image_tempfile(image)
40
+ sample_file = self.image_upload(image_path)
41
+
42
+ response = self.model.generate_content([sample_file, self.prompt])
43
+ return response.text