24Sureshkumar commited on
Commit
0267d3c
·
verified ·
1 Parent(s): 7d496b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -141
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
- # Detect if GPU is available
15
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
16
-
17
- # Load the MarianMT model and tokenizer for translation (Tamil to English)
18
- model_name = "Helsinki-NLP/opus-mt-mul-en"
19
- translation_model = MarianMTModel.from_pretrained(model_name).to(device)
20
- translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
21
-
22
- # Load GPT-Neo for creative text generation
23
- text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
24
- text_generation_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name).to(device)
25
- text_generation_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
26
-
27
- # Add padding token to GPT-Neo tokenizer if not present
28
- if text_generation_tokenizer.pad_token is None:
29
- text_generation_tokenizer.add_special_tokens({'pad_token': '[PAD]'})
30
-
31
- # Set your Hugging Face API key here or set the environment variable before running:
32
- # export HF_API_KEY='your_actual_api_key' # on Linux/Mac
33
- # setx HF_API_KEY "your_actual_api_key" # on Windows cmd (restart terminal after)
34
- api_key = os.getenv('HF_API_KEY')
35
- if api_key is None:
36
- raise ValueError("Hugging Face API key is not set. Please set it as environment variable HF_API_KEY.")
37
-
38
- headers = {"Authorization": f"Bearer {api_key}"}
39
-
40
- # Define the API URL for image generation (replace with actual model URL)
41
- API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell" # Replace if you want a different model
42
-
43
- # Query Hugging Face API to generate image with error handling
44
- def query(payload):
45
- response = requests.post(API_URL, headers=headers, json=payload)
46
- if response.status_code != 200:
47
- print(f"Error: Received status code {response.status_code}")
48
- print(f"Response: {response.text}")
49
- return None
50
- return response.content
51
-
52
- # Translate Tamil text to English
53
- def translate_text(tamil_text):
54
- inputs = translation_tokenizer(tamil_text, return_tensors="pt", padding=True, truncation=True).to(device)
55
- translated_tokens = translation_model.generate(**inputs)
56
- translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
57
- return translation
58
-
59
- # Generate an image based on the translated text with error handling
60
- def generate_image(prompt):
61
- image_bytes = query({"inputs": prompt})
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
- interface.launch(debug=True, server_name="0.0.0.0")
 
 
 
 
 
 
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)