24Sureshkumar commited on
Commit
9e730f5
Β·
verified Β·
1 Parent(s): 44904b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -26
app.py CHANGED
@@ -4,48 +4,65 @@ from diffusers import StableDiffusionPipeline
4
  import torch
5
  import os
6
 
7
- # βœ… Load HF token safely from environment
8
  HF_TOKEN = os.getenv("HF_TOKEN", None)
9
 
10
- # βœ… Select device
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
13
- # βœ… 1. Tamil β†’ English Translator (multilingual model)
14
- translator = pipeline(
15
- "translation",
16
- model="Helsinki-NLP/opus-mt-mul-en",
17
- use_auth_token=HF_TOKEN
18
- )
 
 
 
 
19
 
20
- # βœ… 2. English Text Generator
21
- generator = pipeline("text-generation", model="gpt2")
 
 
 
 
22
 
23
- # βœ… 3. Image Generator using Stable Diffusion
24
- image_pipe = StableDiffusionPipeline.from_pretrained(
25
- "CompVis/stable-diffusion-v1-4",
26
- use_auth_token=HF_TOKEN,
27
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
28
- )
29
- image_pipe = image_pipe.to(device)
 
 
 
 
 
 
 
 
30
 
31
- # βœ… Function to connect all 3 stages
32
- def generate_image_from_tamil(tamil_input):
33
- translated = translator(tamil_input, max_length=100)[0]['translation_text']
34
- generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text'].strip()
35
- image = image_pipe(generated).images[0]
36
- return translated, generated, image
 
37
 
38
- # βœ… Gradio Interface
39
  iface = gr.Interface(
40
  fn=generate_image_from_tamil,
41
  inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
42
  outputs=[
43
  gr.Textbox(label="Translated English Text"),
44
- gr.Textbox(label="Generated English Prompt"),
45
  gr.Image(label="Generated Image")
46
  ],
47
  title="Tamil to Image Generator",
48
- description="πŸ”€ Translates Tamil β†’ English β†’ Generates prompt β†’ Creates an image using Stable Diffusion"
49
  )
50
 
51
  iface.launch()
 
4
  import torch
5
  import os
6
 
7
+ # 1. Use Hugging Face token securely
8
  HF_TOKEN = os.getenv("HF_TOKEN", None)
9
 
10
+ # 2. Set device
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
13
+ # 3. Load translator (Tamil β†’ English using multilingual model)
14
+ try:
15
+ translator = pipeline(
16
+ "translation",
17
+ model="Helsinki-NLP/opus-mt-mul-en",
18
+ use_auth_token=HF_TOKEN
19
+ )
20
+ except Exception as e:
21
+ translator = None
22
+ print(f"Error loading translator: {e}")
23
 
24
+ # 4. Load GPT2 for English text generation
25
+ try:
26
+ generator = pipeline("text-generation", model="gpt2")
27
+ except Exception as e:
28
+ generator = None
29
+ print(f"Error loading GPT2: {e}")
30
 
31
+ # 5. Load Stable Diffusion for image generation
32
+ try:
33
+ image_pipe = StableDiffusionPipeline.from_pretrained(
34
+ "CompVis/stable-diffusion-v1-4",
35
+ use_auth_token=HF_TOKEN,
36
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
37
+ ).to(device)
38
+ except Exception as e:
39
+ image_pipe = None
40
+ print(f"Error loading Stable Diffusion: {e}")
41
+
42
+ # 6. Full pipeline function with safe error handling
43
+ def generate_image_from_tamil(tamil_text):
44
+ if not translator or not generator or not image_pipe:
45
+ return "Model load error", "Model load error", None
46
 
47
+ try:
48
+ translated = translator(tamil_text, max_length=100)[0]['translation_text']
49
+ prompt = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
50
+ image = image_pipe(prompt).images[0]
51
+ return translated, prompt, image
52
+ except Exception as e:
53
+ return f"Translation/Image generation error: {str(e)}", "", None
54
 
55
+ # 7. Gradio UI
56
  iface = gr.Interface(
57
  fn=generate_image_from_tamil,
58
  inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
59
  outputs=[
60
  gr.Textbox(label="Translated English Text"),
61
+ gr.Textbox(label="Generated Prompt"),
62
  gr.Image(label="Generated Image")
63
  ],
64
  title="Tamil to Image Generator",
65
+ description="Translate Tamil ➜ Generate English Text ➜ Create Image"
66
  )
67
 
68
  iface.launch()