Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,50 +3,53 @@ from transformers import AutoModel, AutoTokenizer
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
#
|
| 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 |
-
|
| 16 |
).eval()
|
| 17 |
|
| 18 |
def process_accountancy_image(input_image):
|
| 19 |
if input_image is None:
|
| 20 |
return "Please upload an image."
|
| 21 |
|
| 22 |
-
#
|
| 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 |
-
#
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
gr.Markdown("# 📊 Professional Accountancy Table Extractor")
|
| 45 |
-
gr.Markdown("
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
with gr.Column():
|
| 49 |
-
image_input = gr.Image(type="pil", label="Upload
|
| 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()
|