IFMedTechdemo commited on
Commit
91fc8ec
·
verified ·
1 Parent(s): 948ec0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -9
app.py CHANGED
@@ -1,6 +1,6 @@
1
  """
2
  OCR Application with Multiple Models including DeepSeek OCR
3
- Fixed version with @spaces.GPU decorator for Hugging Face Spaces
4
  """
5
 
6
  import os
@@ -97,7 +97,7 @@ except Exception as e:
97
  processor_m = None
98
  print(f"✗ olmOCR-2-7B-1025: Failed to load - {str(e)}")
99
 
100
- # Load DeepSeek-OCR
101
  try:
102
  MODEL_ID_DS = "deepseek-ai/deepseek-ocr"
103
  processor_ds = AutoProcessor.from_pretrained(MODEL_ID_DS, trust_remote_code=True)
@@ -106,7 +106,14 @@ try:
106
  trust_remote_code=True,
107
  torch_dtype=torch.float16
108
  ).eval()
109
- print("✓ DeepSeek-OCR loaded")
 
 
 
 
 
 
 
110
  except Exception as e:
111
  model_ds = None
112
  processor_ds = None
@@ -189,12 +196,17 @@ def generate_image(model_name: str, text: str, image: Image.Image,
189
  ]
190
  }]
191
 
192
- # Apply chat template
193
- prompt_full = processor.apply_chat_template(
194
- messages,
195
- tokenize=False,
196
- add_generation_prompt=True
197
- )
 
 
 
 
 
198
 
199
  # Process inputs
200
  inputs = processor(
@@ -239,6 +251,9 @@ def generate_image(model_name: str, text: str, image: Image.Image,
239
 
240
  except Exception as e:
241
  error_msg = f"Error during generation: {str(e)}"
 
 
 
242
  yield error_msg, error_msg
243
 
244
 
 
1
  """
2
  OCR Application with Multiple Models including DeepSeek OCR
3
+ Fixed version with proper chat template handling for DeepSeek OCR
4
  """
5
 
6
  import os
 
97
  processor_m = None
98
  print(f"✗ olmOCR-2-7B-1025: Failed to load - {str(e)}")
99
 
100
+ # Load DeepSeek-OCR with custom chat template
101
  try:
102
  MODEL_ID_DS = "deepseek-ai/deepseek-ocr"
103
  processor_ds = AutoProcessor.from_pretrained(MODEL_ID_DS, trust_remote_code=True)
 
106
  trust_remote_code=True,
107
  torch_dtype=torch.float16
108
  ).eval()
109
+
110
+ # Set a default chat template for DeepSeek OCR if it doesn't have one
111
+ if not hasattr(processor_ds.tokenizer, 'chat_template') or processor_ds.tokenizer.chat_template is None:
112
+ # Use a standard Qwen-style chat template
113
+ processor_ds.tokenizer.chat_template = "{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'text' %}{{ content['text'] }}{% endif %}{% endfor %}{% endif %}<|im_end|>\n{% endfor %}<|im_start|>assistant\n"
114
+ print("✓ DeepSeek-OCR loaded (with custom chat template)")
115
+ else:
116
+ print("✓ DeepSeek-OCR loaded")
117
  except Exception as e:
118
  model_ds = None
119
  processor_ds = None
 
196
  ]
197
  }]
198
 
199
+ # Apply chat template with error handling
200
+ try:
201
+ prompt_full = processor.apply_chat_template(
202
+ messages,
203
+ tokenize=False,
204
+ add_generation_prompt=True
205
+ )
206
+ except Exception as template_error:
207
+ # Fallback: create a simple prompt without chat template
208
+ print(f"Chat template error: {template_error}. Using fallback prompt.")
209
+ prompt_full = f"<|im_start|>user\n{text}<|im_end|>\n<|im_start|>assistant\n"
210
 
211
  # Process inputs
212
  inputs = processor(
 
251
 
252
  except Exception as e:
253
  error_msg = f"Error during generation: {str(e)}"
254
+ print(f"Full error: {e}")
255
+ import traceback
256
+ traceback.print_exc()
257
  yield error_msg, error_msg
258
 
259