Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,47 @@
|
|
1 |
-
|
2 |
-
|
3 |
import gradio as gr
|
4 |
-
from
|
5 |
-
from transformers import (
|
6 |
-
AutoTokenizer,
|
7 |
-
AutoModelForSeq2SeqLM,
|
8 |
-
GPT2LMHeadModel,
|
9 |
-
GPT2Tokenizer
|
10 |
-
)
|
11 |
from diffusers import StableDiffusionPipeline
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
15 |
-
if hf_token:
|
16 |
-
login(token=hf_token)
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
trans_model = AutoModelForSeq2SeqLM.from_pretrained(trans_checkpoint)
|
22 |
|
23 |
-
#
|
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 |
-
|
31 |
-
|
|
|
32 |
use_auth_token=hf_token,
|
33 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
34 |
).to(device)
|
35 |
|
36 |
-
#
|
37 |
-
def
|
38 |
-
# Translate Tamil
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
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 |
-
|
62 |
-
fn=
|
63 |
-
inputs=gr.Textbox(label="Enter Tamil Text"),
|
64 |
outputs=[
|
65 |
gr.Textbox(label="Translated English Text"),
|
66 |
-
gr.Textbox(label="Generated Description"),
|
67 |
gr.Image(label="Generated Image")
|
68 |
],
|
69 |
-
title="Tamil
|
70 |
-
description="
|
71 |
)
|
72 |
|
73 |
-
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from diffusers import StableDiffusionPipeline
|
6 |
+
import torch
|
7 |
+
import os
|
8 |
|
9 |
+
# Get Hugging Face token from environment (in Hugging Face Spaces this is auto-populated from secrets)
|
10 |
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
|
|
11 |
|
12 |
+
# Load Translation Pipeline: Tamil β English using MarianMT
|
13 |
+
translation_model_name = "Helsinki-NLP/opus-mt-ta-en"
|
14 |
+
translator = pipeline("translation", model=translation_model_name)
|
|
|
15 |
|
16 |
+
# Load Stable Diffusion Pipeline: English β Image
|
|
|
|
|
|
|
|
|
|
|
17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
+
|
19 |
+
image_pipe = StableDiffusionPipeline.from_pretrained(
|
20 |
+
"CompVis/stable-diffusion-v1-4",
|
21 |
use_auth_token=hf_token,
|
22 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
23 |
).to(device)
|
24 |
|
25 |
+
# Core function
|
26 |
+
def translate_and_generate(tamil_text):
|
27 |
+
# Step 1: Translate Tamil to English
|
28 |
+
english_output = translator(tamil_text)[0]['translation_text']
|
29 |
+
|
30 |
+
# Step 2: Generate Image from English Text
|
31 |
+
image = image_pipe(english_output).images[0]
|
32 |
+
|
33 |
+
return english_output, image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# Gradio UI
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=translate_and_generate,
|
38 |
+
inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
|
39 |
outputs=[
|
40 |
gr.Textbox(label="Translated English Text"),
|
|
|
41 |
gr.Image(label="Generated Image")
|
42 |
],
|
43 |
+
title="Tamil-to-Image Generator πΈ",
|
44 |
+
description="Enter Tamil text. It will be translated to English and visualized using Stable Diffusion."
|
45 |
)
|
46 |
|
47 |
+
iface.launch()
|