geetika14 commited on
Commit
8ce7418
·
verified ·
1 Parent(s): 0a130ee

Create Gradio_App.py

Browse files
Files changed (1) hide show
  1. Gradio_App.py +140 -0
Gradio_App.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")