Amandeep01 commited on
Commit
3c11672
·
verified ·
1 Parent(s): cd68ed2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -46
app.py CHANGED
@@ -1,64 +1,58 @@
1
  import gradio as gr
 
 
2
  import easyocr
 
3
  import numpy as np
4
- from transformers import pipeline
5
- import urllib.parse
6
 
7
- # Initialize OCR
8
- reader = easyocr.Reader(['en', 'hi', 'mr', 'ne'])
 
 
 
 
 
 
 
9
 
10
  # Initialize translation model
11
- translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="eng_Latn", tgt_lang="hin_Deva")
 
 
12
 
13
- # Cloudinary Info
14
- CLOUD_NAME = "deoux7285" # No need for image name, this will be generated dynamically
15
- FONT = "Arial"
16
- SIZE = "40"
17
- COLOR = "black"
18
- POSITION = "south"
19
 
20
- # Function to generate Cloudinary overlay URL
21
- def generate_cloudinary_url(translated_text):
22
- encoded = urllib.parse.quote(translated_text) # Ensure text is URL encoded
23
- url = f"https://res.cloudinary.com/{deoux7285}/image/upload/l_text:{FONT}_{SIZE}:{encoded},co_{COLOR},g_{POSITION}/sample.jpg" # Image URL dynamically handled
24
- return url
25
 
26
- # Function to process image: OCR + Translation + Overlay
27
- def process_image(image, target_lang):
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
- if not results:
32
- return None, "No text detected"
 
 
 
 
33
 
34
- full_text = ""
35
- for _, text, _ in results:
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
- # Generate Cloudinary URL for translated text overlay
44
- cloud_url = generate_cloudinary_url(full_text.strip())
45
- return cloud_url, full_text.strip()
46
 
47
- # Inside Gradio interface
48
  iface = gr.Interface(
49
  fn=process_image,
50
- inputs=[
51
- gr.Image(type="pil", label="Upload Image"),
52
- gr.Textbox(label="Target Language (e.g., hin_Deva, mar_Deva, nep_Deva)")
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) # Launch Gradio interface
 
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)