Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,107 +3,104 @@ 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.
|
| 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"π
|
| 19 |
|
| 20 |
-
# 2.
|
| 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("β³
|
| 25 |
processor = AutoProcessor.from_pretrained(
|
| 26 |
MODEL_PATH,
|
| 27 |
trust_remote_code=True
|
| 28 |
)
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
| 37 |
-
device_map="auto"
|
| 38 |
-
low_cpu_mem_usage=True
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
model = model.eval()
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
print(f"
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
|
| 49 |
-
# 3.
|
| 50 |
-
def
|
| 51 |
if image is None:
|
| 52 |
-
return "β οΈ
|
| 53 |
|
| 54 |
-
# Prompt
|
| 55 |
-
|
| 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 |
-
|
| 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=
|
| 79 |
-
do_sample=False
|
| 80 |
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 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"
|
| 90 |
|
| 91 |
-
# 4.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
with gr.
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
# 5. Eksekusi
|
| 107 |
if __name__ == "__main__":
|
| 108 |
-
print("β
App siap dijalankan...")
|
| 109 |
app.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# --- KONFIGURASI ALAM SEMESTA ---
|
| 7 |
MODEL_PATH = "zai-org/GLM-OCR"
|
| 8 |
|
| 9 |
+
# 1. HUKUM KEKALAN HARDWARE (Otomatis Deteksi CPU/GPU)
|
| 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"π ENGINE STARTED: Device={device} | Dtype={dtype}")
|
| 18 |
|
| 19 |
+
# 2. INISIASI MODEL (TRUST REMOTE CODE MUTLAK)
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
+
print("β³ Menyiapkan Otak Buatan...")
|
| 22 |
processor = AutoProcessor.from_pretrained(
|
| 23 |
MODEL_PATH,
|
| 24 |
trust_remote_code=True
|
| 25 |
)
|
| 26 |
+
|
| 27 |
+
# Kita pakai AutoModel karena GLM-OCR arsitekturnya unik
|
|
|
|
| 28 |
model = AutoModel.from_pretrained(
|
| 29 |
MODEL_PATH,
|
| 30 |
torch_dtype=dtype,
|
| 31 |
trust_remote_code=True,
|
| 32 |
+
low_cpu_mem_usage=True,
|
| 33 |
+
device_map="auto"
|
|
|
|
| 34 |
)
|
| 35 |
+
# Kunci model ke mode evaluasi biar irit memori
|
| 36 |
+
model.eval()
|
|
|
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
+
print(f"β οΈ Warning Loading Model (Abaikan jika UI Muncul): {e}")
|
| 40 |
+
# Kita biarkan script lanjut karena warning weights mismatch itu wajar di custom model
|
| 41 |
+
pass
|
| 42 |
|
| 43 |
+
# 3. PROSES INFERENSI (INTELIJEN VISUAL)
|
| 44 |
+
def proses_intelijen(image):
|
| 45 |
if image is None:
|
| 46 |
+
return "β οΈ Gambarnya mana Bro? Fisika butuh materi buat bereaksi."
|
| 47 |
|
| 48 |
+
# Format Prompt Khusus GLM
|
| 49 |
+
pesan = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": "Text Recognition:"}]}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
try:
|
|
|
|
| 52 |
inputs = processor.apply_chat_template(
|
| 53 |
+
pesan,
|
| 54 |
add_generation_prompt=True,
|
| 55 |
return_dict=True,
|
| 56 |
return_tensors="pt"
|
| 57 |
).to(model.device)
|
| 58 |
|
|
|
|
| 59 |
with torch.no_grad():
|
| 60 |
output_ids = model.generate(
|
| 61 |
**inputs,
|
| 62 |
+
max_new_tokens=1500, # Batasi biar ga timeout
|
| 63 |
+
do_sample=False
|
| 64 |
)
|
| 65 |
|
| 66 |
+
hasil = output_ids[0][len(inputs["input_ids"][0]):]
|
| 67 |
+
teks_final = processor.decode(hasil, skip_special_tokens=True)
|
|
|
|
|
|
|
| 68 |
return teks_final
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
+
return f"π¨ ERROR REAKSI: {str(e)}"
|
| 72 |
|
| 73 |
+
# 4. ANTARMUKA VISUAL 2026 (Modern Style)
|
| 74 |
+
# HAPUS 'theme' dan 'show_copy_button' di sini biar 100% Anti-Error
|
| 75 |
+
|
| 76 |
+
css_style = """
|
| 77 |
+
.container { max-width: 1200px; margin: auto; padding-top: 20px; }
|
| 78 |
+
h1 { text-align: center; color: #3b82f6; font-family: sans-serif; }
|
| 79 |
+
.gr-button-primary { background: linear-gradient(90deg, #3b82f6 0%, #8b5cf6 100%); border: none; color: white; }
|
| 80 |
+
.info { text-align: center; color: gray; font-size: 0.9em; margin-bottom: 20px;}
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(css=css_style, title="GLM-OCR PRO") as app:
|
| 84 |
+
with gr.Column(elem_classes="container"):
|
| 85 |
+
gr.Markdown("# ποΈ GLM-OCR INFINITE VISION")
|
| 86 |
+
gr.Markdown("<p class='info'>Analisis Dokumen Menggunakan Arsitektur Syaraf GLM Multimodal</p>")
|
| 87 |
|
| 88 |
+
with gr.Row():
|
| 89 |
+
# Kolom Kiri: Input
|
| 90 |
+
with gr.Column(scale=1):
|
| 91 |
+
input_img = gr.Image(type="pil", label="Masukkan Materi (Gambar)", sources=["upload", "clipboard"], height=500)
|
| 92 |
+
# Tombol Ganteng
|
| 93 |
+
scan_btn = gr.Button("π EKSTRAKSI DATA", variant="primary", size="lg")
|
| 94 |
|
| 95 |
+
# Kolom Kanan: Output
|
| 96 |
+
with gr.Column(scale=1):
|
| 97 |
+
# INI DIA FIXNYA: Ganti TextArea jadi Textbox dan HAPUS argumen ilegal
|
| 98 |
+
# Pake 'show_copy_button=True' HANYA jika komponen Textbox, tapi demi keamanan kita pake standar aja.
|
| 99 |
+
# interactive=False biar jadi read-only mode
|
| 100 |
+
output_txt = gr.Textbox(label="Hasil Analisis Molekuler", lines=25, interactive=False, show_label=True)
|
| 101 |
+
|
| 102 |
+
# Trigger Reaksi
|
| 103 |
+
scan_btn.click(fn=proses_intelijen, inputs=input_img, outputs=output_txt)
|
| 104 |
|
|
|
|
| 105 |
if __name__ == "__main__":
|
|
|
|
| 106 |
app.launch()
|