File size: 2,314 Bytes
ac06315
e97aebb
59921cd
ac06315
 
 
 
c652e39
ac06315
 
e5964e8
ac06315
 
 
 
 
 
 
e97aebb
ac06315
 
 
e97aebb
ac06315
 
 
 
 
e97aebb
9e730f5
ac06315
 
 
9e730f5
ac06315
 
 
 
 
 
 
 
 
 
e97aebb
 
ac06315
 
f77ae83
 
ac06315
 
f77ae83
ac06315
 
e97aebb
 
ac06315
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer, pipeline
import gradio as gr

# Load the multilingual translation model (supports Tamil <-> English)
model_name = "facebook/m2m100_418M"
tokenizer = M2M100Tokenizer.from_pretrained(model_name)
model = M2M100ForConditionalGeneration.from_pretrained(model_name)

# Load text generation pipeline (you can replace "gpt2" with your preferred model)
text_generator = pipeline("text-generation", model="gpt2")

def translate_tamil_to_english(text):
    if not text.strip():
        return ""
    tokenizer.src_lang = "ta"
    encoded = tokenizer(text, return_tensors="pt")
    generated_tokens = model.generate(**encoded, forced_bos_token_id=tokenizer.get_lang_id("en"))
    return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]

def process_text(tamil_text):
    if not tamil_text.strip():
        return "Please enter Tamil text.", None, ""

    try:
        # Step 1: Translate Tamil to English
        translation = translate_tamil_to_english(tamil_text)
    except Exception as e:
        return f"Translation error: {str(e)}", None, ""

    try:
        # Step 2: Generate English text based on translation
        generated = text_generator(translation, max_length=50, num_return_sequences=1)
        generated_text = generated[0]['generated_text']
    except Exception as e:
        return translation, None, f"Text generation error: {str(e)}"

    # Step 3: Placeholder for image generation
    # Replace with your own image generation model/function
    image_url = "https://via.placeholder.com/512.png?text=Generated+Image"

    # Step 4: Description based on generated text (you can use an image captioning model here)
    description = f"Generated description: {generated_text}"

    return translation, image_url, description

iface = gr.Interface(
    fn=process_text,
    inputs=gr.Textbox(lines=2, label="Enter Tamil text"),
    outputs=[
        gr.Textbox(label="Translated English Text"),
        gr.Image(label="Generated Image"),
        gr.Textbox(label="Image Description"),
    ],
    title="Tamil to English Translation + Text & Image Generation",
    description="Enter Tamil text, get English translation, generated text, image, and description."
)

if __name__ == "__main__":
    iface.launch()