Donyking1818 commited on
Commit
d440466
·
verified ·
1 Parent(s): db1d09d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -41
app.py CHANGED
@@ -1,90 +1,109 @@
1
  import gradio as gr
2
- from transformers import AutoProcessor, AutoModelForImageTextToText
3
  import torch
4
  from PIL import Image
5
 
6
- # 1. Konfigurasi
7
  MODEL_PATH = "zai-org/GLM-OCR"
8
 
9
- # Paksa deteksi device yang aman
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" Loading model ke: {device} dengan tipe data: {dtype}")
18
 
19
- # 2. Load Model & Processor
20
- # Kita load model dulu baru processor biar manajemen memori lebih rapi di Space gratisan
 
21
  try:
22
- model = AutoModelForImageTextToText.from_pretrained(
 
 
 
 
 
 
 
 
23
  MODEL_PATH,
24
  torch_dtype=dtype,
25
- device_map="auto",
26
  trust_remote_code=True,
27
- low_cpu_mem_usage=True # IQ move buat hemat RAM
 
 
28
  )
29
 
30
- # Kalo device cpu, pastikan model di float32
31
- if device == "cpu":
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"❌ FATAL ERROR LOADING MODEL: {e}")
40
- raise e
 
41
 
42
- # 3. Logic Inferensi
43
- def run_ocr(image):
44
  if image is None:
45
- return "⚠️ Tolong upload gambar dokumennya dulu, Bang."
46
 
47
- # Format Prompt
48
- messages = [
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
- messages,
62
  add_generation_prompt=True,
63
  return_dict=True,
64
  return_tensors="pt"
65
  ).to(model.device)
66
 
67
- # Generate (Batasi token biar gak timeout di CPU)
68
  with torch.no_grad():
69
- generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=False)
70
-
71
- output_text = processor.decode(generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
72
- return output_text
 
 
 
 
 
 
 
73
 
74
  except Exception as e:
75
- return f"Error saat proses OCR: {str(e)}"
76
 
77
- # 4. Interface (Gradio)
78
- with gr.Blocks(title="GLM-OCR by IQ 10M") as demo:
79
- gr.Markdown("# 👁️ GLM-OCR Demo (CPU/GPU Friendly)")
80
- gr.Markdown("Upload gambar dokumen. Sabar ya kalau pake CPU (Gratis), prosesnya agak lama.")
81
 
82
  with gr.Row():
83
- input_img = gr.Image(type="pil", label="Upload Disini", sources=["upload", "clipboard"])
84
- output_txt = gr.TextArea(label="Hasil OCR", interactive=False)
 
 
 
 
85
 
86
- btn = gr.Button("�� Scan Dokumen", variant="primary")
87
- btn.click(fn=run_ocr, inputs=input_img, outputs=output_txt)
88
 
 
89
  if __name__ == "__main__":
90
- demo.launch()
 
 
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()