Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,23 +18,36 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
| 18 |
# ---------------------------------------------------------------------------
|
| 19 |
# MODULE 4 — Generation Module (LLM setup)
|
| 20 |
# ---------------------------------------------------------------------------
|
| 21 |
-
MODEL_ID = "google/gemma-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# ---------------------------------------------------------------------------
|
| 40 |
# MODULE 3 — Pre-processing Module (6-Element Framework)
|
|
@@ -182,6 +195,13 @@ def validate_and_evaluate(description: str, code: str):
|
|
| 182 |
# --- Pre-processing ---
|
| 183 |
prompt = build_prompt(description, code)
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
# --- Generation ---
|
| 186 |
try:
|
| 187 |
raw_output = generate(prompt)
|
|
|
|
| 18 |
# ---------------------------------------------------------------------------
|
| 19 |
# MODULE 4 — Generation Module (LLM setup)
|
| 20 |
# ---------------------------------------------------------------------------
|
| 21 |
+
MODEL_ID = "google/gemma-4-it" # swap for "google/gemma-4-9b-it" if VRAM allows
|
| 22 |
+
|
| 23 |
+
_tokenizer=None
|
| 24 |
+
_pipe=None
|
| 25 |
+
|
| 26 |
+
def get_tokenizer():
|
| 27 |
+
"""Returns the tokenizer, loading it on first call."""
|
| 28 |
+
global _tokenizer
|
| 29 |
+
if _tokenizer is None:
|
| 30 |
+
_tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 31 |
+
return _tokenizer
|
| 32 |
+
|
| 33 |
+
def get_pipeline():
|
| 34 |
+
"""Returns the generation pipeline, loading model on first call (lazy load)."""
|
| 35 |
+
global _pipe
|
| 36 |
+
if _pipe is None:
|
| 37 |
+
tokenizer = get_tokenizer()
|
| 38 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 39 |
+
MODEL_ID,
|
| 40 |
+
torch_dtype=torch.bfloat16, # efficient on modern GPUs/CPUs
|
| 41 |
+
device_map="auto", # auto-detect GPU/CPU
|
| 42 |
+
)
|
| 43 |
+
_pipe = pipeline(
|
| 44 |
+
"text-generation",
|
| 45 |
+
model=model,
|
| 46 |
+
tokenizer=tokenizer,
|
| 47 |
+
max_new_tokens=512,
|
| 48 |
+
do_sample=False, # deterministic output
|
| 49 |
+
)
|
| 50 |
+
return _pipe
|
| 51 |
|
| 52 |
# ---------------------------------------------------------------------------
|
| 53 |
# MODULE 3 — Pre-processing Module (6-Element Framework)
|
|
|
|
| 195 |
# --- Pre-processing ---
|
| 196 |
prompt = build_prompt(description, code)
|
| 197 |
|
| 198 |
+
token_count = len(get_tokenizer().encode(prompt))
|
| 199 |
+
if token_count > 2048:
|
| 200 |
+
return "", "", "", (
|
| 201 |
+
f"⚠️ Input too long ({token_count} tokens). "
|
| 202 |
+
"Please shorten your description or code and try again."
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
# --- Generation ---
|
| 206 |
try:
|
| 207 |
raw_output = generate(prompt)
|