anjomj commited on
Commit
767688f
·
verified ·
1 Parent(s): 999141b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -6,23 +6,27 @@ import torch
6
  from transformers import (
7
  AutoModelForImageTextToText,
8
  AutoProcessor,
 
9
  TextIteratorStreamer,
10
  )
11
 
12
- # Choose your Gemma 4 checkpoint:
13
- # - "google/gemma-4-E2B-it" (Fits nicely on Free 16GB CPU or ZeroGPU)
14
- # - "google/gemma-4-E4B-it" (Fits on Free 16GB CPU or ZeroGPU)
15
- # - "google/gemma-4-12B-it" (Requires ZeroGPU or dedicated GPU tier)
16
  MODEL_ID = os.getenv("MODEL_ID", "google/gemma-4-E2B-it")
17
  HF_TOKEN = os.getenv("HF_TOKEN", None)
18
 
19
- # Automatically select bfloat16 on GPU, or float32 for CPU compatibility
20
  device = "cuda" if torch.cuda.is_available() else "cpu"
21
  dtype = torch.bfloat16 if device == "cuda" else torch.float32
22
 
23
  print(f"Loading {MODEL_ID} locally on {device} ({dtype})...")
24
 
25
- processor = AutoProcessor.from_pretrained(MODEL_ID, token=HF_TOKEN)
 
 
 
 
 
 
 
 
26
  model = AutoModelForImageTextToText.from_pretrained(
27
  MODEL_ID,
28
  torch_dtype=dtype,
@@ -41,7 +45,6 @@ def generate_response(
41
  ):
42
  messages = []
43
 
44
- # Gemma 4 triggers internal step-by-step reasoning when <|think|> starts the system prompt
45
  sys_prompt = "You are a helpful AI assistant."
46
  if enable_thinking:
47
  sys_prompt = "<|think|>" + sys_prompt
@@ -51,13 +54,12 @@ def generate_response(
51
  "content": [{"type": "text", "text": sys_prompt}],
52
  })
53
 
54
- # Format conversation history
55
  for turn in history:
56
  if isinstance(turn, dict):
57
- messages.append({
58
- "role": turn["role"],
59
- "content": [{"type": "text", "text": str(turn["content"])}],
60
- })
61
  elif isinstance(turn, (list, tuple)) and len(turn) == 2:
62
  user_msg, bot_msg = turn
63
  if user_msg:
@@ -76,7 +78,6 @@ def generate_response(
76
  "content": [{"type": "text", "text": message}],
77
  })
78
 
79
- # Prepare inputs via Gemma 4 chat template
80
  inputs = processor.apply_chat_template(
81
  messages,
82
  tokenize=True,
@@ -86,7 +87,7 @@ def generate_response(
86
  ).to(model.device)
87
 
88
  streamer = TextIteratorStreamer(
89
- processor.tokenizer,
90
  skip_prompt=True,
91
  skip_special_tokens=True,
92
  )
 
6
  from transformers import (
7
  AutoModelForImageTextToText,
8
  AutoProcessor,
9
+ AutoTokenizer,
10
  TextIteratorStreamer,
11
  )
12
 
 
 
 
 
13
  MODEL_ID = os.getenv("MODEL_ID", "google/gemma-4-E2B-it")
14
  HF_TOKEN = os.getenv("HF_TOKEN", None)
15
 
 
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
  dtype = torch.bfloat16 if device == "cuda" else torch.float32
18
 
19
  print(f"Loading {MODEL_ID} locally on {device} ({dtype})...")
20
 
21
+ # Load processor with automatic fallback to AutoTokenizer for text-only inference
22
+ try:
23
+ processor = AutoProcessor.from_pretrained(MODEL_ID, token=HF_TOKEN)
24
+ tokenizer = processor.tokenizer
25
+ except Exception as e:
26
+ print(f"AutoProcessor failed ({e}), falling back to AutoTokenizer...")
27
+ processor = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
28
+ tokenizer = processor
29
+
30
  model = AutoModelForImageTextToText.from_pretrained(
31
  MODEL_ID,
32
  torch_dtype=dtype,
 
45
  ):
46
  messages = []
47
 
 
48
  sys_prompt = "You are a helpful AI assistant."
49
  if enable_thinking:
50
  sys_prompt = "<|think|>" + sys_prompt
 
54
  "content": [{"type": "text", "text": sys_prompt}],
55
  })
56
 
 
57
  for turn in history:
58
  if isinstance(turn, dict):
59
+ content = turn["content"]
60
+ if isinstance(content, str):
61
+ content = [{"type": "text", "text": content}]
62
+ messages.append({"role": turn["role"], "content": content})
63
  elif isinstance(turn, (list, tuple)) and len(turn) == 2:
64
  user_msg, bot_msg = turn
65
  if user_msg:
 
78
  "content": [{"type": "text", "text": message}],
79
  })
80
 
 
81
  inputs = processor.apply_chat_template(
82
  messages,
83
  tokenize=True,
 
87
  ).to(model.device)
88
 
89
  streamer = TextIteratorStreamer(
90
+ tokenizer,
91
  skip_prompt=True,
92
  skip_special_tokens=True,
93
  )