Antharee commited on
Commit
ef180e8
·
verified ·
1 Parent(s): 2c56320

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -1,26 +1,40 @@
1
- from transformers import AutoProcessor, AutoModelForVision2Seq
2
  import torch
3
  from PIL import Image
4
  import gradio as gr
 
5
 
6
- processor = AutoProcessor.from_pretrained("scb10x/typhoon-ocr-1b")
7
  model = AutoModelForVision2Seq.from_pretrained(
8
- "scb10x/typhoon-ocr-1b",
9
  torch_dtype=torch.float16,
10
- device_map="auto"
 
11
  )
12
 
13
  def ocr_infer(image):
14
  try:
 
 
 
15
  image = image.convert("RGB")
16
  inputs = processor(images=image, return_tensors="pt")
 
 
 
 
17
  inputs = {k: v.to(model.device) for k, v in inputs.items()}
18
-
19
  generated_ids = model.generate(**inputs, max_new_tokens=256)
20
  result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
21
  return result
 
22
  except Exception as e:
23
- return f"❌ Error: {e}"
 
 
 
 
 
 
 
24
 
25
- iface = gr.Interface(fn=ocr_infer, inputs=gr.Image(type="pil"), outputs="text", title="Typhoon OCR 1B")
26
  iface.launch()
 
 
1
  import torch
2
  from PIL import Image
3
  import gradio as gr
4
+ from transformers import AutoProcessor, AutoModelForVision2Seq
5
 
6
+ processor = AutoProcessor.from_pretrained("scb10x/typhoon-ocr-3b", use_auth_token=False)
7
  model = AutoModelForVision2Seq.from_pretrained(
8
+ "scb10x/typhoon-ocr-3b",
9
  torch_dtype=torch.float16,
10
+ device_map="auto",
11
+ use_auth_token=False
12
  )
13
 
14
  def ocr_infer(image):
15
  try:
16
+ if image is None:
17
+ return "❌ Error: No image provided"
18
+
19
  image = image.convert("RGB")
20
  inputs = processor(images=image, return_tensors="pt")
21
+
22
+ if inputs is None or "pixel_values" not in inputs:
23
+ return "❌ Error: Invalid processor output"
24
+
25
  inputs = {k: v.to(model.device) for k, v in inputs.items()}
 
26
  generated_ids = model.generate(**inputs, max_new_tokens=256)
27
  result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
28
  return result
29
+
30
  except Exception as e:
31
+ return f"❌ Error during inference: {e}"
32
+
33
+ iface = gr.Interface(
34
+ fn=ocr_infer,
35
+ inputs=gr.Image(type="pil"),
36
+ outputs="text",
37
+ title="Typhoon OCR 3B"
38
+ )
39
 
 
40
  iface.launch()