24Sureshkumar commited on
Commit
a558492
·
verified ·
1 Parent(s): aa4f94c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -1,59 +1,59 @@
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()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
+ import torch
4
 
5
+ # Set Hugging Face token if using private models or rate limits apply
6
+ # from huggingface_hub import login
7
+ # login(token="your_huggingface_token")
 
8
 
9
+ # Load Tamil to English translation model (use M2M100 for better support)
10
+ from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
11
+ translator_tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
12
+ translator_model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
13
+
14
+ # Load text generation model
15
  text_generator = pipeline("text-generation", model="gpt2")
16
 
17
+ # Load image generation model (e.g., SD 1.5)
18
+ from diffusers import StableDiffusionPipeline
19
+ import torch
 
 
 
 
20
 
21
+ image_pipe = StableDiffusionPipeline.from_pretrained(
22
+ "runwayml/stable-diffusion-v1-5",
23
+ torch_dtype=torch.float16
24
+ )
25
+ image_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
26
 
27
+ def process_input(tamil_text):
28
  try:
29
  # Step 1: Translate Tamil to English
30
+ translator_tokenizer.src_lang = "ta"
31
+ encoded = translator_tokenizer(tamil_text, return_tensors="pt")
32
+ generated_tokens = translator_model.generate(**encoded, forced_bos_token_id=translator_tokenizer.get_lang_id("en"))
33
+ english_text = translator_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
34
 
35
+ # Step 2: Generate additional text
36
+ generated_output = text_generator(english_text, max_length=50, num_return_sequences=1)[0]["generated_text"]
 
 
 
 
 
 
 
 
37
 
38
+ # Step 3: Generate image from the final English text
39
+ image = image_pipe(generated_output).images[0]
40
 
41
+ return english_text, generated_output, image
42
+ except Exception as e:
43
+ return str(e), "", None
44
 
45
+ # Gradio interface
46
+ demo = gr.Interface(
47
+ fn=process_input,
48
+ inputs=gr.Textbox(label="Enter Tamil Text"),
49
  outputs=[
50
  gr.Textbox(label="Translated English Text"),
51
+ gr.Textbox(label="Generated Description"),
52
+ gr.Image(label="Generated Image")
53
  ],
54
+ title="Tamil to English Text Image Generator",
55
+ description="This app takes Tamil input, translates it to English, generates detailed text, and creates an image."
56
  )
57
 
58
  if __name__ == "__main__":
59
+ demo.launch()