Embrw commited on
Commit
e2ead91
·
verified ·
1 Parent(s): ff229b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -11
app.py CHANGED
@@ -13,15 +13,17 @@ IMAGENET_MEAN = (0.485, 0.456, 0.406)
13
  IMAGENET_STD = (0.229, 0.224, 0.225)
14
 
15
  def build_transform(input_size):
16
- transform = T.Compose([
17
  T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
18
  T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
19
  T.ToTensor(),
20
  T.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
21
  ])
22
- return transform
23
 
24
  def load_image(image, input_size=448):
 
 
 
25
  transform = build_transform(input_size=input_size)
26
  pixel_values = transform(image).unsqueeze(0) # Thêm batch dimension
27
  return pixel_values
@@ -36,22 +38,44 @@ model = AutoModel.from_pretrained(
36
 
37
  tokenizer = AutoTokenizer.from_pretrained("5CD-AI/Vintern-1B-v3_5", trust_remote_code=True, use_fast=False)
38
 
39
- def process_image(image):
40
- pixel_values = load_image(image).to(device)
41
- generation_config = dict(max_new_tokens=1024, do_sample=False, num_beams=3, repetition_penalty=2.5)
 
 
42
 
43
- question = "<image>\nTrích xuất toàn bộ thông tin trong ảnh và trả về dạng text."
44
- response, _ = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  return response
47
 
48
  iface = gr.Interface(
49
  fn=process_image,
50
- inputs=gr.Image(type="pil"),
 
 
 
51
  outputs="text",
52
- title="Vietnamese Hand Writing ORC",
53
- description="Extract all the information from the image and return it in text form."
 
54
  )
55
 
56
  if __name__ == "__main__":
57
- iface.launch()
 
13
  IMAGENET_STD = (0.229, 0.224, 0.225)
14
 
15
  def build_transform(input_size):
16
+ return T.Compose([
17
  T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
18
  T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
19
  T.ToTensor(),
20
  T.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
21
  ])
 
22
 
23
  def load_image(image, input_size=448):
24
+ if image is None:
25
+ raise ValueError("Vui lòng tải lên một hình ảnh hợp lệ.")
26
+
27
  transform = build_transform(input_size=input_size)
28
  pixel_values = transform(image).unsqueeze(0) # Thêm batch dimension
29
  return pixel_values
 
38
 
39
  tokenizer = AutoTokenizer.from_pretrained("5CD-AI/Vintern-1B-v3_5", trust_remote_code=True, use_fast=False)
40
 
41
+ def process_image(image, user_request):
42
+ try:
43
+ pixel_values = load_image(image).to(device)
44
+ except ValueError as e:
45
+ return str(e)
46
 
47
+ generation_config = {
48
+ "max_new_tokens": 256, # Giảm số lượng token để tăng tốc
49
+ "do_sample": False,
50
+ "num_beams": 3,
51
+ "repetition_penalty": 2.0
52
+ }
53
+
54
+ # Nếu người dùng không nhập yêu cầu, dùng mặc định
55
+ if not user_request.strip():
56
+ user_request = "Trích xuất toàn bộ thông tin trong ảnh và trả về dạng text."
57
+
58
+ question = f"<image>\n{user_request}"
59
+
60
+ with torch.inference_mode():
61
+ try:
62
+ response, _ = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
63
+ except Exception as e:
64
+ return f"Lỗi khi xử lý ảnh: {e}"
65
 
66
  return response
67
 
68
  iface = gr.Interface(
69
  fn=process_image,
70
+ inputs=[
71
+ gr.Image(type="pil"),
72
+ gr.Textbox(lines=2, placeholder="Nhập yêu cầu của bạn, ví dụ: 'Nhận dạng chữ viết tay và trả về dạng text'"),
73
+ ],
74
  outputs="text",
75
+ title="Vietnamese Handwriting OCR",
76
+ description="Tải ảnh lên nhập yêu cầu của bạn để trích xuất thông tin từ ảnh.",
77
+ theme="dark" # Chuyển sang giao diện tối
78
  )
79
 
80
  if __name__ == "__main__":
81
+ iface.launch()