|
import gradio as gr |
|
from transformers import MBartForConditionalGeneration, MBart50Tokenizer, AutoModelForCausalLM, AutoTokenizer, pipeline |
|
from diffusers import DiffusionPipeline |
|
import torch |
|
from PIL import Image |
|
|
|
|
|
model_name = "facebook/mbart-large-50-many-to-one-mmt" |
|
tokenizer = MBart50Tokenizer.from_pretrained(model_name) |
|
model = MBartForConditionalGeneration.from_pretrained(model_name) |
|
|
|
|
|
text_generation_model_name = "EleutherAI/gpt-neo-1.3B" |
|
text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name) |
|
text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name) |
|
text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer) |
|
|
|
|
|
pipe = DiffusionPipeline.from_pretrained( |
|
"stabilityai/stable-diffusion-xl-base-1.0", |
|
) |
|
|
|
|
|
if torch.cuda.is_available(): |
|
pipe.to("cuda") |
|
else: |
|
pipe.to("cpu") |
|
|
|
|
|
def generate_image_from_text(translated_text): |
|
try: |
|
print(f"Generating image from translated text: {translated_text}") |
|
|
|
image = pipe(prompt=translated_text).images[0] |
|
print("Image generation completed.") |
|
return image |
|
except Exception as e: |
|
print(f"Error during image generation: {e}") |
|
return None |
|
|
|
|
|
def generate_short_paragraph_from_text(translated_text): |
|
try: |
|
print(f"Generating a short paragraph from translated text: {translated_text}") |
|
paragraph = text_generator( |
|
translated_text, |
|
max_length=80, |
|
num_return_sequences=1, |
|
temperature=0.6, |
|
top_p=0.8, |
|
truncation=True |
|
)[0]['generated_text'] |
|
print(f"Paragraph generation completed: {paragraph}") |
|
return paragraph |
|
except Exception as e: |
|
print(f"Error during paragraph generation: {e}") |
|
return f"Error during paragraph generation: {e}" |
|
|
|
|
|
def translate_generate_paragraph_and_image(tamil_text): |
|
|
|
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 |
|
|
|
|
|
paragraph = generate_short_paragraph_from_text(translated_text) |
|
if "Error" in paragraph: |
|
return translated_text, paragraph, None |
|
|
|
|
|
image = generate_image_from_text(translated_text) |
|
|
|
return translated_text, paragraph, image |
|
|
|
|
|
def interface(tamil_text): |
|
translated_text, paragraph, image = translate_generate_paragraph_and_image(tamil_text) |
|
return translated_text, paragraph, image |
|
|
|
|
|
iface = gr.Interface( |
|
fn=interface, |
|
inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."), |
|
outputs=[ |
|
gr.Textbox(label="Translated Text"), |
|
gr.Textbox(label="Generated Paragraph"), |
|
gr.Image(type="pil", label="Generated Image") |
|
], |
|
title="Tamil Text Translation, Paragraph Generation, and Image Generation", |
|
description="Input Tamil text, and this tool will translate it, generate a short paragraph, and create an image based on the translated text." |
|
) |
|
|
|
|
|
iface.launch(share=True) |
|
|