Update app.py
Browse files
app.py
CHANGED
@@ -10,21 +10,22 @@ from transformers import (
|
|
10 |
)
|
11 |
from diffusers import StableDiffusionPipeline
|
12 |
|
13 |
-
# Authenticate
|
14 |
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
15 |
if hf_token:
|
16 |
login(token=hf_token)
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
#
|
23 |
gpt_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
24 |
gpt_model = GPT2LMHeadModel.from_pretrained("gpt2")
|
25 |
gpt_model.eval()
|
26 |
|
27 |
-
#
|
28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
29 |
sd_pipe = StableDiffusionPipeline.from_pretrained(
|
30 |
"runwayml/stable-diffusion-v1-5",
|
@@ -32,31 +33,31 @@ sd_pipe = StableDiffusionPipeline.from_pretrained(
|
|
32 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
33 |
).to(device)
|
34 |
|
35 |
-
#
|
36 |
def tam_to_image_pipeline(tamil_text):
|
37 |
-
#
|
38 |
-
inputs = trans_tokenizer(tamil_text, return_tensors="pt")
|
39 |
-
|
40 |
-
english_text = trans_tokenizer.decode(
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
with torch.no_grad():
|
45 |
gpt_output = gpt_model.generate(
|
46 |
-
|
47 |
-
max_length=
|
48 |
num_return_sequences=1,
|
49 |
no_repeat_ngram_size=2,
|
50 |
pad_token_id=gpt_tokenizer.eos_token_id
|
51 |
)
|
52 |
generated_text = gpt_tokenizer.decode(gpt_output[0], skip_special_tokens=True)
|
53 |
|
54 |
-
#
|
55 |
image = sd_pipe(generated_text).images[0]
|
56 |
|
57 |
return english_text, generated_text, image
|
58 |
|
59 |
-
# Gradio
|
60 |
interface = gr.Interface(
|
61 |
fn=tam_to_image_pipeline,
|
62 |
inputs=gr.Textbox(label="Enter Tamil Text"),
|
@@ -65,9 +66,8 @@ interface = gr.Interface(
|
|
65 |
gr.Textbox(label="Generated Description"),
|
66 |
gr.Image(label="Generated Image")
|
67 |
],
|
68 |
-
title="Tamil
|
69 |
-
description="
|
70 |
)
|
71 |
|
72 |
-
# Launch app
|
73 |
interface.launch()
|
|
|
10 |
)
|
11 |
from diffusers import StableDiffusionPipeline
|
12 |
|
13 |
+
# Authenticate via token
|
14 |
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
15 |
if hf_token:
|
16 |
login(token=hf_token)
|
17 |
|
18 |
+
# π Tamil β English Translation (Multilingual M2M100 model)
|
19 |
+
trans_checkpoint = "Hemanth-thunder/english-tamil-mt"
|
20 |
+
trans_tokenizer = AutoTokenizer.from_pretrained(trans_checkpoint)
|
21 |
+
trans_model = AutoModelForSeq2SeqLM.from_pretrained(trans_checkpoint)
|
22 |
|
23 |
+
# π§ GPT-2 English Text Generation
|
24 |
gpt_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
25 |
gpt_model = GPT2LMHeadModel.from_pretrained("gpt2")
|
26 |
gpt_model.eval()
|
27 |
|
28 |
+
# π¨ Stable Diffusion
|
29 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
30 |
sd_pipe = StableDiffusionPipeline.from_pretrained(
|
31 |
"runwayml/stable-diffusion-v1-5",
|
|
|
33 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
34 |
).to(device)
|
35 |
|
36 |
+
# Pipeline: Tamil β English β GPT-2 β Image
|
37 |
def tam_to_image_pipeline(tamil_text):
|
38 |
+
# Translate Tamil β English
|
39 |
+
inputs = trans_tokenizer(tamil_text, return_tensors="pt", truncation=True)
|
40 |
+
translated_ids = trans_model.generate(**inputs, max_length=128)
|
41 |
+
english_text = trans_tokenizer.decode(translated_ids[0], skip_special_tokens=True)
|
42 |
|
43 |
+
# Generate additional English Description via GPT-2
|
44 |
+
input_ids = gpt_tokenizer.encode(english_text, return_tensors="pt")
|
45 |
with torch.no_grad():
|
46 |
gpt_output = gpt_model.generate(
|
47 |
+
input_ids,
|
48 |
+
max_length=60,
|
49 |
num_return_sequences=1,
|
50 |
no_repeat_ngram_size=2,
|
51 |
pad_token_id=gpt_tokenizer.eos_token_id
|
52 |
)
|
53 |
generated_text = gpt_tokenizer.decode(gpt_output[0], skip_special_tokens=True)
|
54 |
|
55 |
+
# Generate image from description
|
56 |
image = sd_pipe(generated_text).images[0]
|
57 |
|
58 |
return english_text, generated_text, image
|
59 |
|
60 |
+
# Gradio UI
|
61 |
interface = gr.Interface(
|
62 |
fn=tam_to_image_pipeline,
|
63 |
inputs=gr.Textbox(label="Enter Tamil Text"),
|
|
|
66 |
gr.Textbox(label="Generated Description"),
|
67 |
gr.Image(label="Generated Image")
|
68 |
],
|
69 |
+
title="Tamil β Image Generator",
|
70 |
+
description="π Tamil to English (M2M100) β GPTβ2 β Image via Stable Diffusion"
|
71 |
)
|
72 |
|
|
|
73 |
interface.launch()
|