Update app.py
Browse files
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 |
-
#
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
)
|
20 |
-
except Exception as e:
|
21 |
-
translator = None
|
22 |
-
print(f"Error loading translator: {e}")
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
except Exception as e:
|
28 |
-
generator = None
|
29 |
-
print(f"Error loading GPT2: {e}")
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
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 |
-
|
49 |
-
|
50 |
-
|
51 |
-
return translated, prompt, image
|
52 |
except Exception as e:
|
53 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
# 7. Gradio UI
|
56 |
iface = gr.Interface(
|
57 |
-
fn=
|
58 |
-
inputs=gr.Textbox(lines=2, label="Enter Tamil
|
59 |
outputs=[
|
60 |
gr.Textbox(label="Translated English Text"),
|
61 |
-
gr.
|
62 |
-
gr.
|
63 |
],
|
64 |
-
title="Tamil to Image
|
65 |
-
description="
|
66 |
)
|
67 |
|
68 |
-
|
|
|
|
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()
|