Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,58 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import easyocr
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
from transformers import
|
| 5 |
-
import urllib.parse
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Initialize translation model
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
SIZE = "40"
|
| 17 |
-
COLOR = "black"
|
| 18 |
-
POSITION = "south"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return url
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
img_array = np.array(image) # Convert PIL image to numpy array for OCR
|
| 29 |
-
results = reader.readtext(img_array) # Run OCR on the image
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
if text.strip(): # Process non-empty text
|
| 37 |
-
try:
|
| 38 |
-
translation = translator(text, max_length=200)[0]["translation_text"]
|
| 39 |
-
except:
|
| 40 |
-
translation = "[Translation Error]"
|
| 41 |
-
full_text += translation + " "
|
| 42 |
|
| 43 |
-
|
| 44 |
-
cloud_url = generate_cloudinary_url(full_text.strip())
|
| 45 |
-
return cloud_url, full_text.strip()
|
| 46 |
|
| 47 |
-
#
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=process_image,
|
| 50 |
-
inputs=[
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
outputs=[
|
| 55 |
-
gr.HTML(label="Image with Cloudinary Text"), # ✅ changed from gr.Image to gr.HTML
|
| 56 |
-
gr.Textbox(label="Translated Text Output")
|
| 57 |
-
],
|
| 58 |
-
title="Advanced Image Translator",
|
| 59 |
-
description="Extract & translate text from an image (OCR + translation overlay)"
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
iface.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cloudinary
|
| 3 |
+
import cloudinary.uploader
|
| 4 |
import easyocr
|
| 5 |
+
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
+
from transformers import MarianMTModel, MarianTokenizer
|
|
|
|
| 8 |
|
| 9 |
+
# Cloudinary configuration
|
| 10 |
+
cloudinary.config(
|
| 11 |
+
cloud_name="your_cloud_name",
|
| 12 |
+
api_key="your_api_key",
|
| 13 |
+
api_secret="your_api_secret"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Initialize OCR reader
|
| 17 |
+
reader = easyocr.Reader(['en'])
|
| 18 |
|
| 19 |
# Initialize translation model
|
| 20 |
+
model_name = 'Helsinki-NLP/opus-mt-en-hin'
|
| 21 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 22 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 23 |
|
| 24 |
+
def translate_text(text, target_lang='hin'):
|
| 25 |
+
translated = model.generate(**tokenizer.prepare_seq2seq_batch(text, return_tensors="pt"))
|
| 26 |
+
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
def process_image(image, target_lang='hin'):
|
| 29 |
+
# OCR to extract text
|
| 30 |
+
result = reader.readtext(np.array(image))
|
| 31 |
+
extracted_text = " ".join([item[1] for item in result])
|
|
|
|
| 32 |
|
| 33 |
+
# Translate extracted text
|
| 34 |
+
translated_text = translate_text(extracted_text, target_lang)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Upload image to Cloudinary
|
| 37 |
+
buffered = io.BytesIO()
|
| 38 |
+
image.save(buffered, format="JPEG")
|
| 39 |
+
buffered.seek(0)
|
| 40 |
+
upload_result = cloudinary.uploader.upload(buffered)
|
| 41 |
+
cloud_url = upload_result['secure_url']
|
| 42 |
|
| 43 |
+
# Apply text overlay using Cloudinary
|
| 44 |
+
overlay_url = f"{cloud_url}?overlay_text={translated_text}&font_size=30&font_color=white&gravity=center"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
return overlay_url, translated_text
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Gradio interface
|
| 49 |
iface = gr.Interface(
|
| 50 |
fn=process_image,
|
| 51 |
+
inputs=[gr.Image(type="pil", label="Upload Image"), gr.Textbox(label="Target Language (e.g., hin)"), gr.Slider(minimum=10, maximum=100, label="Font Size")],
|
| 52 |
+
outputs=[gr.Image(label="Image with Overlay"), gr.Textbox(label="Translated Text")],
|
| 53 |
+
title="Image Translator with Overlay",
|
| 54 |
+
description="Upload an image, extract text using OCR, translate it, and overlay the translated text on the image."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
iface.launch(share=True)
|