forked from Varsha-1605/SocioSell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_processor.py
103 lines (88 loc) · 3.66 KB
/
image_processor.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import google.generativeai as genai
import os
from dotenv import load_dotenv
import logging
from PIL import Image
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ImageProcessor:
def __init__(self):
load_dotenv()
api_key = os.getenv('GOOGLE_API_KEY')
if not api_key:
raise ValueError("GOOGLE_API_KEY not found in environment variables")
genai.configure(api_key=api_key)
self.model = genai.GenerativeModel("gemini-1.5-pro-latest")
async def analyze_product(self, image: Image.Image):
"""Analyze product image and return structured data"""
try:
analysis_prompt = [
"""Analyze this product image and provide detailed information in the following format exactly:
BEGIN_ANALYSIS
Product Name: [exact product name]
Category: [main category/subcategory]
Description: [2-3 sentences about the product]
Price: [visible pricing information]
Key Features:
- [feature 1]
- [feature 2]
- [feature 3]
Search Keywords:
- [keyword 1]
- [keyword 2]
- [keyword 3]
END_ANALYSIS""",
image
]
response = self.model.generate_content(analysis_prompt)
analysis_dict = self._parse_analysis(response.text)
analysis_dict['status'] = 'success'
return analysis_dict
except Exception as e:
logger.error(f"Error in analyze_product: {str(e)}")
return {
'status': 'error',
'message': str(e)
}
def _parse_analysis(self, text):
"""Parse the analysis text into structured format"""
analysis_dict = {
'product_name': '',
'category': '',
'description': '',
'price': '',
'key_features': [],
'search_keywords': []
}
try:
if 'BEGIN_ANALYSIS' in text and 'END_ANALYSIS' in text:
content = text.split('BEGIN_ANALYSIS')[-1].split('END_ANALYSIS')[0].strip()
else:
content = text.strip()
lines = content.split('\n')
current_section = None
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith('Product Name:'):
analysis_dict['product_name'] = line.split(':', 1)[1].strip()
elif line.startswith('Category:'):
analysis_dict['category'] = line.split(':', 1)[1].strip()
elif line.startswith('Description:'):
analysis_dict['description'] = line.split(':', 1)[1].strip()
elif line.startswith('Price:'):
analysis_dict['price'] = line.split(':', 1)[1].strip()
elif line.startswith('Key Features:'):
current_section = 'features'
elif line.startswith('Search Keywords:'):
current_section = 'keywords'
elif line.startswith('- '):
if current_section == 'features':
analysis_dict['key_features'].append(line.strip('- '))
elif current_section == 'keywords':
analysis_dict['search_keywords'].append(line.strip('- '))
return analysis_dict
except Exception as e:
logger.error(f"Error parsing analysis: {str(e)}")
return analysis_dict