from transformers import MarianMTModel, AutoModelForSeq2SeqLM, AutoTokenizer, GPTNeoForCausalLM, GPT2Tokenizer import gradio as gr import requests import io from PIL import Image import os # Import os to access environment variables # Load MarianMT model and tokenizer for Tamil to English translation model_name = "Helsinki-NLP/opus-mt-mul-en" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSeq2SeqLM.from_pretrained(model_name) # Load GPT-Neo model and tokenizer gpt_neo_model_name = "EleutherAI/gpt-neo-125M" gpt_neo_model = GPTNeoForCausalLM.from_pretrained(gpt_neo_model_name) gpt_neo_tokenizer = GPT2Tokenizer.from_pretrained(gpt_neo_model_name) # Retrieve the API URL and headers for Flux.1 from environment variables API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev" headers = {"Authorization": f"Bearer {os.environ.get('HUGGINGFACE_API_KEY')}"} # Use the environment variable def generate_image_from_text(english_text): payload = {"inputs": english_text} response = requests.post(API_URL, headers=headers, json=payload) if response.status_code == 200: image_bytes = response.content image = Image.open(io.BytesIO(image_bytes)) return image else: return None # Handle error appropriately def translate_tamil_to_english(tamil_text): # Tokenize input and generate translation inputs = tokenizer(tamil_text, return_tensors="pt", padding=True) translated_tokens = model.generate(**inputs) translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True) return translated_text def generate_creative_text(english_text): input_ids = gpt_neo_tokenizer.encode(english_text, return_tensors='pt') output = gpt_neo_model.generate(input_ids, max_length=150, num_return_sequences=1) return gpt_neo_tokenizer.decode(output[0], skip_special_tokens=True) def process_input(tamil_text): # Step 1: Translate Tamil to English translated_text = translate_tamil_to_english(tamil_text) # Step 2: Generate Image from Translated English Text image = generate_image_from_text(translated_text) # Step 3: Generate Creative Text creative_text = generate_creative_text(translated_text) # Return results (translated text, image, and creative text) return translated_text, image, creative_text # Create a Gradio interface with input and output components interface = gr.Interface( fn=process_input, inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text..."), outputs=[gr.Textbox(label="Translated Text (English)"), gr.Image(label="Generated Image"), gr.Textbox(label="Creative Text")], title="Tamil to Creative Text & Image Generator", description="Enter Tamil text to translate, generate an image, and produce creative content." ) # Launch the Gradio app interface.launch(debug=True)