feliksius commited on
Commit
a973a24
·
verified ·
1 Parent(s): bbb0589

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -10
app.py CHANGED
@@ -7,20 +7,31 @@ from typing import Optional
7
  import re
8
  from functools import lru_cache
9
  import asyncio
10
- import logging.handlers
11
 
12
  # Set environment variables for Hugging Face cache
13
  os.environ["HF_HOME"] = "/app/cache"
14
  os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
15
 
 
 
 
 
 
 
 
 
 
 
 
16
  app = FastAPI()
17
 
18
- # Configure asynchronous logging with RotatingFileHandler and StreamHandler
19
  logging.basicConfig(
 
20
  level=logging.INFO,
21
  handlers=[
22
  logging.handlers.RotatingFileHandler("/app/logs/app.log", maxBytes=1000000, backupCount=1),
23
- logging.StreamHandler() # Added for console output
24
  ]
25
  )
26
  logger = logging.getLogger(__name__)
@@ -43,13 +54,17 @@ def get_translator(lang: str):
43
  """Load or retrieve cached translator for the given language."""
44
  if lang not in translators:
45
  logger.info(f"Loading model for {lang} from {MODEL_MAP[lang]}...")
46
- translators[lang] = pipeline(
47
- "translation",
48
- model=MODEL_MAP[lang],
49
- device=-1, # Explicitly use CPU for Hugging Face Spaces (free tier)
50
- model_kwargs={"load_in_8bit": True} if os.getenv("USE_QUANTIZATION", "0") == "1" else {}
51
- )
52
- logger.info(f"Model for {lang} loaded successfully.")
 
 
 
 
53
  return translators[lang]
54
 
55
  @lru_cache(maxsize=100)
@@ -96,6 +111,12 @@ async def translate(text: str, source_lang_override: Optional[str] = None):
96
  if not text:
97
  raise HTTPException(status_code=400, detail="Text input is required.")
98
 
 
 
 
 
 
 
99
  try:
100
  # Determine source language
101
  if source_lang_override and source_lang_override in MODEL_MAP:
 
7
  import re
8
  from functools import lru_cache
9
  import asyncio
 
10
 
11
  # Set environment variables for Hugging Face cache
12
  os.environ["HF_HOME"] = "/app/cache"
13
  os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
14
 
15
+ # Environment configuration
16
+ USE_8BIT = False
17
+ try:
18
+ import bitsandbytes # hanya untuk memastikan modul tersedia
19
+ USE_8BIT = os.getenv("USE_QUANTIZATION", "0") == "1"
20
+ except ImportError:
21
+ USE_8BIT = False
22
+
23
+ DEVICE = int(os.getenv("DEVICE", "-1")) # -1 for CPU, 0+ for GPU
24
+ MAX_TEXT_LENGTH = int(os.getenv("MAX_TEXT_LENGTH", "5000"))
25
+
26
  app = FastAPI()
27
 
28
+ # Configure logging with timestamp and level
29
  logging.basicConfig(
30
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
31
  level=logging.INFO,
32
  handlers=[
33
  logging.handlers.RotatingFileHandler("/app/logs/app.log", maxBytes=1000000, backupCount=1),
34
+ logging.StreamHandler()
35
  ]
36
  )
37
  logger = logging.getLogger(__name__)
 
54
  """Load or retrieve cached translator for the given language."""
55
  if lang not in translators:
56
  logger.info(f"Loading model for {lang} from {MODEL_MAP[lang]}...")
57
+ try:
58
+ translators[lang] = pipeline(
59
+ "translation",
60
+ model=MODEL_MAP[lang],
61
+ device=DEVICE,
62
+ model_kwargs={"load_in_8bit": USE_8BIT}
63
+ )
64
+ logger.info(f"Model for {lang} loaded successfully.")
65
+ except Exception as e:
66
+ logger.error(f"Failed to load model for {lang}: {str(e)}")
67
+ raise
68
  return translators[lang]
69
 
70
  @lru_cache(maxsize=100)
 
111
  if not text:
112
  raise HTTPException(status_code=400, detail="Text input is required.")
113
 
114
+ if len(text) > MAX_TEXT_LENGTH:
115
+ raise HTTPException(
116
+ status_code=413,
117
+ detail=f"Text too long. Max allowed length: {MAX_TEXT_LENGTH}."
118
+ )
119
+
120
  try:
121
  # Determine source language
122
  if source_lang_override and source_lang_override in MODEL_MAP: