Spaces:
Sleeping
Sleeping
perf: update model and library for faster gemini call
Browse files- inference_api.py +23 -11
inference_api.py
CHANGED
|
@@ -13,6 +13,7 @@ hints using Google Gemini based on student preference.
|
|
| 13 |
import os
|
| 14 |
import json
|
| 15 |
import math
|
|
|
|
| 16 |
import logging
|
| 17 |
from typing import Optional, List, Dict, Any
|
| 18 |
|
|
@@ -57,6 +58,9 @@ else:
|
|
| 57 |
TOP_CATEGORY_PATH = "top_category.json"
|
| 58 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
# Keras Custom Layers Definition
|
| 62 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -662,27 +666,35 @@ def trigger_gemini_explanation(skills: List[str], preference: str, question: Opt
|
|
| 662 |
|
| 663 |
if not GEMINI_API_KEY:
|
| 664 |
placeholder = (
|
| 665 |
-
f"
|
| 666 |
-
f"
|
| 667 |
-
f"Try taking it step by step β you are fully capable of masteries like this!"
|
| 668 |
)
|
| 669 |
logger.warning("GEMINI_API_KEY is not set. Returning offline placeholder.")
|
| 670 |
return placeholder
|
| 671 |
|
| 672 |
try:
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
logger.info(f"[Gemini Response]\n{text}")
|
| 679 |
return text
|
| 680 |
except Exception as e:
|
| 681 |
logger.error(f"Error calling Gemini: {e}")
|
| 682 |
fallback = (
|
| 683 |
-
f"
|
| 684 |
-
f"
|
| 685 |
-
f"Draw a picture or break it down into simple equations β you've got this!"
|
| 686 |
)
|
| 687 |
return fallback
|
| 688 |
|
|
|
|
| 13 |
import os
|
| 14 |
import json
|
| 15 |
import math
|
| 16 |
+
import time
|
| 17 |
import logging
|
| 18 |
from typing import Optional, List, Dict, Any
|
| 19 |
|
|
|
|
| 58 |
TOP_CATEGORY_PATH = "top_category.json"
|
| 59 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
|
| 60 |
|
| 61 |
+
# Lazily initialized Google GenAI client (new SDK)
|
| 62 |
+
_genai_client = None
|
| 63 |
+
|
| 64 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 65 |
# Keras Custom Layers Definition
|
| 66 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 666 |
|
| 667 |
if not GEMINI_API_KEY:
|
| 668 |
placeholder = (
|
| 669 |
+
f"Terus berlatih, ya! Coba ulangi konsep inti tentang {', '.join(skills)}. "
|
| 670 |
+
f"Kerjakan pelan-pelan langkah demi langkah β kamu pasti bisa!"
|
|
|
|
| 671 |
)
|
| 672 |
logger.warning("GEMINI_API_KEY is not set. Returning offline placeholder.")
|
| 673 |
return placeholder
|
| 674 |
|
| 675 |
try:
|
| 676 |
+
global _genai_client
|
| 677 |
+
if _genai_client is None:
|
| 678 |
+
from google import genai
|
| 679 |
+
_genai_client = genai.Client(api_key=GEMINI_API_KEY)
|
| 680 |
+
|
| 681 |
+
time_start = time.perf_counter()
|
| 682 |
+
response = _genai_client.models.generate_content(
|
| 683 |
+
model="gemini-2.5-flash-lite",
|
| 684 |
+
contents=prompt,
|
| 685 |
+
)
|
| 686 |
+
time_end = time.perf_counter()
|
| 687 |
+
latency_ms = (time_end - time_start) * 1000.0
|
| 688 |
+
logger.info(f"[Gemini Latency] models.generate_content took {latency_ms:.0f}ms")
|
| 689 |
+
|
| 690 |
+
text = (getattr(response, "text", "") or "").strip()
|
| 691 |
logger.info(f"[Gemini Response]\n{text}")
|
| 692 |
return text
|
| 693 |
except Exception as e:
|
| 694 |
logger.error(f"Error calling Gemini: {e}")
|
| 695 |
fallback = (
|
| 696 |
+
f"Coba tinjau lagi aturan/konsep utama untuk {', '.join(skills)}. "
|
| 697 |
+
f"Buat gambar atau pecah menjadi persamaan sederhana β kamu pasti bisa!"
|
|
|
|
| 698 |
)
|
| 699 |
return fallback
|
| 700 |
|