24Sureshkumar commited on
Commit
ac06315
·
verified ·
1 Parent(s): 9e730f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -52
app.py CHANGED
@@ -1,68 +1,59 @@
 
1
  import gradio as gr
2
- from transformers import pipeline
3
- from diffusers import StableDiffusionPipeline
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()
 
 
1
+ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer, pipeline
2
  import gradio as gr
 
 
 
 
3
 
4
+ # Load the multilingual translation model (supports Tamil <-> English)
5
+ model_name = "facebook/m2m100_418M"
6
+ tokenizer = M2M100Tokenizer.from_pretrained(model_name)
7
+ model = M2M100ForConditionalGeneration.from_pretrained(model_name)
8
 
9
+ # Load text generation pipeline (you can replace "gpt2" with your preferred model)
10
+ text_generator = pipeline("text-generation", model="gpt2")
11
 
12
+ def translate_tamil_to_english(text):
13
+ if not text.strip():
14
+ return ""
15
+ tokenizer.src_lang = "ta"
16
+ encoded = tokenizer(text, return_tensors="pt")
17
+ generated_tokens = model.generate(**encoded, forced_bos_token_id=tokenizer.get_lang_id("en"))
18
+ return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
 
 
 
19
 
20
+ def process_text(tamil_text):
21
+ if not tamil_text.strip():
22
+ return "Please enter Tamil text.", None, ""
 
 
 
23
 
24
+ try:
25
+ # Step 1: Translate Tamil to English
26
+ translation = translate_tamil_to_english(tamil_text)
27
+ except Exception as e:
28
+ return f"Translation error: {str(e)}", None, ""
 
 
 
 
 
 
 
 
 
 
29
 
30
  try:
31
+ # Step 2: Generate English text based on translation
32
+ generated = text_generator(translation, max_length=50, num_return_sequences=1)
33
+ generated_text = generated[0]['generated_text']
 
34
  except Exception as e:
35
+ return translation, None, f"Text generation error: {str(e)}"
36
+
37
+ # Step 3: Placeholder for image generation
38
+ # Replace with your own image generation model/function
39
+ image_url = "https://via.placeholder.com/512.png?text=Generated+Image"
40
+
41
+ # Step 4: Description based on generated text (you can use an image captioning model here)
42
+ description = f"Generated description: {generated_text}"
43
+
44
+ return translation, image_url, description
45
 
 
46
  iface = gr.Interface(
47
+ fn=process_text,
48
+ inputs=gr.Textbox(lines=2, label="Enter Tamil text"),
49
  outputs=[
50
  gr.Textbox(label="Translated English Text"),
51
+ gr.Image(label="Generated Image"),
52
+ gr.Textbox(label="Image Description"),
53
  ],
54
+ title="Tamil to English Translation + Text & Image Generation",
55
+ description="Enter Tamil text, get English translation, generated text, image, and description."
56
  )
57
 
58
+ if __name__ == "__main__":
59
+ iface.launch()