Gaurav vashistha commited on
Commit
b8195df
·
1 Parent(s): d288e3a

Fix vision core and error handling

Browse files
Files changed (1) hide show
  1. agents/visual_analyst.py +44 -37
agents/visual_analyst.py CHANGED
@@ -1,53 +1,60 @@
1
- import google.generativeai as genai
2
  import os
3
  import json
 
 
 
4
  from dotenv import load_dotenv
5
 
6
  load_dotenv()
7
 
8
  class VisualAnalyst:
9
  def __init__(self):
10
- self.api_key = os.getenv("GEMINI_API_KEY")
11
- if not self.api_key:
12
- raise ValueError("GEMINI_API_KEY not found in environment variables")
13
- genai.configure(api_key=self.api_key)
14
- self.model = genai.GenerativeModel('gemini-flash-latest')
15
 
16
- def analyze_image(self, image_bytes):
17
- prompt = (
18
- "Analyze this product image for an e-commerce listing. "
19
- "detailed visual attributes including: Main Color, Material/Texture, Style/Vibe, "
20
- "and 3 distinct Visual Features. Return the result strictly as a JSON object."
21
- )
22
 
 
 
 
23
  try:
24
- # Gemini supports passing bytes directly if packaged correctly,
25
- # but usually it expects a PIL image or a specific blob format.
26
- # The python library often accepts a dictionary for inline data.
27
- # let's use the 'parts' construction which is standard.
28
 
29
- response = self.model.generate_content([
30
- {'mime_type': 'image/jpeg', 'data': image_bytes},
31
- prompt
32
- ])
33
-
34
- text_response = response.text
 
 
 
 
 
 
 
35
 
36
- # Clean up markdown code blocks if present
37
- if text_response.startswith('```json'):
38
- text_response = text_response[7:]
39
- if text_response.startswith('```'):
40
- text_response = text_response[3:]
41
- if text_response.endswith('```'):
42
- text_response = text_response[:-3]
43
-
44
- return json.loads(text_response.strip())
45
-
46
  except Exception as e:
47
- print(f"⚠️ API Quota Hit. Switching to Simulation Mode. (Error: {e})")
 
48
  return {
49
- "color": "Midnight Blue",
50
- "material": "Synthetic Leather",
51
- "vibe": "Modern Minimalist",
52
- "features": ["White rubber sole", "Perforated texture", "Low-top silhouette"]
 
 
53
  }
 
 
1
  import os
2
  import json
3
+ import asyncio
4
+ import google.generativeai as genai
5
+ from PIL import Image
6
  from dotenv import load_dotenv
7
 
8
  load_dotenv()
9
 
10
  class VisualAnalyst:
11
  def __init__(self):
12
+ api_key = os.getenv("GEMINI_API_KEY")
13
+ if not api_key:
14
+ print("⚠️ GEMINI_API_KEY missing")
 
 
15
 
16
+ genai.configure(api_key=api_key)
17
+ # Use the modern, faster Flash model
18
+ self.model = genai.GenerativeModel('gemini-1.5-flash')
 
 
 
19
 
20
+ async def analyze_image(self, image_path: str):
21
+ print(f"👁️ Analyzing image: {image_path}")
22
+
23
  try:
24
+ # 1. Load image properly with Pillow (Fixes format issues)
25
+ img = Image.open(image_path)
 
 
26
 
27
+ # 2. Define the prompt
28
+ prompt = """
29
+ Analyze this product image for an e-commerce listing.
30
+ Return ONLY a raw JSON object (no markdown formatting) with this structure:
31
+ {
32
+ "main_color": "string",
33
+ "product_type": "string",
34
+ "design_style": "string (minimalist, streetwear, vintage, etc)",
35
+ "visual_features": ["list", "of", "visible", "features"],
36
+ "suggested_title": "creative product title",
37
+ "condition_guess": "new/used"
38
+ }
39
+ """
40
 
41
+ # 3. Run in a thread to prevent blocking (Sync to Async wrapper)
42
+ response = await asyncio.to_thread(
43
+ self.model.generate_content,
44
+ [prompt, img]
45
+ )
46
+
47
+ # 4. Clean and Parse JSON
48
+ text_response = response.text.replace('```json', '').replace('```', '').strip()
49
+ return json.loads(text_response)
 
50
  except Exception as e:
51
+ print(f" Vision Error: {e}")
52
+ # Return a Safe Fallback (Simulation)
53
  return {
54
+ "main_color": "Unknown",
55
+ "product_type": "Unidentified Item",
56
+ "design_style": "Standard",
57
+ "visual_features": ["Error analyzing image"],
58
+ "suggested_title": "Manual Review Needed",
59
+ "condition_guess": "New"
60
  }