Spaces:
Sleeping
Sleeping
Commit
·
89ef32e
1
Parent(s):
29e7f58
Change model Gemini
Browse files- app/services/text_normalizer.py +36 -11
app/services/text_normalizer.py
CHANGED
|
@@ -6,16 +6,30 @@ CACHE_TTL = 60 * 60 * 24 * 3 # 3 days
|
|
| 6 |
# Simple in-memory cache (có thể thay bằng Redis, v.v. sau này)
|
| 7 |
# _normalize_cache = {}
|
| 8 |
|
| 9 |
-
# --- Gemini client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
import google.generativeai as genai
|
| 12 |
from app.config.settings import GEMINI_API_KEY
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
else:
|
| 18 |
-
|
| 19 |
|
| 20 |
async def normalize_text(raw_text: str) -> str:
|
| 21 |
cache_key = f"normalize:{sha256(raw_text)}"
|
|
@@ -33,14 +47,25 @@ Văn bản:
|
|
| 33 |
{raw_text}
|
| 34 |
"""
|
| 35 |
result = raw_text
|
| 36 |
-
if
|
| 37 |
-
# Google GenerativeAI Gemini API (synchronous, wrap in thread for async)
|
| 38 |
import asyncio
|
| 39 |
loop = asyncio.get_event_loop()
|
|
|
|
| 40 |
def call_gemini():
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
# Nếu chưa cấu hình Gemini, trả về text gốc
|
| 46 |
result = raw_text
|
|
|
|
| 6 |
# Simple in-memory cache (có thể thay bằng Redis, v.v. sau này)
|
| 7 |
# _normalize_cache = {}
|
| 8 |
|
| 9 |
+
# --- Gemini client (use new `google.genai` if available) ---
|
| 10 |
+
try:
|
| 11 |
+
import google.genai as genai
|
| 12 |
+
from google.api_core.exceptions import GoogleAPIError # optional but useful
|
| 13 |
+
except Exception:
|
| 14 |
+
genai = None
|
| 15 |
+
class GoogleAPIError(Exception):
|
| 16 |
+
pass
|
| 17 |
|
|
|
|
| 18 |
from app.config.settings import GEMINI_API_KEY
|
| 19 |
|
| 20 |
+
_gemini_client = None
|
| 21 |
+
_GEMINI_MODEL = "gemini-1.5-flash" # hoặc "gemini-1.5-flash-latest"
|
| 22 |
+
|
| 23 |
+
if GEMINI_API_KEY and genai is not None:
|
| 24 |
+
try:
|
| 25 |
+
_gemini_client = genai.Client(api_key=GEMINI_API_KEY)
|
| 26 |
+
except Exception:
|
| 27 |
+
_gemini_client = None
|
| 28 |
+
elif GEMINI_API_KEY and genai is None:
|
| 29 |
+
# package not installed
|
| 30 |
+
_gemini_client = None
|
| 31 |
else:
|
| 32 |
+
_gemini_client = None
|
| 33 |
|
| 34 |
async def normalize_text(raw_text: str) -> str:
|
| 35 |
cache_key = f"normalize:{sha256(raw_text)}"
|
|
|
|
| 47 |
{raw_text}
|
| 48 |
"""
|
| 49 |
result = raw_text
|
| 50 |
+
if _gemini_client:
|
|
|
|
| 51 |
import asyncio
|
| 52 |
loop = asyncio.get_event_loop()
|
| 53 |
+
|
| 54 |
def call_gemini():
|
| 55 |
+
resp = _gemini_client.models.generate_content(
|
| 56 |
+
model=_GEMINI_MODEL,
|
| 57 |
+
contents=prompt,
|
| 58 |
+
)
|
| 59 |
+
return resp.text if hasattr(resp, 'text') else str(resp)
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
result = await loop.run_in_executor(None, call_gemini)
|
| 63 |
+
if isinstance(result, str):
|
| 64 |
+
result = result.strip()
|
| 65 |
+
except GoogleAPIError:
|
| 66 |
+
result = raw_text
|
| 67 |
+
except Exception:
|
| 68 |
+
result = raw_text
|
| 69 |
else:
|
| 70 |
# Nếu chưa cấu hình Gemini, trả về text gốc
|
| 71 |
result = raw_text
|