24Sureshkumar's picture
Update app.py
060cc15 verified
raw
history blame
1.95 kB
import gradio as gr
from transformers import pipeline
from diffusers import StableDiffusionPipeline
import torch
import os
# 1. Check for HF_TOKEN
HF_TOKEN = os.getenv("HF_TOKEN")
if HF_TOKEN is None:
raise ValueError("Please set the HF_TOKEN environment variable in Hugging Face repository secrets.")
# 2. Set device
device = "cuda" if torch.cuda.is_available() else "cpu"
# 3. Load translator with token
translator = pipeline(
"translation",
model="Helsinki-NLP/opus-mt-ta-en",
use_auth_token=HF_TOKEN
)
# 4. Load text generator (GPT-2) — public, no token needed
generator = pipeline("text-generation", model="gpt2")
# 5. Load image generator (Stable Diffusion) with token
image_pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
use_auth_token=HF_TOKEN,
torch_dtype=torch.float16 if device == "cuda" else torch.float32
)
image_pipe = image_pipe.to(device)
# 6. Main function
def generate_image_from_tamil(tamil_input):
# Translate Tamil to English
translated = translator(tamil_input, max_length=100)[0]['translation_text']
# Generate a prompt using GPT-2
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
generated = generated.strip()
# Generate image using Stable Diffusion
image = image_pipe(generated).images[0]
return translated, generated, image
# 7. Gradio Interface
iface = gr.Interface(
fn=generate_image_from_tamil,
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
outputs=[
gr.Textbox(label="Translated English Text"),
gr.Textbox(label="Generated English Prompt"),
gr.Image(label="Generated Image")
],
title="Tamil to Image Generator",
description="This app translates Tamil text to English, generates creative English prompts, and visualizes them using Stable Diffusion.",
allow_flagging="never"
)
# 8. Launch app
iface.launch()