pratyyush commited on
Commit
63b2a7e
·
verified ·
1 Parent(s): 8906490

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -3,50 +3,53 @@ from transformers import AutoModel, AutoTokenizer
3
  import torch
4
  from PIL import Image
5
 
6
- # 1. Load the High-Accuracy Model
7
- # Note: Using AutoModel instead of AutoModelForCausalLM for custom GLM architectures
8
  model_id = "zai-org/GLM-OCR"
9
 
 
 
10
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
11
  model = AutoModel.from_pretrained(
12
  model_id,
13
  trust_remote_code=True,
14
  device_map="auto",
15
- dtype=torch.float16 # Fixed the deprecated parameter
16
  ).eval()
17
 
18
  def process_accountancy_image(input_image):
19
  if input_image is None:
20
  return "Please upload an image."
21
 
22
- # 2. Define the Prompt for Accountancy
23
  prompt = (
24
  "Extract all text and tables from this image. "
25
  "If there is an accountancy table (Ledger, Balance Sheet, P&L), "
26
  "format it strictly as a Markdown table. Preserve all symbols and currency signs."
27
  )
28
 
29
- # 3. Run Inference
30
- # For GLM models, we use the specific chat input builder provided by their remote code
31
- inputs = tokenizer.build_chat_input(prompt, images=[input_image], history=[])
32
- inputs = {k: v.to(model.device) for k, v in inputs.items() if torch.is_tensor(v)}
33
-
34
- with torch.no_grad():
35
- # GLM-OCR typically uses a custom generate method
36
- outputs = model.generate(**inputs, max_new_tokens=2048)
37
- # Decoding logic
38
- response = tokenizer.decode(outputs[0][len(inputs['input_ids'][0]):], skip_special_tokens=True)
39
-
40
- return response
41
-
42
- # 4. User Interface
43
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
44
  gr.Markdown("# 📊 Professional Accountancy Table Extractor")
45
- gr.Markdown("High-accuracy extraction for ledgers and financial questions.")
46
 
47
  with gr.Row():
48
  with gr.Column():
49
- image_input = gr.Image(type="pil", label="Upload Image")
50
  submit_btn = gr.Button("Extract Text & Tables", variant="primary")
51
 
52
  with gr.Column():
@@ -54,4 +57,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
54
 
55
  submit_btn.click(fn=process_accountancy_image, inputs=image_input, outputs=text_output)
56
 
 
57
  demo.launch()
 
3
  import torch
4
  from PIL import Image
5
 
6
+ # Use the same model ID
 
7
  model_id = "zai-org/GLM-OCR"
8
 
9
+ # 1. Load the tokenizer and model with remote code enabled
10
+ # We use AutoModel because GLM-OCR has a custom vision-language architecture
11
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
12
  model = AutoModel.from_pretrained(
13
  model_id,
14
  trust_remote_code=True,
15
  device_map="auto",
16
+ torch_dtype=torch.float16
17
  ).eval()
18
 
19
  def process_accountancy_image(input_image):
20
  if input_image is None:
21
  return "Please upload an image."
22
 
23
+ # Define the specific prompt for accountancy extraction
24
  prompt = (
25
  "Extract all text and tables from this image. "
26
  "If there is an accountancy table (Ledger, Balance Sheet, P&L), "
27
  "format it strictly as a Markdown table. Preserve all symbols and currency signs."
28
  )
29
 
30
+ # FIX: GLM-OCR uses a custom 'chat' method usually defined in its modeling script
31
+ # We will use the model's own chat functionality which handles the image processing internally
32
+ try:
33
+ # Most GLM vision models use this specific internal method for inference
34
+ with torch.no_grad():
35
+ response, _ = model.chat(
36
+ tokenizer,
37
+ query=prompt,
38
+ images=[input_image],
39
+ history=[]
40
+ )
41
+ return response
42
+ except Exception as e:
43
+ return f"Extraction Error: {str(e)}\n\nTry ensuring the image is clear and not too large."
44
+
45
+ # Gradio Interface
46
+ with gr.Blocks() as demo:
47
  gr.Markdown("# 📊 Professional Accountancy Table Extractor")
48
+ gr.Markdown("Extracts complex ledgers and financial tables into Markdown format.")
49
 
50
  with gr.Row():
51
  with gr.Column():
52
+ image_input = gr.Image(type="pil", label="Upload Accountancy Question")
53
  submit_btn = gr.Button("Extract Text & Tables", variant="primary")
54
 
55
  with gr.Column():
 
57
 
58
  submit_btn.click(fn=process_accountancy_image, inputs=image_input, outputs=text_output)
59
 
60
+ # Launching with the fix for Gradio 6 theme warning
61
  demo.launch()