Update app.py
Browse files
app.py
CHANGED
|
@@ -5,54 +5,87 @@ import easyocr
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from transformers import MarianMTModel, MarianTokenizer
|
|
|
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
| 10 |
cloudinary.config(
|
| 11 |
-
cloud_name="
|
| 12 |
-
api_key="
|
| 13 |
-
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
| 17 |
reader = easyocr.Reader(['en'])
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
model_name =
|
| 21 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 22 |
model = MarianMTModel.from_pretrained(model_name)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
translated_text = translate_text(extracted_text
|
| 35 |
|
| 36 |
-
# Upload
|
| 37 |
buffered = io.BytesIO()
|
| 38 |
image.save(buffered, format="JPEG")
|
| 39 |
buffered.seek(0)
|
| 40 |
upload_result = cloudinary.uploader.upload(buffered)
|
| 41 |
-
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
return overlay_url, translated_text
|
| 47 |
|
| 48 |
-
#
|
|
|
|
|
|
|
| 49 |
iface = gr.Interface(
|
| 50 |
fn=process_image,
|
| 51 |
-
inputs=[
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
iface.launch(share=True)
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from transformers import MarianMTModel, MarianTokenizer
|
| 8 |
+
import io
|
| 9 |
|
| 10 |
+
# ---------------------
|
| 11 |
+
# Cloudinary Config (replace with your actual values)
|
| 12 |
+
# ---------------------
|
| 13 |
cloudinary.config(
|
| 14 |
+
cloud_name="deoux7285", # <-- 🔁 Replace this
|
| 15 |
+
api_key="494111995449977", # <-- 🔁 Replace this
|
| 16 |
+
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# ---------------------
|
| 20 |
+
# Load OCR & Translation Models
|
| 21 |
+
# ---------------------
|
| 22 |
reader = easyocr.Reader(['en'])
|
| 23 |
|
| 24 |
+
# Using English to Hindi as example
|
| 25 |
+
model_name = "Helsinki-NLP/opus-mt-en-hi"
|
| 26 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 27 |
model = MarianMTModel.from_pretrained(model_name)
|
| 28 |
|
| 29 |
+
# ---------------------
|
| 30 |
+
# Function to Translate Text
|
| 31 |
+
# ---------------------
|
| 32 |
+
def translate_text(text):
|
| 33 |
+
batch = tokenizer.prepare_seq2seq_batch([text], return_tensors="pt")
|
| 34 |
+
translated = model.generate(**batch)
|
| 35 |
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 36 |
|
| 37 |
+
# ---------------------
|
| 38 |
+
# Main Function
|
| 39 |
+
# ---------------------
|
| 40 |
+
def process_image(image, font_size):
|
| 41 |
+
# Step 1: OCR
|
| 42 |
+
np_image = np.array(image)
|
| 43 |
+
results = reader.readtext(np_image)
|
| 44 |
+
if not results:
|
| 45 |
+
return "No text found", "No text extracted."
|
| 46 |
+
|
| 47 |
+
extracted_text = " ".join([res[1] for res in results])
|
| 48 |
|
| 49 |
+
# Step 2: Translate
|
| 50 |
+
translated_text = translate_text(extracted_text)
|
| 51 |
|
| 52 |
+
# Step 3: Upload to Cloudinary
|
| 53 |
buffered = io.BytesIO()
|
| 54 |
image.save(buffered, format="JPEG")
|
| 55 |
buffered.seek(0)
|
| 56 |
upload_result = cloudinary.uploader.upload(buffered)
|
| 57 |
+
public_id = upload_result["public_id"]
|
| 58 |
|
| 59 |
+
# Step 4: Create Cloudinary overlay URL
|
| 60 |
+
# NOTE: Overlay text needs to be URL-safe (spaces → %20)
|
| 61 |
+
safe_text = translated_text.replace(" ", "%20")
|
| 62 |
+
overlay_url = (
|
| 63 |
+
f"https://res.cloudinary.com/{cloudinary.config().cloud_name}/image/upload/"
|
| 64 |
+
f"l_text:arial_{font_size}:{safe_text},g_center,c_scale/"
|
| 65 |
+
f"{public_id}.jpg"
|
| 66 |
+
)
|
| 67 |
|
| 68 |
return overlay_url, translated_text
|
| 69 |
|
| 70 |
+
# ---------------------
|
| 71 |
+
# Gradio Interface
|
| 72 |
+
# ---------------------
|
| 73 |
iface = gr.Interface(
|
| 74 |
fn=process_image,
|
| 75 |
+
inputs=[
|
| 76 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 77 |
+
gr.Slider(20, 80, value=30, label="Overlay Font Size")
|
| 78 |
+
],
|
| 79 |
+
outputs=[
|
| 80 |
+
gr.Image(type="filepath", label="Image with Translated Text"),
|
| 81 |
+
gr.Textbox(label="Translated Text")
|
| 82 |
+
],
|
| 83 |
+
title="Image Translator with Text Overlay (Cloudinary)",
|
| 84 |
+
description="Upload an image, extract English text, translate it to Hindi, and overlay it on the image using Cloudinary."
|
| 85 |
)
|
| 86 |
|
| 87 |
+
# ---------------------
|
| 88 |
+
# Launch App
|
| 89 |
+
# ---------------------
|
| 90 |
if __name__ == "__main__":
|
| 91 |
iface.launch(share=True)
|