24Sureshkumar commited on
Commit
a68e5a8
Β·
verified Β·
1 Parent(s): bd44afb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -10,21 +10,22 @@ from transformers import (
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",
@@ -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
- # 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"),
@@ -65,9 +66,8 @@ interface = gr.Interface(
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()
 
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()