Nitin00043 commited on
Commit
9b26701
·
verified ·
1 Parent(s): b1ea2cd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ # Load model and tokenizer
8
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/trocr-base-handwritten")
9
+ model = AutoModelForTokenClassification.from_pretrained("microsoft/trocr-base-handwritten")
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 = np.array(image)
27
+
28
+ # Run OCR
29
+ result = ocr_pipeline(image)
30
+
31
+ # Extract text from results
32
+ transcription = " ".join([word["value"] for word in result])
33
+ return transcription
34
+
35
+ except Exception as e:
36
+ return f"Error processing image: {str(e)}"
37
+
38
+ # Create Gradio interface
39
+ demo = gr.Interface(
40
+ fn=predict_handwriting,
41
+ inputs=gr.Image(type="pil", label="Upload Handwritten Text Image"),
42
+ outputs=gr.Textbox(label="Transcription"),
43
+ title="Handwritten Text to Text Converter",
44
+ description="Upload a handwritten text image and get the transcribed text. Best results with clear, high-contrast images."
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ demo.launch()