Update app.py
Browse files
app.py
CHANGED
@@ -4,21 +4,23 @@ from diffusers import StableDiffusionPipeline
|
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
raise ValueError("Set HF_TOKEN in env variables.")
|
10 |
|
|
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
-
# ✅
|
14 |
translator = pipeline(
|
15 |
"translation",
|
16 |
model="Helsinki-NLP/opus-mt-mul-en",
|
17 |
use_auth_token=HF_TOKEN
|
18 |
)
|
19 |
|
|
|
20 |
generator = pipeline("text-generation", model="gpt2")
|
21 |
|
|
|
22 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
23 |
"CompVis/stable-diffusion-v1-4",
|
24 |
use_auth_token=HF_TOKEN,
|
@@ -26,21 +28,24 @@ image_pipe = StableDiffusionPipeline.from_pretrained(
|
|
26 |
)
|
27 |
image_pipe = image_pipe.to(device)
|
28 |
|
|
|
29 |
def generate_image_from_tamil(tamil_input):
|
30 |
translated = translator(tamil_input, max_length=100)[0]['translation_text']
|
31 |
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text'].strip()
|
32 |
image = image_pipe(generated).images[0]
|
33 |
return translated, generated, image
|
34 |
|
|
|
35 |
iface = gr.Interface(
|
36 |
fn=generate_image_from_tamil,
|
37 |
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
|
38 |
-
outputs=[
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
)
|
45 |
|
46 |
iface.launch()
|
|
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
+
# ✅ Load HF token safely from environment
|
8 |
+
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
|
|
9 |
|
10 |
+
# ✅ Select device
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
+
# ✅ 1. Tamil → English Translator (multilingual model)
|
14 |
translator = pipeline(
|
15 |
"translation",
|
16 |
model="Helsinki-NLP/opus-mt-mul-en",
|
17 |
use_auth_token=HF_TOKEN
|
18 |
)
|
19 |
|
20 |
+
# ✅ 2. English Text Generator
|
21 |
generator = pipeline("text-generation", model="gpt2")
|
22 |
|
23 |
+
# ✅ 3. Image Generator using Stable Diffusion
|
24 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
25 |
"CompVis/stable-diffusion-v1-4",
|
26 |
use_auth_token=HF_TOKEN,
|
|
|
28 |
)
|
29 |
image_pipe = image_pipe.to(device)
|
30 |
|
31 |
+
# ✅ Function to connect all 3 stages
|
32 |
def generate_image_from_tamil(tamil_input):
|
33 |
translated = translator(tamil_input, max_length=100)[0]['translation_text']
|
34 |
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text'].strip()
|
35 |
image = image_pipe(generated).images[0]
|
36 |
return translated, generated, image
|
37 |
|
38 |
+
# ✅ Gradio Interface
|
39 |
iface = gr.Interface(
|
40 |
fn=generate_image_from_tamil,
|
41 |
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
|
42 |
+
outputs=[
|
43 |
+
gr.Textbox(label="Translated English Text"),
|
44 |
+
gr.Textbox(label="Generated English Prompt"),
|
45 |
+
gr.Image(label="Generated Image")
|
46 |
+
],
|
47 |
+
title="Tamil to Image Generator",
|
48 |
+
description="🔤 Translates Tamil → English → Generates prompt → Creates an image using Stable Diffusion"
|
49 |
)
|
50 |
|
51 |
iface.launch()
|