Spaces:
Sleeping
Sleeping
Gaurav vashistha
commited on
Commit
·
3a6f5ad
1
Parent(s):
aa46d2c
Fix Visual Analyst model and logic
Browse files- agents/visual_analyst.py +42 -45
agents/visual_analyst.py
CHANGED
|
@@ -1,70 +1,67 @@
|
|
|
|
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
| 22 |
-
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
response = await asyncio.to_thread(
|
| 43 |
self.model.generate_content,
|
| 44 |
-
[
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
# 4. Clean and Parse JSON
|
| 49 |
-
# Robust extraction of JSON content
|
| 50 |
-
content = response.text
|
| 51 |
-
if "```" in content:
|
| 52 |
-
import re
|
| 53 |
-
# Find content between ```json (or just ```) and ```
|
| 54 |
-
match = re.search(r"```(?:json)?\s*(.*?)```", content, re.DOTALL)
|
| 55 |
-
if match:
|
| 56 |
-
content = match.group(1)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
print(f"
|
| 62 |
-
# Return a
|
| 63 |
return {
|
| 64 |
"main_color": "Unknown",
|
| 65 |
"product_type": "Unidentified Item",
|
| 66 |
"design_style": "Standard",
|
| 67 |
-
"visual_features": ["Error
|
| 68 |
-
"suggested_title": "Manual Review Needed",
|
| 69 |
-
"condition_guess": "New"
|
| 70 |
}
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import asyncio
|
|
|
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
class VisualAnalyst:
|
| 10 |
def __init__(self):
|
| 11 |
+
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
+
if not self.api_key:
|
| 13 |
+
raise ValueError("GEMINI_API_KEY not found in environment variables")
|
| 14 |
+
genai.configure(api_key=self.api_key)
|
| 15 |
+
# UPDATED: Using the stable model version
|
|
|
|
| 16 |
self.model = genai.GenerativeModel('gemini-1.5-flash')
|
| 17 |
|
| 18 |
async def analyze_image(self, image_path: str):
|
| 19 |
+
# Read file as bytes to match the user's logic requirements
|
|
|
|
| 20 |
try:
|
| 21 |
+
with open(image_path, "rb") as f:
|
| 22 |
+
image_bytes = f.read()
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return {
|
| 25 |
+
"main_color": "Error",
|
| 26 |
+
"product_type": "File Read Error",
|
| 27 |
+
"design_style": "Error",
|
| 28 |
+
"visual_features": [f"Could not read file: {str(e)}"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
+
|
| 31 |
+
prompt = (
|
| 32 |
+
"Analyze this product image for an e-commerce listing. "
|
| 33 |
+
"detailed visual attributes including: Main Color, Material/Texture, Style/Vibe, "
|
| 34 |
+
"and 3 distinct Visual Features. Return the result strictly as a JSON object "
|
| 35 |
+
"with keys: main_color, product_type, design_style, visual_features."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Run blocking call in thread
|
| 40 |
response = await asyncio.to_thread(
|
| 41 |
self.model.generate_content,
|
| 42 |
+
[
|
| 43 |
+
{'mime_type': 'image/jpeg', 'data': image_bytes},
|
| 44 |
+
prompt
|
| 45 |
+
]
|
| 46 |
)
|
| 47 |
|
| 48 |
+
text_response = response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Clean up markdown code blocks if present
|
| 51 |
+
if text_response.startswith('```json'):
|
| 52 |
+
text_response = text_response[7:]
|
| 53 |
+
if text_response.startswith('```'):
|
| 54 |
+
text_response = text_response[3:]
|
| 55 |
+
if text_response.endswith('```'):
|
| 56 |
+
text_response = text_response[:-3]
|
| 57 |
+
|
| 58 |
+
return json.loads(text_response.strip())
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"⚠️ API Error: {e}")
|
| 61 |
+
# Return a clearer error for debugging
|
| 62 |
return {
|
| 63 |
"main_color": "Unknown",
|
| 64 |
"product_type": "Unidentified Item",
|
| 65 |
"design_style": "Standard",
|
| 66 |
+
"visual_features": [f"Error: {str(e)}"]
|
|
|
|
|
|
|
| 67 |
}
|