Spaces:
Running
Running
Gaurav vashistha
commited on
Commit
Β·
1900bac
1
Parent(s):
9d3f2ae
Implement smart model selection for Visual Analyst
Browse files- agents/visual_analyst.py +39 -12
agents/visual_analyst.py
CHANGED
|
@@ -11,30 +11,57 @@ class VisualAnalyst:
|
|
| 11 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
if not self.api_key:
|
| 13 |
raise ValueError("GEMINI_API_KEY not found")
|
|
|
|
| 14 |
genai.configure(api_key=self.api_key)
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
async def analyze_image(self, image_path: str):
|
| 19 |
-
# Read file
|
| 20 |
try:
|
| 21 |
with open(image_path, "rb") as f:
|
| 22 |
image_bytes = f.read()
|
| 23 |
except Exception as e:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"
|
| 27 |
-
"
|
| 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 |
"Return a JSON object with keys: main_color, product_type, design_style, visual_features."
|
| 34 |
)
|
| 35 |
-
|
| 36 |
try:
|
| 37 |
-
# Run
|
| 38 |
response = await asyncio.to_thread(
|
| 39 |
self.model.generate_content,
|
| 40 |
[
|
|
@@ -44,12 +71,12 @@ class VisualAnalyst:
|
|
| 44 |
)
|
| 45 |
|
| 46 |
text = response.text
|
| 47 |
-
# Clean markdown
|
| 48 |
if text.startswith('```json'): text = text[7:]
|
| 49 |
-
if text.
|
| 50 |
|
| 51 |
return json.loads(text.strip())
|
| 52 |
except Exception as e:
|
|
|
|
| 53 |
return {
|
| 54 |
"main_color": "Unknown",
|
| 55 |
"visual_features": [f"Error: {str(e)}"]
|
|
|
|
| 11 |
self.api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
if not self.api_key:
|
| 13 |
raise ValueError("GEMINI_API_KEY not found")
|
| 14 |
+
|
| 15 |
genai.configure(api_key=self.api_key)
|
| 16 |
+
|
| 17 |
+
# --- SMART MODEL SELECTION ---
|
| 18 |
+
print("π Checking available Gemini models for this key...")
|
| 19 |
+
try:
|
| 20 |
+
my_models = [m.name for m in genai.list_models() if 'generateContent' in m.supported_generation_methods]
|
| 21 |
+
print(f"π Available Models: {my_models}")
|
| 22 |
+
|
| 23 |
+
# Priority list (Newest -> Oldest)
|
| 24 |
+
preferred_order = [
|
| 25 |
+
'models/gemini-1.5-flash',
|
| 26 |
+
'models/gemini-1.5-flash-001',
|
| 27 |
+
'models/gemini-1.5-pro',
|
| 28 |
+
'models/gemini-1.5-pro-001',
|
| 29 |
+
'models/gemini-pro-vision' # Legacy fallback
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
selected_model = "models/gemini-1.5-flash" # Default
|
| 33 |
+
|
| 34 |
+
for model_name in preferred_order:
|
| 35 |
+
if model_name in my_models:
|
| 36 |
+
selected_model = model_name
|
| 37 |
+
break
|
| 38 |
+
|
| 39 |
+
print(f"β
Selected Vision Model: {selected_model}")
|
| 40 |
+
self.model = genai.GenerativeModel(selected_model)
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"β οΈ Model list failed ({e}), defaulting to gemini-1.5-flash")
|
| 44 |
+
self.model = genai.GenerativeModel('gemini-1.5-flash')
|
| 45 |
|
| 46 |
async def analyze_image(self, image_path: str):
|
| 47 |
+
# Adaptation: Read file path to bytes, as main.py passes a path
|
| 48 |
try:
|
| 49 |
with open(image_path, "rb") as f:
|
| 50 |
image_bytes = f.read()
|
| 51 |
except Exception as e:
|
| 52 |
+
print(f"β File Read Error: {e}")
|
| 53 |
+
return {
|
| 54 |
+
"main_color": "Unknown",
|
| 55 |
+
"visual_features": [f"Error reading file: {str(e)}"]
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
+
# Prompt for analysis
|
| 59 |
prompt = (
|
| 60 |
"Analyze this product image for an e-commerce listing. "
|
| 61 |
"Return a JSON object with keys: main_color, product_type, design_style, visual_features."
|
| 62 |
)
|
|
|
|
| 63 |
try:
|
| 64 |
+
# Adaptation: Run in thread to allow async await
|
| 65 |
response = await asyncio.to_thread(
|
| 66 |
self.model.generate_content,
|
| 67 |
[
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
text = response.text
|
|
|
|
| 74 |
if text.startswith('```json'): text = text[7:]
|
| 75 |
+
if text.startswith('```'): text = text[:-3]
|
| 76 |
|
| 77 |
return json.loads(text.strip())
|
| 78 |
except Exception as e:
|
| 79 |
+
print(f"β Analysis Failed: {e}")
|
| 80 |
return {
|
| 81 |
"main_color": "Unknown",
|
| 82 |
"visual_features": [f"Error: {str(e)}"]
|