import os
import requests
from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
from PIL import Image, ImageDraw
import io
import gradio as gr
import torch
Detect if GPU is available
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
Load the MarianMT model and tokenizer for translation (Tamil to English)
os.environ['HF_API_KEY'] = 'api key' # Replace with your actual API key
api_key = os.getenv('HF_API_KEY')
if api_key is None:
raise ValueError("Hugging Face API key is not set. Please set it in your environment.")
headers = {"Authorization": f"Bearer {api_key}"}
Define the API URL for image generation (ensure this is a valid inference endpoint)
Query Hugging Face API to generate image with error handling
def query(payload):
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status() # Raises an HTTPError for bad responses
return response.content, response.headers # Return both content and headers
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
except Exception as e:
print(f"An error occurred: {e}")
return None, None # Return None for both if there's an error
Generate an image based on the translated text with error handling
def generate_image(prompt):
image_bytes, headers = query({"inputs": prompt}) # Capture headers as well
if image_bytes is None:
# Return a blank image with error message
error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
d = ImageDraw.Draw(error_img)
d.text((10, 150), "Image Generation Failed", fill=(255, 255, 255))
return error_img
try:
# Check if content type is valid for images using headers
if 'image' not in headers.get('Content-Type', ''):
raise ValueError("Response content is not an image.")
image = Image.open(io.BytesIO(image_bytes))
return image
except Exception as e:
print(f"Error: {e}")
# Return an error image in case of failure
error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
d = ImageDraw.Draw(error_img)
d.text((10, 200), "Invalid Image Data", fill=(255, 255, 255))
return error_img
Generate creative text based on the translated English text
def translate_generate_image_and_text(tamil_text):
# Step 1: Translate Tamil to English
translated_text = translate_text(tamil_text)
# Step 2: Generate an image from the translated text
image = generate_image(translated_text)
# Step 3: Generate creative text from the translated text
creative_text = generate_creative_text(translated_text)
return translated_text, creative_text, image
Custom HTML for title and subtitle (can be displayed in Markdown)
title_markdown = """
TransArt
Tamil to English Translation, Creative Text & Image Generation
"""
Gradio interface with customized layout and aesthetics
with gr.Blocks(css=css) as interface:
gr.Markdown(title_markdown) # Title and subtitle in Markdown
with gr.Row():
with gr.Column():
tamil_input = gr.Textbox(label="Enter Tamil Text", placeholder="Type Tamil text here...", lines=3) # Input for Tamil text
with gr.Column():
translated_output = gr.Textbox(label="Translated Text", interactive=False) # Output for translated text
creative_text_output = gr.Textbox(label="Creative Generated Text", interactive=False) # Output for creative text
generated_image_output = gr.Image(label="Generated Image") # Output for generated image