gokilashree's picture
Update app.py
34e7460 verified
import gradio as gr
from transformers import MBartForConditionalGeneration, MBart50Tokenizer, AutoModelForCausalLM, AutoTokenizer, pipeline
from diffusers import DiffusionPipeline
import torch
from PIL import Image
# Load the Translation Model (MBART for Tamil to English Translation)
model_name = "facebook/mbart-large-50-many-to-one-mmt"
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
model = MBartForConditionalGeneration.from_pretrained(model_name)
# Load the Text Generation Model (for generating a short paragraph)
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)
# Load the Stable Diffusion XL Model for Image Generation in full precision (fp32)
pipe = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", # Remove torch_dtype and variant for CPU-friendly precision
)
# Check if GPU is available and use it for inference
if torch.cuda.is_available():
pipe.to("cuda") # Use GPU for faster inference
else:
pipe.to("cpu") # Use CPU if GPU is not available
# Function to generate image from text prompt using Stable Diffusion XL
def generate_image_from_text(translated_text):
try:
print(f"Generating image from translated text: {translated_text}")
# Generate the image using the pipeline
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
# Function to generate a short paragraph from the translated text
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, # Reduced to 80 tokens
num_return_sequences=1,
temperature=0.6,
top_p=0.8,
truncation=True # Added truncation to avoid long sequences
)[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}"
# Function to translate Tamil text, generate a short paragraph, and create an image
def translate_generate_paragraph_and_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: Generate a shorter paragraph based on the translated English text
paragraph = generate_short_paragraph_from_text(translated_text)
if "Error" in paragraph:
return translated_text, paragraph, None
# Step 3: Generate an image using the translated English text with the new model
image = generate_image_from_text(translated_text)
return translated_text, paragraph, image
# Define Gradio Interface
def interface(tamil_text):
translated_text, paragraph, image = translate_generate_paragraph_and_image(tamil_text)
return translated_text, paragraph, image
# Create Gradio Interface (with the image output)
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."
)
# Launch the Gradio app with share=True to create a shareable link
iface.launch(share=True)