Spaces:
Sleeping
Sleeping
update gemini
Browse files
app.py
CHANGED
|
@@ -128,16 +128,15 @@ def image_to_data_uri(image: Image.Image) -> str:
|
|
| 128 |
# ============================================================================
|
| 129 |
# STEP 1 — GEMINI 2.0 FLASH: GENERATE 5 DIVERSE CAPTIONS
|
| 130 |
# Single API call — all 5 captions in one request
|
| 131 |
-
#
|
|
|
|
| 132 |
# ============================================================================
|
| 133 |
def generate_captions_gemini(image: Image.Image) -> list:
|
| 134 |
|
| 135 |
-
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 136 |
-
|
| 137 |
prompt = """Look at this image carefully and write 5 different captions from different perspectives.
|
| 138 |
|
| 139 |
-
1. Overall scene:
|
| 140 |
-
2. People: Describe the people, their clothing colors, style, and what they are doing.
|
| 141 |
3. Background: Describe the background, setting, and surroundings.
|
| 142 |
4. Objects: Describe the objects, plants, and items visible in the image.
|
| 143 |
5. Full description: A complete description covering who is in the image, what they are doing, their appearance, and where the scene takes place.
|
|
@@ -149,26 +148,51 @@ CAPTION_3: [your caption here]
|
|
| 149 |
CAPTION_4: [your caption here]
|
| 150 |
CAPTION_5: [your caption here]"""
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
marker = f"CAPTION_{i}:"
|
| 159 |
-
next_marker = f"CAPTION_{i+1}:" if i < 5 else None
|
| 160 |
-
|
| 161 |
-
if marker in raw_text:
|
| 162 |
-
start = raw_text.index(marker) + len(marker)
|
| 163 |
-
end = raw_text.index(next_marker) if next_marker and next_marker in raw_text else len(raw_text)
|
| 164 |
-
cap = raw_text[start:end].strip().lower()
|
| 165 |
-
captions.append(cap if cap else "a scene shown in the image")
|
| 166 |
-
else:
|
| 167 |
-
captions.append("a scene shown in the image")
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
seen, unique = set(), []
|
| 174 |
for c in captions:
|
|
|
|
| 128 |
# ============================================================================
|
| 129 |
# STEP 1 — GEMINI 2.0 FLASH: GENERATE 5 DIVERSE CAPTIONS
|
| 130 |
# Single API call — all 5 captions in one request
|
| 131 |
+
# Retry logic: tries gemini-2.0-flash first, falls back to gemini-1.5-flash-8b
|
| 132 |
+
# gemini-1.5-flash-8b has separate quota pool from gemini-2.0-flash
|
| 133 |
# ============================================================================
|
| 134 |
def generate_captions_gemini(image: Image.Image) -> list:
|
| 135 |
|
|
|
|
|
|
|
| 136 |
prompt = """Look at this image carefully and write 5 different captions from different perspectives.
|
| 137 |
|
| 138 |
+
1. Overall scene: One sentence describing the general scene.
|
| 139 |
+
2. People: Describe the people, their clothing colors, style, and what they are doing in detail.
|
| 140 |
3. Background: Describe the background, setting, and surroundings.
|
| 141 |
4. Objects: Describe the objects, plants, and items visible in the image.
|
| 142 |
5. Full description: A complete description covering who is in the image, what they are doing, their appearance, and where the scene takes place.
|
|
|
|
| 148 |
CAPTION_4: [your caption here]
|
| 149 |
CAPTION_5: [your caption here]"""
|
| 150 |
|
| 151 |
+
# Try primary model first, fallback to secondary if quota exceeded
|
| 152 |
+
models_to_try = [
|
| 153 |
+
"gemini-2.0-flash",
|
| 154 |
+
"gemini-1.5-flash-8b",
|
| 155 |
+
"gemini-1.5-flash"
|
| 156 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
raw_text = None
|
| 159 |
+
|
| 160 |
+
for model_name in models_to_try:
|
| 161 |
+
try:
|
| 162 |
+
model = genai.GenerativeModel(model_name)
|
| 163 |
+
response = model.generate_content([prompt, image])
|
| 164 |
+
raw_text = response.text.strip()
|
| 165 |
+
break
|
| 166 |
+
except Exception as e:
|
| 167 |
+
error_msg = str(e)
|
| 168 |
+
if "429" in error_msg:
|
| 169 |
+
st.warning(f"{model_name} quota exceeded, trying next model...")
|
| 170 |
+
continue
|
| 171 |
+
else:
|
| 172 |
+
st.warning(f"Gemini error ({model_name}): {error_msg[:80]}")
|
| 173 |
+
continue
|
| 174 |
+
|
| 175 |
+
if raw_text is None:
|
| 176 |
+
st.error(
|
| 177 |
+
"All Gemini models hit quota limit. "
|
| 178 |
+
"Quota resets at midnight (Pacific Time). "
|
| 179 |
+
"Using fallback captions for now."
|
| 180 |
+
)
|
| 181 |
+
return ["a scene shown in the image"] * 5
|
| 182 |
+
|
| 183 |
+
# Parse the 5 captions from structured response
|
| 184 |
+
captions = []
|
| 185 |
+
for i in range(1, 6):
|
| 186 |
+
marker = f"CAPTION_{i}:"
|
| 187 |
+
next_marker = f"CAPTION_{i+1}:" if i < 5 else None
|
| 188 |
+
|
| 189 |
+
if marker in raw_text:
|
| 190 |
+
start = raw_text.index(marker) + len(marker)
|
| 191 |
+
end = raw_text.index(next_marker) if next_marker and next_marker in raw_text else len(raw_text)
|
| 192 |
+
cap = raw_text[start:end].strip().lower()
|
| 193 |
+
captions.append(cap if cap else "a scene shown in the image")
|
| 194 |
+
else:
|
| 195 |
+
captions.append("a scene shown in the image")
|
| 196 |
|
| 197 |
seen, unique = set(), []
|
| 198 |
for c in captions:
|