Update app.py
Browse files
app.py
CHANGED
@@ -4,23 +4,25 @@ from diffusers import StableDiffusionPipeline
|
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# )
|
12 |
-
|
13 |
|
|
|
14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
-
HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set your token in env variables
|
16 |
|
17 |
-
#
|
18 |
-
translator = pipeline(
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
generator = pipeline("text-generation", model="gpt2")
|
22 |
|
23 |
-
#
|
24 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
25 |
"CompVis/stable-diffusion-v1-4",
|
26 |
use_auth_token=HF_TOKEN,
|
@@ -28,12 +30,21 @@ image_pipe = StableDiffusionPipeline.from_pretrained(
|
|
28 |
)
|
29 |
image_pipe = image_pipe.to(device)
|
30 |
|
|
|
31 |
def generate_image_from_tamil(tamil_input):
|
|
|
32 |
translated = translator(tamil_input, max_length=100)[0]['translation_text']
|
|
|
|
|
33 |
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
|
|
|
|
|
|
|
34 |
image = image_pipe(generated).images[0]
|
|
|
35 |
return translated, generated, image
|
36 |
|
|
|
37 |
iface = gr.Interface(
|
38 |
fn=generate_image_from_tamil,
|
39 |
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
|
@@ -43,7 +54,9 @@ iface = gr.Interface(
|
|
43 |
gr.Image(label="Generated Image")
|
44 |
],
|
45 |
title="Tamil to Image Generator",
|
46 |
-
description="
|
|
|
47 |
)
|
48 |
|
|
|
49 |
iface.launch()
|
|
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
+
# 1. Check for HF_TOKEN
|
8 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
9 |
+
if HF_TOKEN is None:
|
10 |
+
raise ValueError("Please set the HF_TOKEN environment variable in Hugging Face repository secrets.")
|
|
|
|
|
11 |
|
12 |
+
# 2. Set device
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
14 |
|
15 |
+
# 3. Load translator with token
|
16 |
+
translator = pipeline(
|
17 |
+
"translation",
|
18 |
+
model="Helsinki-NLP/opus-mt-ta-en",
|
19 |
+
use_auth_token=HF_TOKEN
|
20 |
+
)
|
21 |
|
22 |
+
# 4. Load text generator (GPT-2) — public, no token needed
|
23 |
generator = pipeline("text-generation", model="gpt2")
|
24 |
|
25 |
+
# 5. Load image generator (Stable Diffusion) with token
|
26 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
27 |
"CompVis/stable-diffusion-v1-4",
|
28 |
use_auth_token=HF_TOKEN,
|
|
|
30 |
)
|
31 |
image_pipe = image_pipe.to(device)
|
32 |
|
33 |
+
# 6. Main function
|
34 |
def generate_image_from_tamil(tamil_input):
|
35 |
+
# Translate Tamil to English
|
36 |
translated = translator(tamil_input, max_length=100)[0]['translation_text']
|
37 |
+
|
38 |
+
# Generate a prompt using GPT-2
|
39 |
generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
|
40 |
+
generated = generated.strip()
|
41 |
+
|
42 |
+
# Generate image using Stable Diffusion
|
43 |
image = image_pipe(generated).images[0]
|
44 |
+
|
45 |
return translated, generated, image
|
46 |
|
47 |
+
# 7. Gradio Interface
|
48 |
iface = gr.Interface(
|
49 |
fn=generate_image_from_tamil,
|
50 |
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
|
|
|
54 |
gr.Image(label="Generated Image")
|
55 |
],
|
56 |
title="Tamil to Image Generator",
|
57 |
+
description="This app translates Tamil text to English, generates creative English prompts, and visualizes them using Stable Diffusion.",
|
58 |
+
allow_flagging="never"
|
59 |
)
|
60 |
|
61 |
+
# 8. Launch app
|
62 |
iface.launch()
|