Update app.py
Browse files
app.py
CHANGED
@@ -4,48 +4,65 @@ from diffusers import StableDiffusionPipeline
|
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
-
#
|
8 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
9 |
|
10 |
-
#
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
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 |
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
|
45 |
gr.Image(label="Generated Image")
|
46 |
],
|
47 |
title="Tamil to Image Generator",
|
48 |
-
description="
|
49 |
)
|
50 |
|
51 |
iface.launch()
|
|
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
+
# 1. Use Hugging Face token securely
|
8 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
9 |
|
10 |
+
# 2. Set device
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
+
# 3. Load translator (Tamil β English using multilingual model)
|
14 |
+
try:
|
15 |
+
translator = pipeline(
|
16 |
+
"translation",
|
17 |
+
model="Helsinki-NLP/opus-mt-mul-en",
|
18 |
+
use_auth_token=HF_TOKEN
|
19 |
+
)
|
20 |
+
except Exception as e:
|
21 |
+
translator = None
|
22 |
+
print(f"Error loading translator: {e}")
|
23 |
|
24 |
+
# 4. Load GPT2 for English text generation
|
25 |
+
try:
|
26 |
+
generator = pipeline("text-generation", model="gpt2")
|
27 |
+
except Exception as e:
|
28 |
+
generator = None
|
29 |
+
print(f"Error loading GPT2: {e}")
|
30 |
|
31 |
+
# 5. Load Stable Diffusion for image generation
|
32 |
+
try:
|
33 |
+
image_pipe = StableDiffusionPipeline.from_pretrained(
|
34 |
+
"CompVis/stable-diffusion-v1-4",
|
35 |
+
use_auth_token=HF_TOKEN,
|
36 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
37 |
+
).to(device)
|
38 |
+
except Exception as e:
|
39 |
+
image_pipe = None
|
40 |
+
print(f"Error loading Stable Diffusion: {e}")
|
41 |
+
|
42 |
+
# 6. Full pipeline function with safe error handling
|
43 |
+
def generate_image_from_tamil(tamil_text):
|
44 |
+
if not translator or not generator or not image_pipe:
|
45 |
+
return "Model load error", "Model load error", None
|
46 |
|
47 |
+
try:
|
48 |
+
translated = translator(tamil_text, max_length=100)[0]['translation_text']
|
49 |
+
prompt = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
|
50 |
+
image = image_pipe(prompt).images[0]
|
51 |
+
return translated, prompt, image
|
52 |
+
except Exception as e:
|
53 |
+
return f"Translation/Image generation error: {str(e)}", "", None
|
54 |
|
55 |
+
# 7. Gradio UI
|
56 |
iface = gr.Interface(
|
57 |
fn=generate_image_from_tamil,
|
58 |
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
|
59 |
outputs=[
|
60 |
gr.Textbox(label="Translated English Text"),
|
61 |
+
gr.Textbox(label="Generated Prompt"),
|
62 |
gr.Image(label="Generated Image")
|
63 |
],
|
64 |
title="Tamil to Image Generator",
|
65 |
+
description="Translate Tamil β Generate English Text β Create Image"
|
66 |
)
|
67 |
|
68 |
iface.launch()
|