24Sureshkumar commited on
Commit
7b64e9f
·
verified ·
1 Parent(s): 684dc51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -29
app.py CHANGED
@@ -1,40 +1,148 @@
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- # Load models
5
- translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
6
- text_generation_pipeline = pipeline("text-generation", model="gpt2")
7
 
8
- # Simulated image generation (replace with Hugging Face Diffusers or similar if needed)
9
- def generate_image(prompt: str) -> str:
10
- # You can integrate actual image generation here
11
- return f"https://via.placeholder.com/512?text={prompt.replace(' ', '+')}"
 
 
 
 
12
 
13
- # Main function
14
- def multimodal_pipeline(tamil_text: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Step 1: Translate Tamil to English
16
- translated = translation_pipeline(tamil_text)[0]["translation_text"]
 
 
 
 
 
 
 
 
17
 
18
- # Step 2: Generate English text
19
- generated = text_generation_pipeline(translated, max_length=50, do_sample=True)[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Step 3: Generate Image (simulate)
22
- image_url = generate_image(generated)
 
 
 
23
 
24
- return translated, generated, image_url
 
 
 
 
 
 
 
 
 
25
 
26
- # Gradio Interface
27
- interface = gr.Interface(
28
- fn=multimodal_pipeline,
29
- inputs=gr.Textbox(label="Enter Tamil Text", placeholder="உங்கள் தமிழ் உரையை இங்கே உள்ளிடவும்"),
30
- outputs=[
31
- gr.Textbox(label="English Translation"),
32
- gr.Textbox(label="Generated Prompt"),
33
- gr.Image(label="Generated Image"),
34
- ],
35
- title="Tamil to Image Multimodal App",
36
- description="This app translates Tamil to English, generates a descriptive sentence, and creates an image based on it."
37
- )
38
 
39
  if __name__ == "__main__":
40
- interface.launch()
 
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")