Spaces:
Running
Running
Gaurav vashistha
commited on
Commit
·
b8195df
1
Parent(s):
d288e3a
Fix vision core and error handling
Browse files- 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 |
-
|
| 11 |
-
if not
|
| 12 |
-
|
| 13 |
-
genai.configure(api_key=self.api_key)
|
| 14 |
-
self.model = genai.GenerativeModel('gemini-flash-latest')
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
#
|
| 25 |
-
|
| 26 |
-
# The python library often accepts a dictionary for inline data.
|
| 27 |
-
# let's use the 'parts' construction which is standard.
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return json.loads(text_response
|
| 45 |
-
|
| 46 |
except Exception as e:
|
| 47 |
-
print(f"
|
|
|
|
| 48 |
return {
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
|
|
|
|
|
|
| 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 |
}
|