Update app.py
Browse files
app.py
CHANGED
|
@@ -29,46 +29,35 @@ translate = pipeline("translation",model="marefa-nlp/marefa-mt-en-ar")
|
|
| 29 |
|
| 30 |
def caption_and_translate(img, min_len, max_len):
|
| 31 |
# Generate English caption
|
| 32 |
-
|
| 33 |
-
raw_image = Image.open(img).convert('RGB')
|
| 34 |
-
#prepares the image data for input to the Blip model
|
| 35 |
inputs_blip = processor_blip(raw_image, return_tensors="pt")
|
| 36 |
-
|
| 37 |
-
out_blip = model_blip.generate(**inputs_blip, min_length=
|
| 38 |
english_caption = processor_blip.decode(out_blip[0], skip_special_tokens=True)
|
| 39 |
|
| 40 |
-
|
| 41 |
# Translate caption from English to Arabic
|
| 42 |
arabic_caption = translate(english_caption)
|
| 43 |
arabic_caption = arabic_caption[0]['translation_text']
|
| 44 |
-
|
| 45 |
-
# The Arabic caption is formatted with right-to-left directionality.
|
| 46 |
translated_caption = f'<div dir="rtl">{arabic_caption}</div>'
|
| 47 |
|
| 48 |
-
|
| 49 |
-
# Return both caption and translated caption
|
| 50 |
return english_caption, translated_caption
|
| 51 |
|
| 52 |
|
| 53 |
# Gradio interface with multiple outputs
|
| 54 |
img_cap_en_ar = gr.Interface(
|
| 55 |
-
fn=caption_and_translate,
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
gr.Slider(label='Minimum Length', minimum=1, maximum=500, value=30),
|
| 60 |
-
gr.Slider(label='Maximum Length', minimum=1, maximum=500, value=100)],
|
| 61 |
-
|
| 62 |
outputs=[gr.Textbox(label='English Caption'),
|
| 63 |
gr.HTML(label='Arabic Caption')],
|
| 64 |
-
|
| 65 |
title='Image Captioning | وصف الصورة',
|
| 66 |
description="Upload an image to generate an English & Arabic caption | قم برفع صورة وأرسلها ليظهر لك وصف للصورة",
|
| 67 |
-
examples =[["
|
| 68 |
)
|
| 69 |
|
| 70 |
|
| 71 |
-
|
| 72 |
# Load the model
|
| 73 |
text_rec = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
|
| 74 |
|
|
@@ -126,7 +115,7 @@ def recognize_handwritten_text(image2):
|
|
| 126 |
# Gradio interface with image upload input and text output
|
| 127 |
handwritten_rec = gr.Interface(
|
| 128 |
fn=recognize_handwritten_text,
|
| 129 |
-
inputs=gr.Image(
|
| 130 |
outputs=[gr.Textbox(label='English Text'),
|
| 131 |
gr.HTML(label='Arabic Text')],
|
| 132 |
title="Handwritten Text Extraction | | إستخراج النص المكتوب بخط اليد وترجمتة",
|
|
|
|
| 29 |
|
| 30 |
def caption_and_translate(img, min_len, max_len):
|
| 31 |
# Generate English caption
|
| 32 |
+
raw_image = Image.open(img).convert('RGB')
|
|
|
|
|
|
|
| 33 |
inputs_blip = processor_blip(raw_image, return_tensors="pt")
|
| 34 |
+
|
| 35 |
+
out_blip = model_blip.generate(**inputs_blip, min_length=70, max_length=1000)
|
| 36 |
english_caption = processor_blip.decode(out_blip[0], skip_special_tokens=True)
|
| 37 |
|
|
|
|
| 38 |
# Translate caption from English to Arabic
|
| 39 |
arabic_caption = translate(english_caption)
|
| 40 |
arabic_caption = arabic_caption[0]['translation_text']
|
|
|
|
|
|
|
| 41 |
translated_caption = f'<div dir="rtl">{arabic_caption}</div>'
|
| 42 |
|
| 43 |
+
# Return both captions
|
|
|
|
| 44 |
return english_caption, translated_caption
|
| 45 |
|
| 46 |
|
| 47 |
# Gradio interface with multiple outputs
|
| 48 |
img_cap_en_ar = gr.Interface(
|
| 49 |
+
fn=caption_and_translate,
|
| 50 |
+
inputs=[gr.Image(type='filepath', label='Image')],
|
| 51 |
+
#gr.Slider(label='Minimum Length', minimum=1, maximum=500, value=30),
|
| 52 |
+
#gr.Slider(label='Maximum Length', minimum=1, maximum=500, value=100)],
|
|
|
|
|
|
|
|
|
|
| 53 |
outputs=[gr.Textbox(label='English Caption'),
|
| 54 |
gr.HTML(label='Arabic Caption')],
|
|
|
|
| 55 |
title='Image Captioning | وصف الصورة',
|
| 56 |
description="Upload an image to generate an English & Arabic caption | قم برفع صورة وأرسلها ليظهر لك وصف للصورة",
|
| 57 |
+
examples =[["image_2.png"]]
|
| 58 |
)
|
| 59 |
|
| 60 |
|
|
|
|
| 61 |
# Load the model
|
| 62 |
text_rec = pipeline("image-to-text", model="jinhybr/OCR-Donut-CORD")
|
| 63 |
|
|
|
|
| 115 |
# Gradio interface with image upload input and text output
|
| 116 |
handwritten_rec = gr.Interface(
|
| 117 |
fn=recognize_handwritten_text,
|
| 118 |
+
inputs=gr.Image(label="Upload Image"),
|
| 119 |
outputs=[gr.Textbox(label='English Text'),
|
| 120 |
gr.HTML(label='Arabic Text')],
|
| 121 |
title="Handwritten Text Extraction | | إستخراج النص المكتوب بخط اليد وترجمتة",
|