Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,9 @@ from transformers import AutoProcessor, AutoModelForVision2Seq
|
|
| 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(
|
|
@@ -15,12 +13,9 @@ model = AutoModelForVision2Seq.from_pretrained(
|
|
| 15 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 16 |
).to(device)
|
| 17 |
|
| 18 |
-
|
| 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 |
|
|
@@ -33,14 +28,21 @@ def predict(image):
|
|
| 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=
|
|
|
|
|
|
|
|
|
|
| 42 |
outputs="text",
|
| 43 |
-
title="Camel-Doc OCR - Trích xuất
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
|
|
|
| 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(
|
|
|
|
| 13 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 14 |
).to(device)
|
| 15 |
|
| 16 |
+
def predict(image, question=None):
|
|
|
|
| 17 |
image = image.convert("RGB")
|
| 18 |
+
image = image.resize((1280, 960)) # Resize nếu cần
|
|
|
|
|
|
|
| 19 |
|
| 20 |
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 21 |
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 31 |
+
|
| 32 |
+
# Gắn câu hỏi vào nếu cần log/debug
|
| 33 |
+
if question:
|
| 34 |
+
result = f"[Gợi ý: {question}]\n\n{result}"
|
| 35 |
+
|
| 36 |
return result
|
| 37 |
|
|
|
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=predict,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Image(type="pil", label="Upload ảnh tài liệu"),
|
| 42 |
+
gr.Textbox(label="Bạn muốn hỏi gì (tuỳ chọn)", placeholder="VD: Trích số đơn hàng")
|
| 43 |
+
],
|
| 44 |
outputs="text",
|
| 45 |
+
title="Camel-Doc OCR - Trích xuất tài liệu từ ảnh"
|
| 46 |
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|