Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,111 @@
|
|
1 |
# app.py
|
2 |
-
|
3 |
import gradio as gr
|
4 |
-
from transformers import pipeline,
|
5 |
from diffusers import StableDiffusionPipeline
|
6 |
import torch
|
7 |
-
import
|
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 |
-
return
|
34 |
|
35 |
# Gradio UI
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
gr.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
|
|
|
1 |
# app.py
|
|
|
2 |
import gradio as gr
|
3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
import torch
|
6 |
+
import re
|
7 |
|
8 |
+
# Initialize models (load once at startup)
|
9 |
+
# Translation pipeline (Tamil to English)
|
10 |
+
translator = pipeline(
|
11 |
+
"translation",
|
12 |
+
model="Helsinki-NLP/opus-mt-ta-en",
|
13 |
+
device=0 if torch.cuda.is_available() else -1
|
14 |
+
)
|
15 |
|
16 |
+
# Text generation pipeline (English creative content)
|
17 |
+
text_generator = pipeline(
|
18 |
+
"text-generation",
|
19 |
+
model="gpt2-medium",
|
20 |
+
device=0 if torch.cuda.is_available() else -1
|
21 |
+
)
|
22 |
|
23 |
+
# Image generation pipeline (English text to image)
|
24 |
+
if torch.cuda.is_available():
|
25 |
+
image_pipe = StableDiffusionPipeline.from_pretrained(
|
26 |
+
"runwayml/stable-diffusion-v1-5",
|
27 |
+
torch_dtype=torch.float16,
|
28 |
+
revision="fp16"
|
29 |
+
).to("cuda")
|
30 |
+
else:
|
31 |
+
image_pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
32 |
|
33 |
+
# Clean and format generated text
|
34 |
+
def clean_text(text):
|
35 |
+
"""Remove special characters and truncate to complete sentences"""
|
36 |
+
cleaned = re.split(r'(?<=[.!?])\s+', text)[0] # Take first complete sentence
|
37 |
+
return re.sub(r'[^a-zA-Z0-9,.!?\'"\- ]+', '', cleaned).strip()
|
38 |
|
39 |
+
def process_content(tamil_input, creativity_level):
|
40 |
+
"""Main processing pipeline: Translate → Generate Image → Create Text"""
|
41 |
+
outputs = {}
|
42 |
+
error = ""
|
43 |
|
44 |
+
try:
|
45 |
+
# Translate Tamil to English
|
46 |
+
translation = translator(tamil_input)
|
47 |
+
english_text = translation[0]['translation_text']
|
48 |
+
outputs["translation"] = english_text
|
49 |
+
|
50 |
+
# Generate image from translated text
|
51 |
+
image = image_pipe(
|
52 |
+
english_text,
|
53 |
+
guidance_scale=creativity_level
|
54 |
+
).images[0]
|
55 |
+
outputs["image"] = image
|
56 |
+
|
57 |
+
# Generate creative text from translation
|
58 |
+
creative_output = text_generator(
|
59 |
+
f"Create creative content about: {english_text}",
|
60 |
+
max_length=150,
|
61 |
+
temperature=creativity_level/10,
|
62 |
+
num_return_sequences=1
|
63 |
+
)
|
64 |
+
outputs["creative_text"] = clean_text(creative_output[0]['generated_text'])
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
error = f"⚠️ Error: {str(e)}"
|
68 |
|
69 |
+
return outputs, error
|
70 |
|
71 |
# Gradio UI
|
72 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
73 |
+
gr.Markdown("# 🌐 Tamil Creative Content Generator")
|
74 |
+
gr.Markdown("Enter Tamil text → Get English translation + AI-generated image + creative English text")
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
tamil_input = gr.Textbox(
|
79 |
+
label="தமிழ் உள்ளீடு (Tamil Input)",
|
80 |
+
placeholder="உங்கள் உரையை இங்கே உள்ளிடவும்...",
|
81 |
+
lines=3
|
82 |
+
)
|
83 |
+
creativity = gr.Slider(
|
84 |
+
label="Creativity Level",
|
85 |
+
minimum=1, maximum=10, value=7, step=1
|
86 |
+
)
|
87 |
+
submit_btn = gr.Button("Generate Content")
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
translation_box = gr.Textbox(label="English Translation")
|
91 |
+
creative_output = gr.Textbox(label="Creative English Content")
|
92 |
+
image_output = gr.Image(label="Generated Image")
|
93 |
+
error_output = gr.Textbox(label="System Messages", visible=True)
|
94 |
+
|
95 |
+
examples = gr.Examples(
|
96 |
+
examples=[
|
97 |
+
["ஒரு மலையின் மீது உள்ள அழகிய கோட்டை", 8],
|
98 |
+
["விண்மீன்கள் நிறைந்த இரவு வா��த்தில் பறக்கும் யானை", 9],
|
99 |
+
["தாவரங்கள் வளரும் புதிரான கோளம்", 10]
|
100 |
+
],
|
101 |
+
inputs=[tamil_input, creativity]
|
102 |
+
)
|
103 |
+
|
104 |
+
submit_btn.click(
|
105 |
+
fn=process_content,
|
106 |
+
inputs=[tamil_input, creativity],
|
107 |
+
outputs=[{"translation": translation_box, "creative_text": creative_output, "image": image_output}, error_output]
|
108 |
+
)
|
109 |
|
110 |
+
if __name__ == "__main__":
|
111 |
+
app.launch(share=True)
|