Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,42 @@ import torch
|
|
| 3 |
from transformers import AutoModel, AutoTokenizer
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
model_id = "zai-org/GLM-OCR"
|
| 7 |
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 9 |
model = AutoModel.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
trust_remote_code=True,
|
| 12 |
-
device_map="auto"
|
|
|
|
| 13 |
).eval()
|
| 14 |
|
| 15 |
def extract_all_tables(image):
|
| 16 |
if image is None: return "Please upload an image."
|
| 17 |
|
| 18 |
-
|
| 19 |
-
prompt = (
|
| 20 |
-
"Extract all text and all tables from this image. "
|
| 21 |
-
"Format every table in Markdown. Do not skip any data. "
|
| 22 |
-
"Provide the full content of the page."
|
| 23 |
-
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with torch.no_grad():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
response, _ = model.chat(
|
| 29 |
-
tokenizer,
|
| 30 |
-
query=prompt,
|
| 31 |
-
images=[image],
|
| 32 |
-
history=[],
|
| 33 |
max_new_tokens=4096,
|
|
|
|
| 34 |
repetition_penalty=1.1
|
| 35 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return response
|
| 37 |
|
| 38 |
-
# UI with
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("# π Accountancy Paper Extractor")
|
| 41 |
|
|
@@ -51,18 +54,17 @@ with gr.Blocks() as demo:
|
|
| 51 |
interactive=True,
|
| 52 |
elem_id="result-text"
|
| 53 |
)
|
| 54 |
-
# Standard Gradio version of a copy functionality
|
| 55 |
copy_btn = gr.Button("π Copy to Clipboard")
|
| 56 |
-
|
|
|
|
| 57 |
copy_btn.click(None, None, None, js="""
|
| 58 |
() => {
|
| 59 |
const text = document.querySelector('#result-text textarea').value;
|
| 60 |
navigator.clipboard.writeText(text);
|
| 61 |
-
alert('Copied
|
| 62 |
}
|
| 63 |
""")
|
| 64 |
|
| 65 |
btn.click(extract_all_tables, inputs=img_input, outputs=text_output)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
demo.launch(theme=gr.themes.Default())
|
|
|
|
| 3 |
from transformers import AutoModel, AutoTokenizer
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
model_id = "zai-org/GLM-OCR"
|
| 7 |
|
| 8 |
+
# Load with trust_remote_code=True to get the model's specific processing logic
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 10 |
model = AutoModel.from_pretrained(
|
| 11 |
model_id,
|
| 12 |
trust_remote_code=True,
|
| 13 |
+
device_map="auto",
|
| 14 |
+
torch_dtype=torch.float16
|
| 15 |
).eval()
|
| 16 |
|
| 17 |
def extract_all_tables(image):
|
| 18 |
if image is None: return "Please upload an image."
|
| 19 |
|
| 20 |
+
prompt = "Extract all text and tables from this image. Format tables in Markdown. Do not skip any data."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# 1. Manually build the input using the model's internal helper
|
| 23 |
+
# This replaces the 'model.chat' method that was failing
|
| 24 |
+
inputs = tokenizer.build_chat_input(prompt, images=[image], history=[])
|
| 25 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items() if torch.is_tensor(v)}
|
| 26 |
+
|
| 27 |
+
# 2. Use the standard generate method
|
| 28 |
with torch.no_grad():
|
| 29 |
+
outputs = model.generate(
|
| 30 |
+
**inputs,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
max_new_tokens=4096,
|
| 32 |
+
do_sample=False,
|
| 33 |
repetition_penalty=1.1
|
| 34 |
)
|
| 35 |
+
|
| 36 |
+
# 3. Decode only the new parts (the answer)
|
| 37 |
+
input_len = inputs['input_ids'].shape[1]
|
| 38 |
+
response = tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True)
|
| 39 |
return response
|
| 40 |
|
| 41 |
+
# UI with Copy Functionality
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("# π Accountancy Paper Extractor")
|
| 44 |
|
|
|
|
| 54 |
interactive=True,
|
| 55 |
elem_id="result-text"
|
| 56 |
)
|
|
|
|
| 57 |
copy_btn = gr.Button("π Copy to Clipboard")
|
| 58 |
+
|
| 59 |
+
# JavaScript for copying text
|
| 60 |
copy_btn.click(None, None, None, js="""
|
| 61 |
() => {
|
| 62 |
const text = document.querySelector('#result-text textarea').value;
|
| 63 |
navigator.clipboard.writeText(text);
|
| 64 |
+
alert('Copied!');
|
| 65 |
}
|
| 66 |
""")
|
| 67 |
|
| 68 |
btn.click(extract_all_tables, inputs=img_input, outputs=text_output)
|
| 69 |
|
| 70 |
+
demo.launch()
|
|
|