Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Model setup
|
| 7 |
+
MODEL_PATH = "zai-org/GLM-OCR"
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 11 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 12 |
+
MODEL_PATH,
|
| 13 |
+
torch_dtype="auto",
|
| 14 |
+
device_map="auto",
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
+
)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Error loading model: {e}")
|
| 19 |
+
model = None
|
| 20 |
+
processor = None
|
| 21 |
+
|
| 22 |
+
def process_image(image):
|
| 23 |
+
if model is None or processor is None:
|
| 24 |
+
return "Error: Model not loaded. Please check your connection or try again later."
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# Prepare the input
|
| 28 |
+
messages = [{
|
| 29 |
+
"role": "user",
|
| 30 |
+
"content": [
|
| 31 |
+
{"type": "image", "image": image},
|
| 32 |
+
{"type": "text", "text": "Text Recognition:"}
|
| 33 |
+
],
|
| 34 |
+
}]
|
| 35 |
+
|
| 36 |
+
# Process and generate
|
| 37 |
+
inputs = processor.apply_chat_template(
|
| 38 |
+
messages, tokenize=True, add_generation_prompt=True,
|
| 39 |
+
return_dict=True, return_tensors="pt"
|
| 40 |
+
).to(model.device)
|
| 41 |
+
|
| 42 |
+
# Remove token_type_ids if present
|
| 43 |
+
inputs.pop("token_type_ids", None)
|
| 44 |
+
|
| 45 |
+
# Generate output
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
generated_ids = model.generate(**inputs, max_new_tokens=2048)
|
| 48 |
+
|
| 49 |
+
# Decode output
|
| 50 |
+
output_text = processor.decode(
|
| 51 |
+
generated_ids[0][inputs["input_ids"].shape[1]:],
|
| 52 |
+
skip_special_tokens=True,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return output_text
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"Error processing image: {str(e)}"
|
| 59 |
+
|
| 60 |
+
# Create Gradio interface
|
| 61 |
+
with gr.Blocks(title="GLM-OCR Demo") as demo:
|
| 62 |
+
gr.Markdown("# GLM-OCR: Multimodal OCR Model")
|
| 63 |
+
gr.Markdown("Upload an image to extract text using the GLM-OCR model.")
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
| 67 |
+
output_text = gr.Textbox(label="Extracted Text", lines=10)
|
| 68 |
+
|
| 69 |
+
submit_btn = gr.Button("Extract Text", variant="primary")
|
| 70 |
+
submit_btn.click(
|
| 71 |
+
fn=process_image,
|
| 72 |
+
inputs=input_image,
|
| 73 |
+
outputs=output_text
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
gr.Examples(
|
| 77 |
+
examples=[
|
| 78 |
+
["https://huggingface.co/spaces/twoimo/glm-ocr-demo/file=/tmp/tmpqr9q1h5g.png"],
|
| 79 |
+
],
|
| 80 |
+
inputs=input_image,
|
| 81 |
+
label="Example Images (if available)"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch()
|