Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,49 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
pixel_values = processor(image, return_tensors='pt').pixel_values.to(device)
|
| 51 |
-
generated_ids = trained_model.generate(pixel_values)
|
| 52 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 53 |
-
return generated_text
|
| 54 |
-
|
| 55 |
-
# Créer l'interface Gradio
|
| 56 |
-
iface = gr.Interface(
|
| 57 |
-
fn=ocr,
|
| 58 |
-
inputs=gr.Image(type="pil", label="Upload Image",height=300),
|
| 59 |
-
outputs=gr.Textbox(label="Extracted Text"),
|
| 60 |
-
title="OCR Text Extraction",
|
| 61 |
-
description="Upload an image to extract text using TrOCR model."
|
| 62 |
-
)
|
| 63 |
-
iface.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 6 |
+
from dataclasses import dataclass
|
| 7 |
+
|
| 8 |
+
# Configuration
|
| 9 |
+
@dataclass(frozen=True)
|
| 10 |
+
class ModelConfig:
|
| 11 |
+
MODEL_TYPE: str = 'large' # small|base|large
|
| 12 |
+
MODEL_NAME: str = f'microsoft/trocr-{MODEL_TYPE}-printed'
|
| 13 |
+
MODEL_PATH: str = 'ocr_model_large_2024-07-25_15_32.pt'
|
| 14 |
+
|
| 15 |
+
# Initialisation du device et du modèle
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
processor = TrOCRProcessor.from_pretrained(ModelConfig.MODEL_NAME)
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
trained_model = VisionEncoderDecoderModel.from_pretrained(ModelConfig.MODEL_NAME)
|
| 21 |
+
trained_model.load_state_dict(torch.load(ModelConfig.MODEL_PATH, map_location=device))
|
| 22 |
+
trained_model.to(device)
|
| 23 |
+
trained_model.eval()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Erreur lors du chargement du modèle : {e}")
|
| 26 |
+
exit(1)
|
| 27 |
+
|
| 28 |
+
# Fonction d'inférence
|
| 29 |
+
def ocr(image):
|
| 30 |
+
try:
|
| 31 |
+
image = Image.fromarray(np.array(image))
|
| 32 |
+
pixel_values = processor(image, return_tensors='pt').pixel_values.to(device)
|
| 33 |
+
generated_ids = trained_model.generate(pixel_values)
|
| 34 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 35 |
+
return generated_text
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Erreur lors du traitement de l'image : {e}"
|
| 38 |
+
|
| 39 |
+
# Interface Gradio
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=ocr,
|
| 42 |
+
inputs=gr.Image(type="pil", label="Télécharger une image", image_mode="fit"),
|
| 43 |
+
outputs=gr.Textbox(label="Texte extrait"),
|
| 44 |
+
title="Extraction de texte OCR",
|
| 45 |
+
description="Téléchargez une image pour extraire le texte en utilisant le modèle TrOCR.",
|
| 46 |
+
allow_flagging="never"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|