Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,27 @@ from transformers import AutoProcessor, AutoModelForVision2Seq
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
torch.cuda.empty_cache()
|
| 8 |
-
|
|
|
|
| 9 |
model_id = "prithivMLmods/Camel-Doc-OCR-062825"
|
| 10 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 11 |
-
model = AutoModelForVision2Seq.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
def predict(image
|
| 15 |
image = image.convert("RGB")
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
generated_ids = model.generate(
|
| 18 |
**inputs,
|
| 19 |
max_new_tokens=512,
|
|
@@ -21,19 +31,16 @@ def predict(image, text_input, system_prompt="Trích thông tin, không cần di
|
|
| 21 |
eos_token_id=processor.tokenizer.eos_token_id,
|
| 22 |
pad_token_id=processor.tokenizer.pad_token_id
|
| 23 |
)
|
|
|
|
| 24 |
result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 25 |
return result
|
| 26 |
|
| 27 |
-
# Gradio
|
| 28 |
demo = gr.Interface(
|
| 29 |
fn=predict,
|
| 30 |
-
inputs=
|
| 31 |
-
gr.Image(type="pil", label="Upload ảnh tài liệu"),
|
| 32 |
-
gr.Textbox(label="Câu hỏi muốn hỏi mô hình"),
|
| 33 |
-
gr.Textbox(label="System prompt (tuỳ chọn)", value="Trích thông tin, không cần diễn giải")
|
| 34 |
-
],
|
| 35 |
outputs="text",
|
| 36 |
-
title="
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Thiết lập thiết bị
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
torch.cuda.empty_cache()
|
| 9 |
+
|
| 10 |
+
# Load mô hình và processor
|
| 11 |
model_id = "prithivMLmods/Camel-Doc-OCR-062825"
|
| 12 |
processor = AutoProcessor.from_pretrained(model_id)
|
| 13 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 14 |
+
model_id,
|
| 15 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 16 |
+
).to(device)
|
| 17 |
|
| 18 |
+
# Hàm xử lý dự đoán
|
| 19 |
+
def predict(image):
|
| 20 |
image = image.convert("RGB")
|
| 21 |
+
|
| 22 |
+
# Resize để tránh tốn RAM nếu cần (có thể tùy chỉnh)
|
| 23 |
+
image = image.resize((1280, 960))
|
| 24 |
+
|
| 25 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 26 |
+
|
| 27 |
generated_ids = model.generate(
|
| 28 |
**inputs,
|
| 29 |
max_new_tokens=512,
|
|
|
|
| 31 |
eos_token_id=processor.tokenizer.eos_token_id,
|
| 32 |
pad_token_id=processor.tokenizer.pad_token_id
|
| 33 |
)
|
| 34 |
+
|
| 35 |
result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 36 |
return result
|
| 37 |
|
| 38 |
+
# Giao diện Gradio đơn giản
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=predict,
|
| 41 |
+
inputs=gr.Image(type="pil", label="Upload ảnh tài liệu"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
outputs="text",
|
| 43 |
+
title="Camel-Doc OCR - Trích xuất văn bản từ tài liệu"
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|