24Sureshkumar commited on
Commit
eea6ac5
Β·
verified Β·
1 Parent(s): dc227c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -54
app.py CHANGED
@@ -1,73 +1,47 @@
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 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",
 
32
  use_auth_token=hf_token,
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"),
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 β†’ Image Generator",
70
- description="πŸ“˜ Tamil to English (M2M100) β†’ GPT‑2 β†’ Image via Stable Diffusion"
71
  )
72
 
73
- interface.launch()
 
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()