Update app.py
Browse files
app.py
CHANGED
@@ -1,148 +1,59 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
-
# Install the required libraries before running this script:
|
4 |
-
# pip install transformers gradio Pillow requests torch
|
5 |
-
|
6 |
import os
|
7 |
-
import requests
|
8 |
-
from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
|
9 |
-
from PIL import Image, ImageDraw
|
10 |
-
import io
|
11 |
import gradio as gr
|
|
|
|
|
12 |
import torch
|
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 |
-
|
61 |
-
|
62 |
-
|
63 |
-
if image_bytes is None:
|
64 |
-
# Return a blank image with error message
|
65 |
-
error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
|
66 |
-
d = ImageDraw.Draw(error_img)
|
67 |
-
d.text((10, 150), "Image Generation Failed", fill=(255, 255, 255))
|
68 |
-
return error_img
|
69 |
-
|
70 |
-
try:
|
71 |
-
image = Image.open(io.BytesIO(image_bytes))
|
72 |
-
return image
|
73 |
-
except Exception as e:
|
74 |
-
print(f"Error: {e}")
|
75 |
-
# Return an error image in case of failure
|
76 |
-
error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
|
77 |
-
d = ImageDraw.Draw(error_img)
|
78 |
-
d.text((10, 150), "Invalid Image Data", fill=(255, 255, 255))
|
79 |
-
return error_img
|
80 |
-
|
81 |
-
# Generate creative text based on the translated English text
|
82 |
-
def generate_creative_text(translated_text):
|
83 |
-
inputs = text_generation_tokenizer(translated_text, return_tensors="pt", padding=True, truncation=True).to(device)
|
84 |
-
generated_tokens = text_generation_model.generate(**inputs, max_length=100)
|
85 |
-
creative_text = text_generation_tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
86 |
-
return creative_text
|
87 |
-
|
88 |
-
# Function to handle the full workflow
|
89 |
-
def translate_generate_image_and_text(tamil_text):
|
90 |
-
# Step 1: Translate Tamil to English
|
91 |
-
translated_text = translate_text(tamil_text)
|
92 |
-
|
93 |
-
# Step 2: Generate an image from the translated text
|
94 |
-
image = generate_image(translated_text)
|
95 |
-
|
96 |
-
# Step 3: Generate creative text from the translated text
|
97 |
-
creative_text = generate_creative_text(translated_text)
|
98 |
-
|
99 |
-
return translated_text, creative_text, image
|
100 |
-
|
101 |
-
# CSS styling for the Gradio app
|
102 |
-
css = """
|
103 |
-
#transart-title {
|
104 |
-
font-size: 2.5em;
|
105 |
-
font-weight: bold;
|
106 |
-
color: #4CAF50;
|
107 |
-
text-align: center;
|
108 |
-
margin-bottom: 10px;
|
109 |
-
}
|
110 |
-
#transart-subtitle {
|
111 |
-
font-size: 1.25em;
|
112 |
-
text-align: center;
|
113 |
-
color: #555555;
|
114 |
-
margin-bottom: 20px;
|
115 |
-
}
|
116 |
-
body {
|
117 |
-
background-color: #f0f0f5;
|
118 |
-
}
|
119 |
-
.gradio-container {
|
120 |
-
font-family: 'Arial', sans-serif;
|
121 |
-
}
|
122 |
-
"""
|
123 |
-
|
124 |
-
# Title and subtitle HTML for Gradio markdown
|
125 |
-
title_markdown = """
|
126 |
-
# <div id="transart-title">TransArt</div>
|
127 |
-
### <div id="transart-subtitle">Tamil to English Translation, Creative Text & Image Generation</div>
|
128 |
-
"""
|
129 |
-
|
130 |
-
# Build Gradio interface
|
131 |
-
with gr.Blocks(css=css) as interface:
|
132 |
-
gr.Markdown(title_markdown)
|
133 |
-
with gr.Row():
|
134 |
-
with gr.Column():
|
135 |
-
tamil_input = gr.Textbox(label="Enter Tamil Text", placeholder="Type Tamil text here...", lines=3)
|
136 |
-
with gr.Column():
|
137 |
-
translated_output = gr.Textbox(label="Translated Text", interactive=False)
|
138 |
-
creative_text_output = gr.Textbox(label="Creative Generated Text", interactive=False)
|
139 |
-
generated_image_output = gr.Image(label="Generated Image")
|
140 |
-
|
141 |
-
gr.Button("Generate").click(
|
142 |
-
fn=translate_generate_image_and_text,
|
143 |
-
inputs=tamil_input,
|
144 |
-
outputs=[translated_output, creative_text_output, generated_image_output],
|
145 |
)
|
146 |
|
147 |
if __name__ == "__main__":
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
+
from transformers import MarianMTModel, MarianTokenizer
|
4 |
+
from diffusers import StableDiffusionPipeline
|
5 |
import torch
|
6 |
|
7 |
+
# Set your Hugging Face API token here if needed (for private models)
|
8 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN", None)
|
9 |
+
|
10 |
+
# Translation model name
|
11 |
+
translation_model_name = "Helsinki-NLP/opus-mt-tc-big-en-ta"
|
12 |
+
|
13 |
+
# Load translation tokenizer and model (make sure sentencepiece is installed)
|
14 |
+
translation_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
|
15 |
+
translation_model = MarianMTModel.from_pretrained(translation_model_name)
|
16 |
+
|
17 |
+
# Load stable diffusion pipeline for image generation
|
18 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
19 |
+
"runwayml/stable-diffusion-v1-5",
|
20 |
+
torch_dtype=torch.float16,
|
21 |
+
revision="fp16",
|
22 |
+
use_auth_token=HF_API_TOKEN,
|
23 |
+
)
|
24 |
+
pipe = pipe.to("cuda") if torch.cuda.is_available() else pipe.to("cpu")
|
25 |
+
|
26 |
+
def translate_tamil_to_english(tamil_text):
|
27 |
+
# Tokenize and translate
|
28 |
+
inputs = translation_tokenizer(tamil_text, return_tensors="pt", padding=True)
|
29 |
+
outputs = translation_model.generate(**inputs)
|
30 |
+
english_text = translation_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
+
return english_text
|
32 |
+
|
33 |
+
def generate_image_from_text(text):
|
34 |
+
# Generate image from English text prompt
|
35 |
+
image = pipe(text).images[0]
|
36 |
+
return image
|
37 |
+
|
38 |
+
def translate_and_generate_image(tamil_text):
|
39 |
+
english_text = translate_tamil_to_english(tamil_text)
|
40 |
+
image = generate_image_from_text(english_text)
|
41 |
+
return english_text, image
|
42 |
+
|
43 |
+
with gr.Blocks() as app:
|
44 |
+
gr.Markdown("# Tamil to English Translation + Image Generation")
|
45 |
+
|
46 |
+
tamil_input = gr.Textbox(label="Enter Tamil Text", lines=3)
|
47 |
+
english_output = gr.Textbox(label="Translated English Text")
|
48 |
+
generated_image = gr.Image(label="Generated Image")
|
49 |
+
|
50 |
+
translate_btn = gr.Button("Translate and Generate Image")
|
51 |
+
|
52 |
+
translate_btn.click(
|
53 |
+
fn=translate_and_generate_image,
|
54 |
+
inputs=[tamil_input],
|
55 |
+
outputs=[english_output, generated_image]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
)
|
57 |
|
58 |
if __name__ == "__main__":
|
59 |
+
app.launch(share=True) # share=True creates a public link (optional)
|