|
|
|
|
|
|
|
from huggingface_hub import login |
|
from transformers import MarianTokenizer, MarianMTModel |
|
from PIL import Image |
|
import torch |
|
from torchvision import transforms |
|
from transformers import BlipProcessor, BlipForConditionalGeneration |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
login("your_huggingface_token_here") |
|
|
|
|
|
def translate_tamil_to_english(text): |
|
model_name = "Helsinki-NLP/opus-mt-ta-en" |
|
tokenizer = MarianTokenizer.from_pretrained(model_name) |
|
model = MarianMTModel.from_pretrained(model_name) |
|
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True) |
|
translated = model.generate(**inputs) |
|
english_text = tokenizer.decode(translated[0], skip_special_tokens=True) |
|
return english_text |
|
|
|
|
|
|
|
def get_sample_image(): |
|
img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.png" |
|
img = Image.open(requests.get(img_url, stream=True).raw).convert('RGB') |
|
return img |
|
|
|
|
|
def describe_image(image): |
|
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") |
|
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") |
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
out = model.generate(**inputs) |
|
caption = processor.decode(out[0], skip_special_tokens=True) |
|
return caption |
|
|
|
|
|
if __name__ == "__main__": |
|
import requests |
|
|
|
|
|
tamil_text = "ஒரு சிறிய வீடு கடற்கரைக்கு அருகிலுள்ளது" |
|
print("Tamil Input:", tamil_text) |
|
|
|
|
|
english_translation = translate_tamil_to_english(tamil_text) |
|
print("Translated English:", english_translation) |
|
|
|
|
|
image = get_sample_image() |
|
|
|
|
|
caption = describe_image(image) |
|
print("Image Caption:", caption) |
|
|
|
|
|
plt.imshow(image) |
|
plt.title(caption) |
|
plt.axis("off") |
|
plt.show() |
|
|