24Sureshkumar commited on
Commit
8583867
·
verified ·
1 Parent(s): f64cce4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -45
app.py CHANGED
@@ -1,60 +1,67 @@
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,
@@ -70,42 +77,54 @@ def process_content(tamil_input, creativity_level):
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 textGet 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)
 
1
  # app.py
2
  import gradio as gr
3
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
4
  from diffusers import StableDiffusionPipeline
5
  import torch
6
  import re
7
 
8
+ # Initialize models
9
+ def load_models():
10
+ # Load translation model (NLLB - best for Tamil)
11
+ translator = pipeline(
12
+ "translation",
13
+ model="facebook/nllb-200-distilled-600M",
14
+ src_lang="tam_Taml",
15
+ tgt_lang="eng_Latn",
16
+ device=0 if torch.cuda.is_available() else -1
17
+ )
18
+
19
+ # Text generation pipeline
20
+ text_generator = pipeline(
21
+ "text-generation",
22
+ model="gpt2-medium",
23
+ device=0 if torch.cuda.is_available() else -1
24
+ )
25
+
26
+ # Image generation pipeline
27
+ if torch.cuda.is_available():
28
+ image_pipe = StableDiffusionPipeline.from_pretrained(
29
+ "runwayml/stable-diffusion-v1-5",
30
+ torch_dtype=torch.float16,
31
+ revision="fp16"
32
+ ).to("cuda")
33
+ else:
34
+ image_pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
35
+
36
+ return translator, text_generator, image_pipe
37
 
38
+ # Load models at startup
39
+ translator, text_generator, image_pipe = load_models()
 
 
 
 
 
 
 
40
 
 
41
  def clean_text(text):
42
  """Remove special characters and truncate to complete sentences"""
43
+ cleaned = re.split(r'(?<=[.!?])\s+', text)[0]
44
  return re.sub(r'[^a-zA-Z0-9,.!?\'"\- ]+', '', cleaned).strip()
45
 
46
  def process_content(tamil_input, creativity_level):
 
47
  outputs = {}
48
  error = ""
49
 
50
  try:
51
  # Translate Tamil to English
52
+ translation_result = translator(tamil_input)
53
+ english_text = translation_result[0]['translation_text']
54
  outputs["translation"] = english_text
55
 
56
+ # Generate image
57
  image = image_pipe(
58
  english_text,
59
+ guidance_scale=creativity_level,
60
+ num_inference_steps=30
61
  ).images[0]
62
  outputs["image"] = image
63
 
64
+ # Generate creative text
65
  creative_output = text_generator(
66
  f"Create creative content about: {english_text}",
67
  max_length=150,
 
77
 
78
  # Gradio UI
79
  with gr.Blocks(theme=gr.themes.Soft()) as app:
80
+ gr.Markdown("# 🌐 தமிழ் உரை முதல் பட உருவாக்கம் (Tamil to Image Generator)")
81
+ gr.Markdown("தமிழில் உள்ளீடு செய்துஆங்கில மொழிபெயர்ப்பு + AI உருவம் + படைப்பு உரை பெறவும்")
82
 
83
  with gr.Row():
84
  with gr.Column():
85
  tamil_input = gr.Textbox(
86
+ label="தமிழ் உள்ளீடு",
87
  placeholder="உங்கள் உரையை இங்கே உள்ளிடவும்...",
88
  lines=3
89
  )
90
  creativity = gr.Slider(
91
+ label="படைப்பாற்றல் நிலை",
92
  minimum=1, maximum=10, value=7, step=1
93
  )
94
+ submit_btn = gr.Button("உருவாக்கு")
95
 
96
  with gr.Column():
97
+ translation_box = gr.Textbox(label="ஆங்கில மொழிபெயர்ப்பு")
98
+ creative_output = gr.Textbox(label="படைப்பு உரை", lines=3)
99
+ image_output = gr.Image(label="உருவாக்கப்பட்ட படம்")
100
+ error_output = gr.Textbox(label="பிழை செய்திகள்", visible=True)
101
 
102
  examples = gr.Examples(
103
  examples=[
104
+ ["கடலின் அடியில் மறைந்திருக்கும் பழைய நகரம்", 8],
105
+ ["பனி படர்ந்த குளிர்காலத்தில் வெப்பமான காபி குடிக்கும் பழங்குடி பெண்", 7],
106
+ ["வேறு கிரகத்தில் இருந்து வந்த அறிவார்ந்த இயந்திரங்கள்", 9]
107
  ],
108
  inputs=[tamil_input, creativity]
109
  )
110
 
111
+ # Clear inputs button
112
+ clear_btn = gr.Button("துடைத்து துவக்கவும்")
113
+
114
+ def clear_all():
115
+ return {"": ""}, "", "", None, ""
116
+
117
  submit_btn.click(
118
  fn=process_content,
119
  inputs=[tamil_input, creativity],
120
  outputs=[{"translation": translation_box, "creative_text": creative_output, "image": image_output}, error_output]
121
  )
122
+
123
+ clear_btn.click(
124
+ fn=clear_all,
125
+ inputs=[],
126
+ outputs=[tamil_input, translation_box, creative_output, image_output, error_output]
127
+ )
128
 
129
  if __name__ == "__main__":
130
+ app.launch()