24Sureshkumar commited on
Commit
59921cd
Β·
verified Β·
1 Parent(s): eec0855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -1,36 +1,32 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
3
  from diffusers import StableDiffusionPipeline
4
  import torch
 
 
 
 
5
 
6
  # 1. Tamil to English Translator
7
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
8
 
9
- # 2. English Text Generator (you can use GPT2 or any causal model)
10
  generator = pipeline("text-generation", model="gpt2")
11
 
12
- # 3. Image Generator using Stable Diffusion
13
- device = "cuda" if torch.cuda.is_available() else "cpu"
14
  image_pipe = StableDiffusionPipeline.from_pretrained(
15
  "CompVis/stable-diffusion-v1-4",
 
16
  torch_dtype=torch.float16 if device == "cuda" else torch.float32
17
  )
18
  image_pipe = image_pipe.to(device)
19
 
20
- # πŸ‘‡ Combined function
21
  def generate_image_from_tamil(tamil_input):
22
- # Step 1: Translate Tamil β†’ English
23
  translated = translator(tamil_input, max_length=100)[0]['translation_text']
24
-
25
- # Step 2: Generate English sentence based on translated input
26
  generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
27
-
28
- # Step 3: Generate Image based on English text
29
  image = image_pipe(generated).images[0]
30
-
31
  return translated, generated, image
32
 
33
- # 🎨 Gradio UI
34
  iface = gr.Interface(
35
  fn=generate_image_from_tamil,
36
  inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
@@ -39,8 +35,8 @@ iface = gr.Interface(
39
  gr.Textbox(label="Generated English Prompt"),
40
  gr.Image(label="Generated Image")
41
  ],
42
- title="Tamil to Image Generator πŸŒ…",
43
- description="Translates Tamil β†’ English, generates story β†’ creates image using Stable Diffusion."
44
  )
45
 
46
  iface.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
+ import os
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN") # Make sure to set your token in env variables
9
 
10
  # 1. Tamil to English Translator
11
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ta-en")
12
 
13
+ # 2. English Text Generator
14
  generator = pipeline("text-generation", model="gpt2")
15
 
16
+ # 3. Image Generator using Stable Diffusion with token
 
17
  image_pipe = StableDiffusionPipeline.from_pretrained(
18
  "CompVis/stable-diffusion-v1-4",
19
+ use_auth_token=HF_TOKEN,
20
  torch_dtype=torch.float16 if device == "cuda" else torch.float32
21
  )
22
  image_pipe = image_pipe.to(device)
23
 
 
24
  def generate_image_from_tamil(tamil_input):
 
25
  translated = translator(tamil_input, max_length=100)[0]['translation_text']
 
 
26
  generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
 
 
27
  image = image_pipe(generated).images[0]
 
28
  return translated, generated, image
29
 
 
30
  iface = gr.Interface(
31
  fn=generate_image_from_tamil,
32
  inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
 
35
  gr.Textbox(label="Generated English Prompt"),
36
  gr.Image(label="Generated Image")
37
  ],
38
+ title="Tamil to Image Generator",
39
+ description="Translates Tamil β†’ English, generates text β†’ creates image using Stable Diffusion."
40
  )
41
 
42
  iface.launch()