Update app.py
Browse files
app.py
CHANGED
|
@@ -7,45 +7,43 @@ 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 |
model_id,
|
| 13 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
|
|
|
| 14 |
).to(device)
|
| 15 |
|
| 16 |
-
def predict(image,
|
| 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 |
inputs = processor(images=image, text="", return_tensors="pt").to(device)
|
| 22 |
|
| 23 |
-
|
| 24 |
generated_ids = model.generate(
|
| 25 |
**inputs,
|
| 26 |
max_new_tokens=512,
|
| 27 |
do_sample=False,
|
|
|
|
| 28 |
eos_token_id=processor.tokenizer.eos_token_id,
|
| 29 |
-
pad_token_id=processor.tokenizer.pad_token_id
|
| 30 |
-
use_cache=False
|
| 31 |
)
|
| 32 |
|
| 33 |
result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
result = f"[Gợi ý: {question}]\n\n{result}"
|
| 38 |
|
| 39 |
return result
|
| 40 |
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=predict,
|
| 43 |
inputs=[
|
| 44 |
-
gr.Image(type="pil", label="
|
| 45 |
-
gr.Textbox(label="
|
| 46 |
],
|
| 47 |
outputs="text",
|
| 48 |
-
title="Camel-Doc OCR - Trích xuất
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
|
|
|
| 7 |
torch.cuda.empty_cache()
|
| 8 |
|
| 9 |
model_id = "prithivMLmods/Camel-Doc-OCR-062825"
|
| 10 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 11 |
model = AutoModelForVision2Seq.from_pretrained(
|
| 12 |
model_id,
|
| 13 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 14 |
+
trust_remote_code=True
|
| 15 |
).to(device)
|
| 16 |
|
| 17 |
+
def predict(image, prompt=None):
|
| 18 |
image = image.convert("RGB")
|
| 19 |
+
image = image.resize((1280, 960)) # Resize nhẹ nếu cần
|
| 20 |
|
|
|
|
| 21 |
inputs = processor(images=image, text="", return_tensors="pt").to(device)
|
| 22 |
|
|
|
|
| 23 |
generated_ids = model.generate(
|
| 24 |
**inputs,
|
| 25 |
max_new_tokens=512,
|
| 26 |
do_sample=False,
|
| 27 |
+
use_cache=False, # Fix lỗi cache
|
| 28 |
eos_token_id=processor.tokenizer.eos_token_id,
|
| 29 |
+
pad_token_id=processor.tokenizer.pad_token_id
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 33 |
|
| 34 |
+
if prompt:
|
| 35 |
+
result = f"[Gợi ý: {prompt}]\n\n{result}"
|
|
|
|
| 36 |
|
| 37 |
return result
|
| 38 |
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=predict,
|
| 41 |
inputs=[
|
| 42 |
+
gr.Image(type="pil", label="Tải ảnh tài liệu lên"),
|
| 43 |
+
gr.Textbox(label="Gợi ý (tuỳ chọn)", placeholder="VD: Trích số hóa đơn")
|
| 44 |
],
|
| 45 |
outputs="text",
|
| 46 |
+
title="Camel-Doc OCR - Trích xuất văn bản từ ảnh"
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|