Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,109 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoProcessor,
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
#
|
| 7 |
MODEL_PATH = "zai-org/GLM-OCR"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
device = "cuda"
|
| 12 |
dtype = torch.float16
|
| 13 |
else:
|
| 14 |
device = "cpu"
|
|
|
|
| 15 |
dtype = torch.float32
|
| 16 |
|
| 17 |
-
print(f"
|
| 18 |
|
| 19 |
-
# 2.
|
| 20 |
-
#
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
MODEL_PATH,
|
| 24 |
torch_dtype=dtype,
|
| 25 |
-
device_map="auto",
|
| 26 |
trust_remote_code=True,
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
model = model.float()
|
| 33 |
|
| 34 |
-
processor = AutoProcessor.from_pretrained(
|
| 35 |
-
MODEL_PATH,
|
| 36 |
-
trust_remote_code=True
|
| 37 |
-
)
|
| 38 |
except Exception as e:
|
| 39 |
-
print(f"❌
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
# 3.
|
| 43 |
-
def
|
| 44 |
if image is None:
|
| 45 |
-
return "⚠️
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
{
|
| 50 |
"role": "user",
|
| 51 |
"content": [
|
| 52 |
{"type": "image", "image": image},
|
| 53 |
{"type": "text", "text": "Text Recognition:"}
|
| 54 |
-
]
|
| 55 |
}
|
| 56 |
]
|
| 57 |
|
| 58 |
-
# Proses
|
| 59 |
try:
|
|
|
|
| 60 |
inputs = processor.apply_chat_template(
|
| 61 |
-
|
| 62 |
add_generation_prompt=True,
|
| 63 |
return_dict=True,
|
| 64 |
return_tensors="pt"
|
| 65 |
).to(model.device)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
with torch.no_grad():
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
return f"Error
|
| 76 |
|
| 77 |
-
# 4.
|
| 78 |
-
with gr.Blocks(
|
| 79 |
-
gr.Markdown("#
|
| 80 |
-
gr.Markdown("
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
btn.click(fn=run_ocr, inputs=input_img, outputs=output_txt)
|
| 88 |
|
|
|
|
| 89 |
if __name__ == "__main__":
|
| 90 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModel
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# --- KONFIGURASI ---
|
| 7 |
MODEL_PATH = "zai-org/GLM-OCR"
|
| 8 |
|
| 9 |
+
# 1. Deteksi Hardware (Biar gak maksain CPU nangis)
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
device = "cuda"
|
| 12 |
dtype = torch.float16
|
| 13 |
else:
|
| 14 |
device = "cpu"
|
| 15 |
+
# Pake float32 aja buat CPU biar aman, walau lambat
|
| 16 |
dtype = torch.float32
|
| 17 |
|
| 18 |
+
print(f"🚀 Mulai System: Device={device} | Dtype={dtype}")
|
| 19 |
|
| 20 |
+
# 2. LOAD MODEL (LOGIKA BARU)
|
| 21 |
+
# GLM-OCR butuh arsitektur khusus. Kita pake 'AutoModel' bukan 'AutoModelForImageTextToText'
|
| 22 |
+
# karena 'AutoModel' lebih fleksibel buat nerima arsitektur custom via remote code.
|
| 23 |
try:
|
| 24 |
+
print("⏳ Loading Processor...")
|
| 25 |
+
processor = AutoProcessor.from_pretrained(
|
| 26 |
+
MODEL_PATH,
|
| 27 |
+
trust_remote_code=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
print("⏳ Loading Model...")
|
| 31 |
+
# Pake AutoModel biasa, dia bakal baca config.json dan narik class GLMOCRModel otomatis
|
| 32 |
+
model = AutoModel.from_pretrained(
|
| 33 |
MODEL_PATH,
|
| 34 |
torch_dtype=dtype,
|
|
|
|
| 35 |
trust_remote_code=True,
|
| 36 |
+
# Opsi memori buat CPU (PENTING BUAT SPACE GRATISAN)
|
| 37 |
+
device_map="auto" if device == "cuda" else "cpu",
|
| 38 |
+
low_cpu_mem_usage=True
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# Kalau CPU, model di eval mode
|
| 42 |
+
model = model.eval()
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
+
print(f"❌ KEGAGALAN SISTEM: {e}")
|
| 46 |
+
# Pesan ini biar muncul di log kalau error
|
| 47 |
+
raise ValueError(f"Gagal Load Model GLM-OCR. Detail: {e}")
|
| 48 |
|
| 49 |
+
# 3. Fungsi Kerja (Inferensi)
|
| 50 |
+
def proses_gambar(image):
|
| 51 |
if image is None:
|
| 52 |
+
return "⚠️ Woi bro, upload gambarnya dulu dong."
|
| 53 |
|
| 54 |
+
# Prompt standar GLM-OCR
|
| 55 |
+
pesan_user = [
|
| 56 |
{
|
| 57 |
"role": "user",
|
| 58 |
"content": [
|
| 59 |
{"type": "image", "image": image},
|
| 60 |
{"type": "text", "text": "Text Recognition:"}
|
| 61 |
+
]
|
| 62 |
}
|
| 63 |
]
|
| 64 |
|
|
|
|
| 65 |
try:
|
| 66 |
+
# Preprocessing input
|
| 67 |
inputs = processor.apply_chat_template(
|
| 68 |
+
pesan_user,
|
| 69 |
add_generation_prompt=True,
|
| 70 |
return_dict=True,
|
| 71 |
return_tensors="pt"
|
| 72 |
).to(model.device)
|
| 73 |
|
| 74 |
+
# Generasi Teks
|
| 75 |
with torch.no_grad():
|
| 76 |
+
output_ids = model.generate(
|
| 77 |
+
**inputs,
|
| 78 |
+
max_new_tokens=1024, # Biar ga kepanjangan render di CPU
|
| 79 |
+
do_sample=False # Greedy search biar stabil
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Ambil hasilnya doang (potong input dari output)
|
| 83 |
+
hasil_asli = output_ids[0][len(inputs["input_ids"][0]):]
|
| 84 |
+
teks_final = processor.decode(hasil_asli, skip_special_tokens=True)
|
| 85 |
+
|
| 86 |
+
return teks_final
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
+
return f"😭 Error Pas Baca Gambar: {str(e)}"
|
| 90 |
|
| 91 |
+
# 4. Tampilan Web (UI)
|
| 92 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 93 |
+
gr.Markdown("# 🔍 GLM-OCR Detector (Versi IQ Tinggi)")
|
| 94 |
+
gr.Markdown("Model OCR canggih buat baca dokumen rumit. Upload aja langsung scan.")
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
+
with gr.Column():
|
| 98 |
+
img_input = gr.Image(type="pil", label="Gambar Dokumen/Teks", sources=["upload", "clipboard"])
|
| 99 |
+
scan_btn = gr.Button("🚀 MULAI SCAN", variant="primary")
|
| 100 |
+
|
| 101 |
+
with gr.Column():
|
| 102 |
+
txt_output = gr.TextArea(label="Hasil Bacaan", show_copy_button=True, lines=20)
|
| 103 |
|
| 104 |
+
scan_btn.click(fn=proses_gambar, inputs=img_input, outputs=txt_output)
|
|
|
|
| 105 |
|
| 106 |
+
# 5. Eksekusi
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
print("✅ App siap dijalankan...")
|
| 109 |
+
app.launch()
|