Spaces:
Runtime error
Runtime error
| from transformers import MBartForConditionalGeneration, MBart50Tokenizer | |
| import gradio as gr | |
| import requests | |
| import io | |
| from PIL import Image | |
| import os | |
| import time | |
| # Load the translation model and tokenizer | |
| model_name = "facebook/mbart-large-50-many-to-one-mmt" | |
| tokenizer = MBart50Tokenizer.from_pretrained(model_name) | |
| model = MBartForConditionalGeneration.from_pretrained(model_name) | |
| # Use the Hugging Face API key from environment variables for text-to-image model | |
| hf_api_key = os.getenv("full_token") | |
| if hf_api_key is None: | |
| raise ValueError("Hugging Face API key not found! Please set 'full_token' environment variable.") | |
| else: | |
| headers = {"Authorization": f"Bearer {hf_api_key}"} | |
| # Define the text-to-image model URL (using a stable diffusion model) | |
| API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4" | |
| # Function to generate an image using Hugging Face's text-to-image model | |
| def generate_image_from_text(translated_text): | |
| try: | |
| print(f"Generating image from translated text: {translated_text}") | |
| response = requests.post(API_URL, headers=headers, json={"inputs": translated_text}) | |
| # Check if the response is successful | |
| if response.status_code != 200: | |
| print(f"Error generating image: {response.text}") | |
| return None, f"Error generating image: {response.text}" | |
| # Read and return the generated image | |
| image_bytes = response.content | |
| image = Image.open(io.BytesIO(image_bytes)) | |
| print("Image generation completed.") | |
| return image, None | |
| except Exception as e: | |
| print(f"Error during image generation: {e}") | |
| return None, f"Error during image generation: {e}" | |
| # Define the function to translate Tamil text and generate an image | |
| def translate_and_generate_image(tamil_text): | |
| # Step 1: Translate Tamil text to English using mbart-large-50 | |
| try: | |
| print("Translating Tamil text to English...") | |
| tokenizer.src_lang = "ta_IN" | |
| inputs = tokenizer(tamil_text, return_tensors="pt") | |
| translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"]) | |
| translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0] | |
| print(f"Translation completed: {translated_text}") | |
| except Exception as e: | |
| return f"Error during translation: {e}", None | |
| # Step 2: Directly generate an image using the translated English text | |
| image, error_message = generate_image_from_text(translated_text) | |
| if error_message: | |
| return translated_text, error_message | |
| return translated_text, image | |
| # Gradio interface setup | |
| iface = gr.Interface( | |
| fn=translate_and_generate_image, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."), | |
| outputs=[gr.Textbox(label="Translated English Text"), | |
| gr.Image(label="Generated Image")], | |
| title="Tamil to English Translation and Image Creation", | |
| description="Translate Tamil text to English using Facebook's mbart-large-50 model and create an image using the translated text.", | |
| ) | |
| # Launch Gradio app without `share=True` | |
| iface.launch() | |