Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,46 @@
|
|
1 |
-
import
|
2 |
-
from transformers import pipeline
|
3 |
from diffusers import StableDiffusionPipeline
|
4 |
import torch
|
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 |
-
st.image(image, caption="🖼️ AI Generated Image", use_column_width=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
|
3 |
from diffusers import StableDiffusionPipeline
|
4 |
import torch
|
5 |
|
6 |
+
# 1. Tamil to English Translator
|
7 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ta-en")
|
8 |
+
|
9 |
+
# 2. English Text Generator (you can use GPT2 or any causal model)
|
10 |
+
generator = pipeline("text-generation", model="gpt2")
|
11 |
+
|
12 |
+
# 3. Image Generator using Stable Diffusion
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
image_pipe = StableDiffusionPipeline.from_pretrained(
|
15 |
+
"CompVis/stable-diffusion-v1-4",
|
16 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
17 |
+
)
|
18 |
+
image_pipe = image_pipe.to(device)
|
19 |
+
|
20 |
+
# 👇 Combined function
|
21 |
+
def generate_image_from_tamil(tamil_input):
|
22 |
+
# Step 1: Translate Tamil → English
|
23 |
+
translated = translator(tamil_input, max_length=100)[0]['translation_text']
|
24 |
+
|
25 |
+
# Step 2: Generate English sentence based on translated input
|
26 |
+
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
|
27 |
+
|
28 |
+
# Step 3: Generate Image based on English text
|
29 |
+
image = image_pipe(generated).images[0]
|
30 |
+
|
31 |
+
return translated, generated, image
|
32 |
+
|
33 |
+
# 🎨 Gradio UI
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=generate_image_from_tamil,
|
36 |
+
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
|
37 |
+
outputs=[
|
38 |
+
gr.Textbox(label="Translated English Text"),
|
39 |
+
gr.Textbox(label="Generated English Prompt"),
|
40 |
+
gr.Image(label="Generated Image")
|
41 |
+
],
|
42 |
+
title="Tamil to Image Generator 🌅",
|
43 |
+
description="Translates Tamil → English, generates story → creates image using Stable Diffusion."
|
44 |
+
)
|
45 |
+
|
46 |
+
iface.launch()
|
|