Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# 1. Konfigurasi Model
|
| 7 |
+
# Pake CPU kalau gak ada GPU, pake cuda kalau ada
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 10 |
+
|
| 11 |
+
MODEL_PATH = "zai-org/GLM-OCR"
|
| 12 |
+
|
| 13 |
+
print(f"Loading model ke {device}...")
|
| 14 |
+
|
| 15 |
+
# 2. Load Processor dan Model
|
| 16 |
+
try:
|
| 17 |
+
processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 18 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 19 |
+
MODEL_PATH,
|
| 20 |
+
torch_dtype=dtype,
|
| 21 |
+
device_map="auto",
|
| 22 |
+
trust_remote_code=True
|
| 23 |
+
)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Error loading model: {e}")
|
| 26 |
+
raise e
|
| 27 |
+
|
| 28 |
+
# 3. Fungsi Inferensi
|
| 29 |
+
def run_ocr(image):
|
| 30 |
+
if image is None:
|
| 31 |
+
return "Tolong upload gambar dulu, Bro."
|
| 32 |
+
|
| 33 |
+
# Format pesan khusus GLM-OCR
|
| 34 |
+
messages = [
|
| 35 |
+
{
|
| 36 |
+
"role": "user",
|
| 37 |
+
"content": [
|
| 38 |
+
{
|
| 39 |
+
"type": "image",
|
| 40 |
+
"image": image, # Gradio ngasih format PIL Image
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"type": "text",
|
| 44 |
+
"text": "Text Recognition:"
|
| 45 |
+
}
|
| 46 |
+
],
|
| 47 |
+
}
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
# Proses input
|
| 51 |
+
inputs = processor.apply_chat_template(
|
| 52 |
+
messages,
|
| 53 |
+
add_generation_prompt=True,
|
| 54 |
+
return_dict=True,
|
| 55 |
+
return_tensors="pt"
|
| 56 |
+
).to(model.device)
|
| 57 |
+
|
| 58 |
+
# Generate (OCR)
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
generated_ids = model.generate(**inputs, max_new_tokens=2048) # Token bisa dinaikin kalo dokumen panjang
|
| 61 |
+
|
| 62 |
+
# Decode hasil jadi teks
|
| 63 |
+
output_text = processor.decode(generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 64 |
+
|
| 65 |
+
return output_text
|
| 66 |
+
|
| 67 |
+
# 4. Bikin Tampilan UI (Gradio)
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# 👁️ GLM-OCR Demo")
|
| 70 |
+
gr.Markdown("Upload gambar dokumen, surat, atau teks tulisan tangan. Model ini jago baca layout.")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column():
|
| 74 |
+
input_img = gr.Image(type="pil", label="Upload Gambar")
|
| 75 |
+
btn_submit = gr.Button("Baca Teks (OCR)", variant="primary")
|
| 76 |
+
|
| 77 |
+
with gr.Column():
|
| 78 |
+
output_txt = gr.Textbox(label="Hasil Text / Markdown", lines=20)
|
| 79 |
+
|
| 80 |
+
btn_submit.click(fn=run_ocr, inputs=input_img, outputs=output_txt)
|
| 81 |
+
|
| 82 |
+
# Jalankan App
|
| 83 |
+
demo.launch()
|