Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ from transformers import AutoProcessor, AutoModelForImageTextToText
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# --- KONFIGURASI
|
| 7 |
MODEL_PATH = "zai-org/GLM-OCR"
|
| 8 |
|
| 9 |
-
# 1. HUKUM KEKALAN HARDWARE
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
device = "cuda"
|
| 12 |
dtype = torch.float16
|
|
@@ -16,17 +16,17 @@ else:
|
|
| 16 |
|
| 17 |
print(f"🚀 ENGINE STARTED: Device={device} | Dtype={dtype}")
|
| 18 |
|
| 19 |
-
# 2. INISIASI MODEL (
|
| 20 |
try:
|
| 21 |
print("⏳ Menyiapkan Otak GLM...")
|
| 22 |
|
| 23 |
-
# Processor
|
| 24 |
processor = AutoProcessor.from_pretrained(
|
| 25 |
MODEL_PATH,
|
| 26 |
trust_remote_code=True
|
| 27 |
)
|
| 28 |
|
| 29 |
-
# Model (
|
| 30 |
model = AutoModelForImageTextToText.from_pretrained(
|
| 31 |
MODEL_PATH,
|
| 32 |
torch_dtype=dtype,
|
|
@@ -35,20 +35,17 @@ try:
|
|
| 35 |
device_map="auto"
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# Mode Hemat Energi (Evaluasi)
|
| 39 |
model.eval()
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"⚠️ Warning Model
|
| 43 |
-
# Biasanya warning doang, gas terus.
|
| 44 |
pass
|
| 45 |
|
| 46 |
-
# 3.
|
| 47 |
def proses_intelijen(image):
|
| 48 |
if image is None:
|
| 49 |
-
return "⚠️
|
| 50 |
|
| 51 |
-
# Format Pesan Sesuai Standar GLM
|
| 52 |
messages = [
|
| 53 |
{
|
| 54 |
"role": "user",
|
|
@@ -60,64 +57,53 @@ def proses_intelijen(image):
|
|
| 60 |
]
|
| 61 |
|
| 62 |
try:
|
| 63 |
-
#
|
| 64 |
-
# tokenize=True adalah KUNCINYA. Biar dia jadi Tensor (Angka), bukan Teks doang.
|
| 65 |
inputs = processor.apply_chat_template(
|
| 66 |
messages,
|
| 67 |
add_generation_prompt=True,
|
| 68 |
-
tokenize=True,
|
| 69 |
return_dict=True,
|
| 70 |
-
return_tensors="pt"
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
# Buang data sampah yang gak perlu
|
| 74 |
-
inputs.pop("token_type_ids", None)
|
| 75 |
-
|
| 76 |
-
# Pindahkan ke Mesin (Device) secara Manual biar aman
|
| 77 |
-
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 78 |
|
| 79 |
-
# GEBER MESINNYA (GENERATE)
|
| 80 |
with torch.no_grad():
|
| 81 |
generated_ids = model.generate(
|
| 82 |
**inputs,
|
| 83 |
-
max_new_tokens=2048,
|
| 84 |
do_sample=False
|
| 85 |
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
hasil_potong = generated_ids[0][inputs["input_ids"].shape[1]:]
|
| 90 |
-
teks_final = processor.decode(hasil_potong, skip_special_tokens=False) # Coba False dulu biar detail
|
| 91 |
-
|
| 92 |
return teks_final
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
-
return f"🚨 SYSTEM CRITICAL FAILURE: {str(e)}
|
| 96 |
|
| 97 |
-
# 4. ANTARMUKA
|
| 98 |
-
|
|
|
|
| 99 |
.container { max-width: 1200px; margin: auto; padding-top: 20px; }
|
| 100 |
-
h1 { text-align: center; color: #
|
| 101 |
-
.btn-primary { background: linear-gradient(90deg, #1e3a8a 0%, #3b82f6 100%); color: white; border: none; font-size: 1.1rem; }
|
| 102 |
-
.btn-primary:hover { opacity: 0.9; }
|
| 103 |
"""
|
| 104 |
|
| 105 |
-
with gr.Blocks(css=
|
| 106 |
with gr.Column(elem_classes="container"):
|
| 107 |
-
gr.Markdown("# 👁️ GLM-OCR ULTRA
|
| 108 |
-
gr.Markdown("
|
| 109 |
|
| 110 |
with gr.Row():
|
| 111 |
-
# Kolom Input
|
| 112 |
with gr.Column(scale=1):
|
| 113 |
-
input_img = gr.Image(type="pil", label="
|
| 114 |
-
scan_btn = gr.Button("
|
| 115 |
|
| 116 |
-
# Kolom Output (Pake Textbox biasa biar anti error 'show_copy_button')
|
| 117 |
with gr.Column(scale=1):
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
# Pemicu
|
| 121 |
scan_btn.click(fn=proses_intelijen, inputs=input_img, outputs=output_txt)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# --- KONFIGURASI ALAM ---
|
| 7 |
MODEL_PATH = "zai-org/GLM-OCR"
|
| 8 |
|
| 9 |
+
# 1. HUKUM KEKALAN HARDWARE
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
device = "cuda"
|
| 12 |
dtype = torch.float16
|
|
|
|
| 16 |
|
| 17 |
print(f"🚀 ENGINE STARTED: Device={device} | Dtype={dtype}")
|
| 18 |
|
| 19 |
+
# 2. INISIASI MODEL (RELOAD AMAN)
|
| 20 |
try:
|
| 21 |
print("⏳ Menyiapkan Otak GLM...")
|
| 22 |
|
| 23 |
+
# Processor
|
| 24 |
processor = AutoProcessor.from_pretrained(
|
| 25 |
MODEL_PATH,
|
| 26 |
trust_remote_code=True
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# Model (Kita balik ke AutoModelForImageTextToText karena Library lu udh sukses load weights)
|
| 30 |
model = AutoModelForImageTextToText.from_pretrained(
|
| 31 |
MODEL_PATH,
|
| 32 |
torch_dtype=dtype,
|
|
|
|
| 35 |
device_map="auto"
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
model.eval()
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
+
print(f"⚠️ Warning Model (Gas Terus): {e}")
|
|
|
|
| 42 |
pass
|
| 43 |
|
| 44 |
+
# 3. LOGIKA EKSTRAKSI (INTELIJEN)
|
| 45 |
def proses_intelijen(image):
|
| 46 |
if image is None:
|
| 47 |
+
return "⚠️ Gambarnya mana Bos? Upload dulu."
|
| 48 |
|
|
|
|
| 49 |
messages = [
|
| 50 |
{
|
| 51 |
"role": "user",
|
|
|
|
| 57 |
]
|
| 58 |
|
| 59 |
try:
|
| 60 |
+
# IQ 1000: Tokenize=True adalah kunci biar tensor kebaca
|
|
|
|
| 61 |
inputs = processor.apply_chat_template(
|
| 62 |
messages,
|
| 63 |
add_generation_prompt=True,
|
| 64 |
+
tokenize=True,
|
| 65 |
return_dict=True,
|
| 66 |
+
return_tensors="pt"
|
| 67 |
+
).to(model.device) # Paksa pindah ke device model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
| 69 |
with torch.no_grad():
|
| 70 |
generated_ids = model.generate(
|
| 71 |
**inputs,
|
| 72 |
+
max_new_tokens=2048,
|
| 73 |
do_sample=False
|
| 74 |
)
|
| 75 |
|
| 76 |
+
hasil = generated_ids[0][len(inputs["input_ids"][0]):]
|
| 77 |
+
teks_final = processor.decode(hasil, skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
| 78 |
return teks_final
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"🚨 SYSTEM CRITICAL FAILURE: {str(e)}"
|
| 82 |
|
| 83 |
+
# 4. ANTARMUKA (CLEAN VERSION - ANTI ERROR)
|
| 84 |
+
# CSS Kita masukin langsung ke head lewat method modern
|
| 85 |
+
css_custom = """
|
| 86 |
.container { max-width: 1200px; margin: auto; padding-top: 20px; }
|
| 87 |
+
h1 { text-align: center; color: #3b82f6; }
|
|
|
|
|
|
|
| 88 |
"""
|
| 89 |
|
| 90 |
+
with gr.Blocks(css=css_custom, title="GLM-OCR V-FINAL") as app:
|
| 91 |
with gr.Column(elem_classes="container"):
|
| 92 |
+
gr.Markdown("# 👁️ GLM-OCR ULTRA")
|
| 93 |
+
gr.Markdown("Scanner Dokumen Tercerdas.")
|
| 94 |
|
| 95 |
with gr.Row():
|
|
|
|
| 96 |
with gr.Column(scale=1):
|
| 97 |
+
input_img = gr.Image(type="pil", label="Upload Disini", height=450)
|
| 98 |
+
scan_btn = gr.Button("🚀 START SCAN", variant="primary", size="lg")
|
| 99 |
|
|
|
|
| 100 |
with gr.Column(scale=1):
|
| 101 |
+
# INI PERBAIKANNYA BANG:
|
| 102 |
+
# 1. Hapus 'show_copy_button=True' -> Penyebab Error.
|
| 103 |
+
# 2. Hapus 'interactive=False' jika mau teksnya bisa dicopy manual.
|
| 104 |
+
# Kita pakai settingan DEFAULT yang paling aman.
|
| 105 |
+
output_txt = gr.Textbox(label="Hasil Teks", lines=24)
|
| 106 |
|
|
|
|
| 107 |
scan_btn.click(fn=proses_intelijen, inputs=input_img, outputs=output_txt)
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|