Spaces:
Running
on
Zero
Running
on
Zero
Update models.py
Browse files
models.py
CHANGED
@@ -62,6 +62,41 @@ class BagelAPIAnalyzer(BaseImageAnalyzer):
|
|
62 |
logger.error(f"BAGEL API client initialization failed: {e}")
|
63 |
return False
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
def _save_temp_image(self, image: Image.Image) -> str:
|
66 |
"""Save image to temporary file for API call"""
|
67 |
try:
|
@@ -101,7 +136,13 @@ class BagelAPIAnalyzer(BaseImageAnalyzer):
|
|
101 |
try:
|
102 |
# Default prompt for detailed image analysis
|
103 |
if prompt is None:
|
104 |
-
prompt = "
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
# Save image to temporary file
|
107 |
temp_path = self._save_temp_image(image)
|
@@ -127,11 +168,20 @@ class BagelAPIAnalyzer(BaseImageAnalyzer):
|
|
127 |
else:
|
128 |
description = str(result)
|
129 |
|
130 |
-
# Clean up the description
|
131 |
if isinstance(description, str) and description.strip():
|
132 |
description = description.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
else:
|
134 |
description = "Detailed image analysis completed successfully"
|
|
|
135 |
|
136 |
# Prepare metadata
|
137 |
metadata = {
|
@@ -158,7 +208,13 @@ class BagelAPIAnalyzer(BaseImageAnalyzer):
|
|
158 |
|
159 |
def analyze_for_flux_prompt(self, image: Image.Image) -> Tuple[str, Dict[str, Any]]:
|
160 |
"""Analyze image specifically for FLUX prompt generation"""
|
161 |
-
flux_prompt = """
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
return self.analyze_image(image, flux_prompt)
|
164 |
|
|
|
62 |
logger.error(f"BAGEL API client initialization failed: {e}")
|
63 |
return False
|
64 |
|
65 |
+
def _extract_camera_setup(self, description: str) -> Optional[str]:
|
66 |
+
"""Extract camera setup recommendation from BAGEL response"""
|
67 |
+
try:
|
68 |
+
# Look for CAMERA_SETUP section
|
69 |
+
if "CAMERA_SETUP:" in description:
|
70 |
+
parts = description.split("CAMERA_SETUP:")
|
71 |
+
if len(parts) > 1:
|
72 |
+
camera_part = parts[1].strip()
|
73 |
+
# Clean up any additional formatting
|
74 |
+
camera_part = camera_part.replace("\n", " ").strip()
|
75 |
+
return camera_part
|
76 |
+
|
77 |
+
# Alternative patterns for camera recommendations
|
78 |
+
camera_patterns = [
|
79 |
+
"Shot on ",
|
80 |
+
"Camera: ",
|
81 |
+
"Equipment: ",
|
82 |
+
"Recommended camera:",
|
83 |
+
"Camera setup:"
|
84 |
+
]
|
85 |
+
|
86 |
+
for pattern in camera_patterns:
|
87 |
+
if pattern in description:
|
88 |
+
# Extract text after the pattern
|
89 |
+
idx = description.find(pattern)
|
90 |
+
camera_text = description[idx:].split('.')[0] # Take first sentence
|
91 |
+
if len(camera_text) > len(pattern) + 10: # Ensure meaningful content
|
92 |
+
return camera_text.strip()
|
93 |
+
|
94 |
+
return None
|
95 |
+
|
96 |
+
except Exception as e:
|
97 |
+
logger.warning(f"Failed to extract camera setup: {e}")
|
98 |
+
return None
|
99 |
+
|
100 |
def _save_temp_image(self, image: Image.Image) -> str:
|
101 |
"""Save image to temporary file for API call"""
|
102 |
try:
|
|
|
136 |
try:
|
137 |
# Default prompt for detailed image analysis
|
138 |
if prompt is None:
|
139 |
+
prompt = """Analyze this image and provide a detailed description in two parts:
|
140 |
+
|
141 |
+
1. DESCRIPTION: Write a flowing paragraph describing the visual elements, composition, lighting, colors, artistic style, mood, and atmosphere. Start directly with the subject (e.g., "A black and white illustration..." not "The image shows..."). Write as a continuous narrative without numbered lists.
|
142 |
+
|
143 |
+
2. CAMERA_SETUP: Based on the photographic characteristics you observe, recommend the specific camera system, lens, and settings that would best capture this type of image. Consider focal length, aperture, lighting setup, and shooting style that matches what you see.
|
144 |
+
|
145 |
+
Format your response clearly with these two sections."""
|
146 |
|
147 |
# Save image to temporary file
|
148 |
temp_path = self._save_temp_image(image)
|
|
|
168 |
else:
|
169 |
description = str(result)
|
170 |
|
171 |
+
# Clean up the description and extract camera setup if present
|
172 |
if isinstance(description, str) and description.strip():
|
173 |
description = description.strip()
|
174 |
+
|
175 |
+
# Store camera setup separately if found
|
176 |
+
camera_setup = self._extract_camera_setup(description)
|
177 |
+
if camera_setup:
|
178 |
+
metadata["camera_setup"] = camera_setup
|
179 |
+
metadata["has_camera_suggestion"] = True
|
180 |
+
else:
|
181 |
+
metadata["has_camera_suggestion"] = False
|
182 |
else:
|
183 |
description = "Detailed image analysis completed successfully"
|
184 |
+
metadata["has_camera_suggestion"] = False
|
185 |
|
186 |
# Prepare metadata
|
187 |
metadata = {
|
|
|
208 |
|
209 |
def analyze_for_flux_prompt(self, image: Image.Image) -> Tuple[str, Dict[str, Any]]:
|
210 |
"""Analyze image specifically for FLUX prompt generation"""
|
211 |
+
flux_prompt = """Analyze this image for FLUX generation and provide two sections:
|
212 |
+
|
213 |
+
1. DESCRIPTION: Create a detailed, flowing description suitable for FLUX generation. Write as a single coherent paragraph starting directly with the subject (avoid "The image shows..."). Describe photographic style, composition, lighting, colors, mood, and artistic elements.
|
214 |
+
|
215 |
+
2. CAMERA_SETUP: Recommend the specific professional camera system, lens, aperture, and technical settings that would recreate this exact image. Be specific about equipment brands, focal lengths, and shooting parameters based on the visual characteristics you observe.
|
216 |
+
|
217 |
+
Provide both sections clearly formatted."""
|
218 |
|
219 |
return self.analyze_image(image, flux_prompt)
|
220 |
|