Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,34 +4,39 @@ from datetime import datetime
|
|
| 4 |
import json
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
-
import pytesseract
|
| 8 |
-
from
|
|
|
|
| 9 |
|
| 10 |
-
# --- CẤU HÌNH VÀ TẢI MÔ HÌNH (
|
| 11 |
print("Ứng dụng đang khởi động...")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
print("Sử dụng Tesseract OCR (siêu nhẹ).")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
print("Đang tải mô hình LLM (Llama-3-8B GGUF for CPU)...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
-
print("Tải xong mô hình LLM.")
|
| 28 |
|
| 29 |
|
| 30 |
# --- CÁC HÀM XỬ LÝ ---
|
| 31 |
|
| 32 |
def run_ocr(image: Image.Image) -> str:
|
| 33 |
-
#
|
| 34 |
-
"""Hàm chạy Tesseract OCR để đọc chữ từ ảnh"""
|
| 35 |
try:
|
| 36 |
text = pytesseract.image_to_string(image)
|
| 37 |
return text
|
|
@@ -40,13 +45,23 @@ def run_ocr(image: Image.Image) -> str:
|
|
| 40 |
return "Lỗi khi đọc chữ từ ảnh."
|
| 41 |
|
| 42 |
def extract_order_from_text(text: str) -> dict:
|
| 43 |
-
#
|
| 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:
|
| 47 |
{text}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
| 48 |
"""
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
json_str = response_text.strip()
|
| 52 |
start = json_str.find('{')
|
|
@@ -58,7 +73,7 @@ def extract_order_from_text(text: str) -> dict:
|
|
| 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
|
| 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')
|
|
@@ -76,7 +91,7 @@ def create_excel_file(order_data: dict):
|
|
| 76 |
return (filename, output.getvalue())
|
| 77 |
|
| 78 |
def process_image_and_extract(image):
|
| 79 |
-
# (Giữ nguyên
|
| 80 |
try:
|
| 81 |
if image is None: return "Vui lòng dán ảnh vào.", None, None
|
| 82 |
extracted_text = run_ocr(image)
|
|
|
|
| 4 |
import json
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
+
import pytesseract
|
| 8 |
+
from huggingface_hub import hf_hub_download # Thư viện mới
|
| 9 |
+
from llama_cpp import Llama # Thư viện mới
|
| 10 |
|
| 11 |
+
# --- CẤU HÌNH VÀ TẢI MÔ HÌNH (SỬ DỤNG LLAMA.CPP) ---
|
| 12 |
print("Ứng dụng đang khởi động...")
|
| 13 |
|
| 14 |
+
# OCR không thay đổi
|
| 15 |
print("Sử dụng Tesseract OCR (siêu nhẹ).")
|
| 16 |
|
| 17 |
+
# THAY ĐỔI LỚN: DÙNG LLAMA-CPP-PYTHON
|
| 18 |
print("Đang tải mô hình LLM (Llama-3-8B GGUF for CPU)...")
|
| 19 |
+
model_repo = "bartowski/Meta-Llama-3-8B-Instruct-GGUF"
|
| 20 |
+
model_file = "Meta-Llama-3-8B-Instruct-Q4_K_M.gguf"
|
| 21 |
+
|
| 22 |
+
# Tải file mô hình về cache của Space
|
| 23 |
+
model_path = hf_hub_download(repo_id=model_repo, filename=model_file)
|
| 24 |
+
print(f"Đã tải xong file mô hình tại: {model_path}")
|
| 25 |
+
|
| 26 |
+
# Khởi tạo mô hình từ file đã tải
|
| 27 |
+
llm = Llama(
|
| 28 |
+
model_path=model_path,
|
| 29 |
+
n_ctx=4096, # Context length
|
| 30 |
+
n_gpu_layers=0, # Chạy hoàn toàn trên CPU
|
| 31 |
+
verbose=True, # In ra thông tin để debug
|
| 32 |
)
|
| 33 |
+
print("Tải xong và khởi tạo thành công mô hình LLM.")
|
| 34 |
|
| 35 |
|
| 36 |
# --- CÁC HÀM XỬ LÝ ---
|
| 37 |
|
| 38 |
def run_ocr(image: Image.Image) -> str:
|
| 39 |
+
# (Giữ nguyên)
|
|
|
|
| 40 |
try:
|
| 41 |
text = pytesseract.image_to_string(image)
|
| 42 |
return text
|
|
|
|
| 45 |
return "Lỗi khi đọc chữ từ ảnh."
|
| 46 |
|
| 47 |
def extract_order_from_text(text: str) -> dict:
|
| 48 |
+
# Cập nhật prompt và cách gọi cho Llama.cpp
|
| 49 |
prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
| 50 |
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|>
|
| 51 |
Text Content:
|
| 52 |
{text}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
| 53 |
"""
|
| 54 |
+
|
| 55 |
+
output = llm(
|
| 56 |
+
prompt,
|
| 57 |
+
max_tokens=1024,
|
| 58 |
+
stop=["<|eot_id|>"],
|
| 59 |
+
temperature=0.1,
|
| 60 |
+
echo=False # Không in lại prompt trong kết quả
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
response_text = output['choices'][0]['text']
|
| 64 |
+
|
| 65 |
try:
|
| 66 |
json_str = response_text.strip()
|
| 67 |
start = json_str.find('{')
|
|
|
|
| 73 |
return {"error": "AI trả về định dạng không hợp lệ", "raw_response": response_text}
|
| 74 |
|
| 75 |
def create_excel_file(order_data: dict):
|
| 76 |
+
# (Giữ nguyên)
|
| 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')
|
|
|
|
| 91 |
return (filename, output.getvalue())
|
| 92 |
|
| 93 |
def process_image_and_extract(image):
|
| 94 |
+
# (Giữ nguyên)
|
| 95 |
try:
|
| 96 |
if image is None: return "Vui lòng dán ảnh vào.", None, None
|
| 97 |
extracted_text = run_ocr(image)
|