Donyking1818 commited on
Commit
3d4e61f
Β·
verified Β·
1 Parent(s): d440466

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -57
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. 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()
 
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()