Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,73 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
-
import torch
|
6 |
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
generator = pipeline("text-generation", model="gpt2")
|
15 |
-
|
16 |
-
# 3. Stable Diffusion image generator (needs token)
|
17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
use_auth_token=HF_TOKEN,
|
22 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
23 |
-
)
|
24 |
-
|
25 |
-
|
26 |
-
def
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
outputs=[
|
43 |
gr.Textbox(label="Translated English Text"),
|
44 |
-
gr.Textbox(label="Generated
|
45 |
gr.Image(label="Generated Image")
|
46 |
],
|
47 |
-
title="Tamil
|
48 |
-
description="
|
49 |
)
|
50 |
|
51 |
-
# Launch
|
52 |
-
|
|
|
1 |
import os
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
+
from huggingface_hub import login
|
5 |
+
from transformers import (
|
6 |
+
AutoTokenizer,
|
7 |
+
AutoModelForSeq2SeqLM,
|
8 |
+
GPT2LMHeadModel,
|
9 |
+
GPT2Tokenizer
|
10 |
+
)
|
11 |
from diffusers import StableDiffusionPipeline
|
|
|
12 |
|
13 |
+
# Authenticate with Hugging Face Token
|
14 |
+
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
15 |
+
if hf_token:
|
16 |
+
login(token=hf_token)
|
17 |
|
18 |
+
# Load Tamil to English Translation Model
|
19 |
+
trans_tokenizer = AutoTokenizer.from_pretrained("nandhinivaradharajan14/tam-eng-translator")
|
20 |
+
trans_model = AutoModelForSeq2SeqLM.from_pretrained("nandhinivaradharajan14/tam-eng-translator")
|
21 |
|
22 |
+
# Load GPT-2 for English Text Generation
|
23 |
+
gpt_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
24 |
+
gpt_model = GPT2LMHeadModel.from_pretrained("gpt2")
|
25 |
+
gpt_model.eval()
|
26 |
|
27 |
+
# Load Stable Diffusion
|
|
|
|
|
|
|
28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
29 |
+
sd_pipe = StableDiffusionPipeline.from_pretrained(
|
30 |
+
"runwayml/stable-diffusion-v1-5",
|
31 |
+
use_auth_token=hf_token,
|
|
|
32 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
33 |
+
).to(device)
|
34 |
+
|
35 |
+
# Main function
|
36 |
+
def tam_to_image_pipeline(tamil_text):
|
37 |
+
# 1. Tamil to English Translation
|
38 |
+
inputs = trans_tokenizer(tamil_text, return_tensors="pt")
|
39 |
+
translated = trans_model.generate(**inputs)
|
40 |
+
english_text = trans_tokenizer.decode(translated[0], skip_special_tokens=True)
|
41 |
+
|
42 |
+
# 2. Generate Descriptive Text using GPT-2
|
43 |
+
gpt_input = gpt_tokenizer.encode(english_text, return_tensors="pt")
|
44 |
+
with torch.no_grad():
|
45 |
+
gpt_output = gpt_model.generate(
|
46 |
+
gpt_input,
|
47 |
+
max_length=50,
|
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 |
+
# 3. Generate Image using Stable Diffusion
|
55 |
+
image = sd_pipe(generated_text).images[0]
|
56 |
+
|
57 |
+
return english_text, generated_text, image
|
58 |
+
|
59 |
+
# Gradio Interface
|
60 |
+
interface = gr.Interface(
|
61 |
+
fn=tam_to_image_pipeline,
|
62 |
+
inputs=gr.Textbox(label="Enter Tamil Text"),
|
63 |
outputs=[
|
64 |
gr.Textbox(label="Translated English Text"),
|
65 |
+
gr.Textbox(label="Generated Description"),
|
66 |
gr.Image(label="Generated Image")
|
67 |
],
|
68 |
+
title="Tamil to Image Generator",
|
69 |
+
description="π€ Tamil β English β GPT-2 Description β π¨ Stable Diffusion Image Generator"
|
70 |
)
|
71 |
|
72 |
+
# Launch app
|
73 |
+
interface.launch()
|