File size: 2,131 Bytes
7b64e9f e97aebb 0267d3c 7b64e9f 0267d3c 7b64e9f e97aebb 87e851b 0267d3c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import os
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer
from diffusers import StableDiffusionPipeline
import torch
# Set your Hugging Face API token here if needed (for private models)
HF_API_TOKEN = os.getenv("HF_API_TOKEN", None)
# Translation model name
translation_model_name = "Helsinki-NLP/opus-mt-tc-big-en-ta"
# Load translation tokenizer and model (make sure sentencepiece is installed)
translation_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
translation_model = MarianMTModel.from_pretrained(translation_model_name)
# Load stable diffusion pipeline for image generation
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16,
revision="fp16",
use_auth_token=HF_API_TOKEN,
)
pipe = pipe.to("cuda") if torch.cuda.is_available() else pipe.to("cpu")
def translate_tamil_to_english(tamil_text):
# Tokenize and translate
inputs = translation_tokenizer(tamil_text, return_tensors="pt", padding=True)
outputs = translation_model.generate(**inputs)
english_text = translation_tokenizer.decode(outputs[0], skip_special_tokens=True)
return english_text
def generate_image_from_text(text):
# Generate image from English text prompt
image = pipe(text).images[0]
return image
def translate_and_generate_image(tamil_text):
english_text = translate_tamil_to_english(tamil_text)
image = generate_image_from_text(english_text)
return english_text, image
with gr.Blocks() as app:
gr.Markdown("# Tamil to English Translation + Image Generation")
tamil_input = gr.Textbox(label="Enter Tamil Text", lines=3)
english_output = gr.Textbox(label="Translated English Text")
generated_image = gr.Image(label="Generated Image")
translate_btn = gr.Button("Translate and Generate Image")
translate_btn.click(
fn=translate_and_generate_image,
inputs=[tamil_input],
outputs=[english_output, generated_image]
)
if __name__ == "__main__":
app.launch(share=True) # share=True creates a public link (optional)
|