24Sureshkumar commited on
Commit
ff88345
·
verified ·
1 Parent(s): 8cd1cdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -55
app.py CHANGED
@@ -1,59 +1,140 @@
 
 
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install the required libraries
2
+ pip install transformers gradio Pillow requests
3
  import os
4
+ import requests
5
+ from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
6
+ from PIL import Image, ImageDraw
7
+ import io
8
  import gradio as gr
 
 
9
  import torch
10
 
11
+ # Detect if GPU is available
12
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
13
+
14
+ # Load the MarianMT model and tokenizer for translation (Tamil to English)
15
+ model_name = "Helsinki-NLP/opus-mt-mul-en"
16
+ translation_model = MarianMTModel.from_pretrained(model_name).to(device)
17
+ translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
18
+
19
+ # Load GPT-Neo for creative text generation
20
+ text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
21
+ text_generation_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name).to(device)
22
+ text_generation_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
23
+
24
+ # Add padding token to GPT-Neo tokenizer if not present
25
+ if text_generation_tokenizer.pad_token is None:
26
+ text_generation_tokenizer.add_special_tokens({'pad_token': '[PAD]'})
27
+
28
+ # Set your Hugging Face API key
29
+ os.environ['HF_API_KEY'] = 'Your_HF_TOKEN' # Replace with your actual API key
30
+ api_key = os.getenv('HF_API_KEY')
31
+ if api_key is None:
32
+ raise ValueError("Hugging Face API key is not set. Please set it in your environment.")
33
+
34
+ headers = {"Authorization": f"Bearer {api_key}"}
35
+
36
+ # Define the API URL for image generation (replace with actual model URL)
37
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell" # Replace with a valid image generation model
38
+
39
+ # Query Hugging Face API to generate image with error handling
40
+ def query(payload):
41
+ response = requests.post(API_URL, headers=headers, json=payload)
42
+ if response.status_code != 200:
43
+ print(f"Error: Received status code {response.status_code}")
44
+ print(f"Response: {response.text}")
45
+ return None
46
+ return response.content
47
+
48
+ # Translate Tamil text to English
49
+ def translate_text(tamil_text):
50
+ inputs = translation_tokenizer(tamil_text, return_tensors="pt", padding=True, truncation=True).to(device)
51
+ translated_tokens = translation_model.generate(**inputs)
52
+ translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
53
+ return translation
54
+
55
+ # Generate an image based on the translated text with error handling
56
+ def generate_image(prompt):
57
+ image_bytes = query({"inputs": prompt})
58
+
59
+ if image_bytes is None:
60
+ # Return a blank image with error message
61
+ error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
62
+ d = ImageDraw.Draw(error_img)
63
+ d.text((10, 150), "Image Generation Failed", fill=(255, 255, 255))
64
+ return error_img
65
+
66
+ try:
67
+ image = Image.open(io.BytesIO(image_bytes))
68
+ return image
69
+ except Exception as e:
70
+ print(f"Error: {e}")
71
+ # Return an error image in case of failure
72
+ error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
73
+ d = ImageDraw.Draw(error_img)
74
+ d.text((10, 150), "Invalid Image Data", fill=(255, 255, 255))
75
+ return error_img
76
+
77
+ # Generate creative text based on the translated English text
78
+ def generate_creative_text(translated_text):
79
+ inputs = text_generation_tokenizer(translated_text, return_tensors="pt", padding=True, truncation=True).to(device)
80
+ generated_tokens = text_generation_model.generate(**inputs, max_length=100)
81
+ creative_text = text_generation_tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
82
+ return creative_text
83
+
84
+ # Function to handle the full workflow
85
+ def translate_generate_image_and_text(tamil_text):
86
+ # Step 1: Translate Tamil to English
87
+ translated_text = translate_text(tamil_text)
88
+
89
+ # Step 2: Generate an image from the translated text
90
+ image = generate_image(translated_text)
91
+
92
+ # Step 3: Generate creative text from the translated text
93
+ creative_text = generate_creative_text(translated_text)
94
+
95
+ return translated_text, creative_text, image
96
+
97
+ # Create a visually appealing Gradio interface
98
+ css = """
99
+ #transart-title {
100
+ font-size: 2.5em;
101
+ font-weight: bold;
102
+ color: #4CAF50;
103
+ text-align: center;
104
+ margin-bottom: 10px;
105
+ }
106
+ #transart-subtitle {
107
+ font-size: 1.25em;
108
+ text-align: center;
109
+ color: #555555;
110
+ margin-bottom: 20px;
111
+ }
112
+ body {
113
+ background-color: #f0f0f5;
114
+ }
115
+ .gradio-container {
116
+ font-family: 'Arial', sans-serif;
117
+ }
118
+ """
119
+
120
+ # Custom HTML for title and subtitle (can be displayed in Markdown)
121
+ title_markdown = """
122
+ # <div id="transart-title">TransArt</div>
123
+ ### <div id="transart-subtitle">Tamil to English Translation, Creative Text & Image Generation</div>
124
+ """
125
+
126
+ # Gradio interface with customized layout and aesthetics
127
+ with gr.Blocks(css=css) as interface:
128
+ gr.Markdown(title_markdown) # Title and subtitle in Markdown
129
+ with gr.Row():
130
+ with gr.Column():
131
+ tamil_input = gr.Textbox(label="Enter Tamil Text", placeholder="Type Tamil text here...", lines=3) # Input for Tamil text
132
+ with gr.Column():
133
+ translated_output = gr.Textbox(label="Translated Text", interactive=False) # Output for translated text
134
+ creative_text_output = gr.Textbox(label="Creative Generated Text", interactive=False) # Output for creative text
135
+ generated_image_output = gr.Image(label="Generated Image") # Output for generated image
136
+
137
+ gr.Button("Generate").click(fn=translate_generate_image_and_text, inputs=tamil_input, outputs=[translated_output, creative_text_output, generated_image_output])
138
+
139
+ # Launch the Gradio app
140
+ interface.launch(debug=True, server_name="0.0.0.0")