Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,24 @@
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
from PIL import Image
|
5 |
-
import numpy as np
|
6 |
|
7 |
-
# Load model and
|
8 |
-
|
9 |
-
model =
|
10 |
-
|
11 |
-
# Create OCR pipeline
|
12 |
-
ocr_pipeline = pipeline(
|
13 |
-
"image-to-text",
|
14 |
-
model=model,
|
15 |
-
tokenizer=tokenizer,
|
16 |
-
feature_extractor=tokenizer.init_feature_extractor()
|
17 |
-
)
|
18 |
|
19 |
def predict_handwriting(image):
|
20 |
"""
|
21 |
Function to process handwritten text image and return transcription
|
22 |
"""
|
23 |
try:
|
24 |
-
# Preprocess image
|
25 |
image = image.convert("RGB")
|
26 |
-
image
|
27 |
-
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
# Extract text from results
|
32 |
-
transcription = " ".join([word["value"] for word in result])
|
33 |
return transcription
|
34 |
|
35 |
except Exception as e:
|
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
+
# Load model and processor
|
7 |
+
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
8 |
+
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def predict_handwriting(image):
|
11 |
"""
|
12 |
Function to process handwritten text image and return transcription
|
13 |
"""
|
14 |
try:
|
15 |
+
# Preprocess the image
|
16 |
image = image.convert("RGB")
|
17 |
+
# Prepare image pixel values
|
18 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
19 |
+
# Generate text
|
20 |
+
generated_ids = model.generate(pixel_values)
|
21 |
+
transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
|
|
|
22 |
return transcription
|
23 |
|
24 |
except Exception as e:
|