Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,17 @@ from datetime import datetime
|
|
| 4 |
import json
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
-
import
|
| 8 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 9 |
from ctransformers import AutoModelForCausalLM as CAutoModelForCausalLM
|
| 10 |
|
| 11 |
-
# --- CẤU HÌNH VÀ TẢI MÔ HÌNH (PHIÊN BẢN
|
| 12 |
-
|
| 13 |
-
print(f"Đang sử dụng thiết bị: {device}")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
print("
|
| 17 |
-
ocr_model_id = "microsoft/Florence-2-base"
|
| 18 |
-
ocr_processor = AutoProcessor.from_pretrained(ocr_model_id, trust_remote_code=True)
|
| 19 |
-
ocr_model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
-
ocr_model_id,
|
| 21 |
-
device_map=device,
|
| 22 |
-
torch_dtype=torch.float32,
|
| 23 |
-
trust_remote_code=True,
|
| 24 |
-
attn_implementation="eager"
|
| 25 |
-
)
|
| 26 |
-
print("Tải xong mô hình OCR.")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
print("Đang tải mô hình LLM (Llama-3-8B GGUF for CPU)...")
|
| 30 |
-
# SỬ DỤNG PHIÊN BẢN GGUF TỪ MỘT NHÀ CUNG CẤP KHÁC
|
| 31 |
llm_model_id = "bartowski/Meta-Llama-3-8B-Instruct-GGUF"
|
| 32 |
llm_model_file = "Meta-Llama-3-8B-Instruct-Q4_K_M.gguf"
|
| 33 |
llm = CAutoModelForCausalLM.from_pretrained(
|
|
@@ -43,20 +30,17 @@ print("Tải xong mô hình LLM.")
|
|
| 43 |
# --- CÁC HÀM XỬ LÝ ---
|
| 44 |
|
| 45 |
def run_ocr(image: Image.Image) -> str:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
)
|
| 55 |
-
generated_text = ocr_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 56 |
-
parsed_text = ocr_processor.post_process_generation(generated_text, task="<OCR>", image_size=(image.width, image.height))
|
| 57 |
-
return parsed_text['<OCR>']
|
| 58 |
|
| 59 |
def extract_order_from_text(text: str) -> dict:
|
|
|
|
| 60 |
prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
| 61 |
You are an expert assistant that only outputs valid JSON. Extract order information from the text. The JSON object must contain "ten_khach_hang" (string, null if not found) and "danh_sach_hang" (an array of items). Each item must have "ten_hang" (string), "so_luong" (number), "don_vi" (string), "ma_hang" (string, null if not found), and "ghi_chu" (string, null if not found).<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 62 |
Text Content:
|
|
@@ -74,6 +58,7 @@ def extract_order_from_text(text: str) -> dict:
|
|
| 74 |
return {"error": "AI trả về định dạng không hợp lệ", "raw_response": response_text}
|
| 75 |
|
| 76 |
def create_excel_file(order_data: dict):
|
|
|
|
| 77 |
if not order_data or "danh_sach_hang" not in order_data or not order_data["danh_sach_hang"]: return None
|
| 78 |
flat_data = []
|
| 79 |
customer = order_data.get('ten_khach_hang', 'N/A')
|
|
@@ -86,11 +71,12 @@ def create_excel_file(order_data: dict):
|
|
| 86 |
df = pd.DataFrame(flat_data)
|
| 87 |
output = io.BytesIO()
|
| 88 |
with pd.ExcelWriter(output, engine='openpyxl') as writer: df.to_excel(writer, index=False, sheet_name='DonHang')
|
| 89 |
-
timestamp = datetime.now().strftime("%
|
| 90 |
filename = f"don_hang_{timestamp}.xlsx"
|
| 91 |
return (filename, output.getvalue())
|
| 92 |
|
| 93 |
def process_image_and_extract(image):
|
|
|
|
| 94 |
try:
|
| 95 |
if image is None: return "Vui lòng dán ảnh vào.", None, None
|
| 96 |
extracted_text = run_ocr(image)
|
|
@@ -111,7 +97,7 @@ def process_image_and_extract(image):
|
|
| 111 |
print(traceback_str)
|
| 112 |
return f"Lỗi nghiêm trọng: {error_str}", None, None
|
| 113 |
|
| 114 |
-
# --- XÂY DỰNG GIAO DIỆN GRADIO ---
|
| 115 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 116 |
gr.Markdown("# Ứng dụng Trích xuất Đơn hàng từ Ảnh chụp màn hình")
|
| 117 |
gr.Markdown("Chụp màn hình email/tin nhắn đặt hàng, sau đó dán (Ctrl+V) vào ô bên dưới và nhấn 'Xử lý'.")
|
|
|
|
| 4 |
import json
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
+
import pytesseract # THAY ĐỔI 1: Thư viện OCR mới
|
|
|
|
| 8 |
from ctransformers import AutoModelForCausalLM as CAutoModelForCausalLM
|
| 9 |
|
| 10 |
+
# --- CẤU HÌNH VÀ TẢI MÔ HÌNH (PHIÊN BẢN SIÊU NHẸ) ---
|
| 11 |
+
print("Ứng dụng đang khởi động...")
|
|
|
|
| 12 |
|
| 13 |
+
# THAY ĐỔI 2: XÓA HOÀN TOÀN KHỐI TẢI MÔ HÌNH OCR NẶNG NỀ
|
| 14 |
+
print("Sử dụng Tesseract OCR (siêu nhẹ).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# DÙNG CTRANSFORMERS ĐỂ TẢI GGUF (Giữ nguyên)
|
| 17 |
print("Đang tải mô hình LLM (Llama-3-8B GGUF for CPU)...")
|
|
|
|
| 18 |
llm_model_id = "bartowski/Meta-Llama-3-8B-Instruct-GGUF"
|
| 19 |
llm_model_file = "Meta-Llama-3-8B-Instruct-Q4_K_M.gguf"
|
| 20 |
llm = CAutoModelForCausalLM.from_pretrained(
|
|
|
|
| 30 |
# --- CÁC HÀM XỬ LÝ ---
|
| 31 |
|
| 32 |
def run_ocr(image: Image.Image) -> str:
|
| 33 |
+
# THAY ĐỔI 3: VIẾT LẠI HOÀN TOÀN HÀM OCR
|
| 34 |
+
"""Hàm chạy Tesseract OCR để đọc chữ từ ảnh"""
|
| 35 |
+
try:
|
| 36 |
+
text = pytesseract.image_to_string(image)
|
| 37 |
+
return text
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Lỗi Tesseract: {e}")
|
| 40 |
+
return "Lỗi khi đọc chữ từ ảnh."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def extract_order_from_text(text: str) -> dict:
|
| 43 |
+
# (Giữ nguyên không thay đổi)
|
| 44 |
prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
| 45 |
You are an expert assistant that only outputs valid JSON. Extract order information from the text. The JSON object must contain "ten_khach_hang" (string, null if not found) and "danh_sach_hang" (an array of items). Each item must have "ten_hang" (string), "so_luong" (number), "don_vi" (string), "ma_hang" (string, null if not found), and "ghi_chu" (string, null if not found).<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 46 |
Text Content:
|
|
|
|
| 58 |
return {"error": "AI trả về định dạng không hợp lệ", "raw_response": response_text}
|
| 59 |
|
| 60 |
def create_excel_file(order_data: dict):
|
| 61 |
+
# (Giữ nguyên không thay đổi)
|
| 62 |
if not order_data or "danh_sach_hang" not in order_data or not order_data["danh_sach_hang"]: return None
|
| 63 |
flat_data = []
|
| 64 |
customer = order_data.get('ten_khach_hang', 'N/A')
|
|
|
|
| 71 |
df = pd.DataFrame(flat_data)
|
| 72 |
output = io.BytesIO()
|
| 73 |
with pd.ExcelWriter(output, engine='openpyxl') as writer: df.to_excel(writer, index=False, sheet_name='DonHang')
|
| 74 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 75 |
filename = f"don_hang_{timestamp}.xlsx"
|
| 76 |
return (filename, output.getvalue())
|
| 77 |
|
| 78 |
def process_image_and_extract(image):
|
| 79 |
+
# (Giữ nguyên không thay đổi)
|
| 80 |
try:
|
| 81 |
if image is None: return "Vui lòng dán ảnh vào.", None, None
|
| 82 |
extracted_text = run_ocr(image)
|
|
|
|
| 97 |
print(traceback_str)
|
| 98 |
return f"Lỗi nghiêm trọng: {error_str}", None, None
|
| 99 |
|
| 100 |
+
# --- XÂY DỰNG GIAO DIỆN GRADIO (GIỮ NGUYÊN) ---
|
| 101 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 102 |
gr.Markdown("# Ứng dụng Trích xuất Đơn hàng từ Ảnh chụp màn hình")
|
| 103 |
gr.Markdown("Chụp màn hình email/tin nhắn đặt hàng, sau đó dán (Ctrl+V) vào ô bên dưới và nhấn 'Xử lý'.")
|