24Sureshkumar commited on
Commit
058ddc5
·
verified ·
1 Parent(s): 19e7438

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -33
app.py CHANGED
@@ -1,47 +1,111 @@
1
  # app.py
2
-
3
  import gradio as gr
4
- from transformers import pipeline, MarianMTModel, MarianTokenizer
5
  from diffusers import StableDiffusionPipeline
6
  import torch
7
- import os
8
 
9
- # Get Hugging Face token from environment (in Hugging Face Spaces this is auto-populated from secrets)
10
- hf_token = os.getenv("HUGGINGFACE_TOKEN")
 
 
 
 
 
11
 
12
- # Load Translation Pipeline: Tamil → English using MarianMT
13
- translation_model_name = "Helsinki-NLP/opus-mt-ta-en"
14
- translator = pipeline("translation", model=translation_model_name)
 
 
 
15
 
16
- # Load Stable Diffusion Pipeline: English Image
17
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
18
 
19
- image_pipe = StableDiffusionPipeline.from_pretrained(
20
- "CompVis/stable-diffusion-v1-4",
21
- use_auth_token=hf_token,
22
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
23
- ).to(device)
24
 
25
- # Core function
26
- def translate_and_generate(tamil_text):
27
- # Step 1: Translate Tamil to English
28
- english_output = translator(tamil_text)[0]['translation_text']
29
 
30
- # Step 2: Generate Image from English Text
31
- image = image_pipe(english_output).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- return english_output, image
34
 
35
  # Gradio UI
36
- iface = gr.Interface(
37
- fn=translate_and_generate,
38
- inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
39
- outputs=[
40
- gr.Textbox(label="Translated English Text"),
41
- gr.Image(label="Generated Image")
42
- ],
43
- title="Tamil-to-Image Generator 🌸",
44
- description="Enter Tamil text. It will be translated to English and visualized using Stable Diffusion."
45
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- iface.launch()
 
 
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)