Doc2GL Deploy commited on
Commit ·
7a2d1b0
1
Parent(s): 7f28b10
Gemini: add model fallback list and GEMINI_MODEL override to avoid 404 NOT_FOUND
Browse files- demo_user.py +34 -13
demo_user.py
CHANGED
|
@@ -172,6 +172,16 @@ def generate_mermaid_from_image_gemini(base64_image):
|
|
| 172 |
if not api_key:
|
| 173 |
raise EnvironmentError("La clé API GEMINI_API_KEY n'est pas définie.")
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
try:
|
| 176 |
client = genai.Client(api_key=api_key)
|
| 177 |
prompt = """
|
|
@@ -188,21 +198,32 @@ def generate_mermaid_from_image_gemini(base64_image):
|
|
| 188 |
- Inclus les relations les plus importantes seulement
|
| 189 |
"""
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
types.
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
],
|
| 203 |
)
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
mermaid_code = (getattr(response, "text", None) or "").strip()
|
| 208 |
if not mermaid_code:
|
|
|
|
| 172 |
if not api_key:
|
| 173 |
raise EnvironmentError("La clé API GEMINI_API_KEY n'est pas définie.")
|
| 174 |
|
| 175 |
+
model_from_env = os.environ.get("GEMINI_MODEL", "").strip()
|
| 176 |
+
model_candidates = [
|
| 177 |
+
model_from_env,
|
| 178 |
+
"gemini-2.0-flash",
|
| 179 |
+
"gemini-2.0-flash-lite",
|
| 180 |
+
"gemini-1.5-flash-latest",
|
| 181 |
+
"gemini-1.5-pro-latest",
|
| 182 |
+
]
|
| 183 |
+
model_candidates = [m for m in model_candidates if m]
|
| 184 |
+
|
| 185 |
try:
|
| 186 |
client = genai.Client(api_key=api_key)
|
| 187 |
prompt = """
|
|
|
|
| 198 |
- Inclus les relations les plus importantes seulement
|
| 199 |
"""
|
| 200 |
|
| 201 |
+
last_error = None
|
| 202 |
+
response = None
|
| 203 |
+
for model_name in model_candidates:
|
| 204 |
+
try:
|
| 205 |
+
response = client.models.generate_content(
|
| 206 |
+
model=model_name,
|
| 207 |
+
contents=[
|
| 208 |
+
types.Content(
|
| 209 |
+
role="user",
|
| 210 |
+
parts=[
|
| 211 |
+
types.Part.from_text(text=prompt),
|
| 212 |
+
types.Part.from_bytes(
|
| 213 |
+
data=base64.b64decode(base64_image),
|
| 214 |
+
mime_type="image/png",
|
| 215 |
+
),
|
| 216 |
+
],
|
| 217 |
+
)
|
| 218 |
],
|
| 219 |
)
|
| 220 |
+
break
|
| 221 |
+
except Exception as e:
|
| 222 |
+
last_error = e
|
| 223 |
+
continue
|
| 224 |
+
|
| 225 |
+
if response is None:
|
| 226 |
+
raise last_error if last_error is not None else RuntimeError("Échec Gemini: aucune réponse")
|
| 227 |
|
| 228 |
mermaid_code = (getattr(response, "text", None) or "").strip()
|
| 229 |
if not mermaid_code:
|