vmshankar86 commited on
Commit
48143db
·
verified ·
1 Parent(s): 129c908

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +276 -0
app.py ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import requests
4
+ import gradio as gr
5
+ from groq import Groq
6
+ from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
7
+ from deep_translator import GoogleTranslator
8
+ from PIL import Image, ImageDraw
9
+ import joblib
10
+ import time
11
+ from indic_transliteration import sanscript
12
+ from indic_transliteration.sanscript import transliterate
13
+ import openai
14
+ import torch
15
+
16
+ # Detect if GPU is available
17
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
18
+
19
+ # Set up Groq API key
20
+ api_key = os.getenv("GROQ_API_KEY") or ""
21
+ client = Groq(api_key=api_key)
22
+
23
+ # Set your Hugging Face API key
24
+ os.environ['HF_API_KEY'] = ''
25
+ api_key = os.getenv('HF_API_KEY')
26
+ if api_key is None:
27
+ raise ValueError("Hugging Face API key is not set. Please set it in your environment.")
28
+
29
+ # Set OpenAI API key for text generation
30
+ openai.api_key = os.getenv('OPENAI_API_KEY') or ''
31
+
32
+ headers = {"Authorization": f"Bearer {api_key}"}
33
+
34
+ # Load GPT-Neo for creative text generation
35
+ text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
36
+ text_generation_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name).to(device)
37
+ text_generation_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
38
+
39
+ # Add padding token to GPT-Neo tokenizer if not present
40
+ if text_generation_tokenizer.pad_token is None:
41
+ text_generation_tokenizer.add_special_tokens({'pad_token': '[PAD]'})
42
+
43
+ # Define the API URL for image generation
44
+ API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
45
+
46
+ # Load the trained sentiment analysis model and preprocessing steps
47
+ model = joblib.load('/content/model.pkl')
48
+
49
+ # Function to query Hugging Face API
50
+ def query(payload, max_retries=5):
51
+ for attempt in range(max_retries):
52
+ response = requests.post(API_URL, headers=headers, json=payload)
53
+
54
+ if response.status_code == 503:
55
+ print(f"Model is still loading, retrying... Attempt {attempt + 1}/{max_retries}")
56
+ estimated_time = min(response.json().get("estimated_time", 60), 60)
57
+ time.sleep(estimated_time)
58
+ continue
59
+
60
+ if response.status_code != 200:
61
+ print(f"Error: Received status code {response.status_code}")
62
+ print(f"Response: {response.text}")
63
+ return None
64
+
65
+ return response.content
66
+
67
+ print(f"Failed to generate image after {max_retries} attempts.")
68
+ return None
69
+
70
+ # Function to generate image
71
+ def generate_image(prompt):
72
+ image_bytes = query({"inputs": prompt})
73
+
74
+ if image_bytes is None:
75
+ error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
76
+ d = ImageDraw.Draw(error_img)
77
+ d.text((10, 150), "Image Generation Failed", fill=(255, 255, 255))
78
+ return error_img
79
+
80
+ try:
81
+ image = Image.open(io.BytesIO(image_bytes))
82
+ return image
83
+ except Exception as e:
84
+ print(f"Error: {e}")
85
+ error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
86
+ d = ImageDraw.Draw(error_img)
87
+ d.text((10, 150), "Invalid Image Data", fill=(255, 255, 255))
88
+ return error_img
89
+
90
+ # Tamil Audio to Tamil text
91
+ def transcribe_audio(audio_path):
92
+ if audio_path is None:
93
+ return "Please upload an audio file."
94
+ try:
95
+ with open(audio_path, "rb") as file:
96
+ transcription = client.audio.transcriptions.create(
97
+ file=(os.path.basename(audio_path), file.read()),
98
+ model="whisper-large-v3",
99
+ response_format="verbose_json",
100
+ )
101
+ return transcription.text
102
+ except Exception as e:
103
+ return f"An error occurred: {str(e)}"
104
+
105
+ # Transliterate Romanized Tamil (in English letters) to Tamil script
106
+ def transliterate_to_tamil(romanized_text):
107
+ try:
108
+ # Step 1: Normalize the input for better transliteration results
109
+ romanized_text = romanized_text.strip().lower() # Remove extra spaces and convert to lowercase
110
+
111
+ # Step 2: Handle common punctuation that might interrupt transliteration
112
+ romanized_text = romanized_text.replace(".", " ").replace(",", " ").replace("?", " ").replace("!", " ")
113
+
114
+ # Step 3: Apply ITRANS transliteration
115
+ tamil_text = transliterate(romanized_text, sanscript.ITRANS, sanscript.TAMIL)
116
+
117
+ return tamil_text
118
+ except Exception as e:
119
+ return f"An error occurred during transliteration: {str(e)}"
120
+
121
+ # Function to translate Tamil text to English using deep-translator
122
+ def translate_tamil_to_english(tamil_text):
123
+ if not tamil_text:
124
+ return "Please provide text to translate."
125
+ try:
126
+ translator = GoogleTranslator(source='ta', target='en')
127
+ translated_text = translator.translate(tamil_text)
128
+
129
+ # Predict sentiment from translated text
130
+ sentiment_result = predict_sentiment(translated_text)
131
+
132
+ return translated_text, sentiment_result, translated_text
133
+ except Exception as e:
134
+ return f"An error occurred during translation: {str(e)}", None, None
135
+
136
+ # Function to predict sentiment from English text
137
+ def predict_sentiment(english_text):
138
+ if not english_text:
139
+ return "No text provided for sentiment analysis."
140
+ try:
141
+ sentiment = model.predict([english_text])[0]
142
+ return f"Sentiment: {sentiment}"
143
+ except Exception as e:
144
+ return f"An error occurred during sentiment prediction: {str(e)}"
145
+
146
+ # Generate creative text based on the translated English text
147
+ def generate_creative_text(english_text):
148
+ if not english_text:
149
+ return "Please provide text to generate creative content."
150
+
151
+ try:
152
+ inputs = text_generation_tokenizer(english_text, return_tensors="pt", padding=True, truncation=True).to(device)
153
+
154
+ # Set parameters to control the output and avoid repetition
155
+ generated_tokens = text_generation_model.generate(
156
+ **inputs,
157
+ max_length=60,
158
+ num_return_sequences=1,
159
+ no_repeat_ngram_size=3,
160
+ temperature=0.7,
161
+ top_p=0.9,
162
+ do_sample=True,
163
+ early_stopping=True
164
+ )
165
+
166
+ creative_text = text_generation_tokenizer.decode(generated_tokens[0], skip_special_tokens=True).strip()
167
+ return creative_text
168
+
169
+ except Exception as e:
170
+ return f"An error occurred during text generation: {str(e)}"
171
+
172
+ # Create Gradio interface
173
+ with gr.Blocks() as demo:
174
+ gr.Markdown(
175
+ """
176
+ <h1 style='color: #4CAF50;'>🎙️ Tamil Audio Transcription, Transliteration, Translation, Sentiment Prediction, Creative Text Generation, and Image Generation</h1>
177
+ <p style='color: #000080;'>Upload an audio file to get the Tamil transcription, edit the transcription or type Romanized Tamil to convert it to Tamil script, translate it to English, predict the sentiment of the translated text, generate creative English text, and generate an image.</p>
178
+ """
179
+ )
180
+
181
+ # Input for audio file
182
+ with gr.Row():
183
+ audio_input = gr.Audio(type="filepath", label="Upload Audio File")
184
+ transcribe_button = gr.Button("Transcribe Audio", elem_id="transcribe_btn")
185
+
186
+ # Output field for Tamil transcription with ability to edit or type Romanized Tamil
187
+ transcription_output = gr.Textbox(label="Transcription (Tamil or Romanized Tamil)", interactive=True ,elem_id="transcription_output")
188
+
189
+ # Button for transliterating Romanized Tamil to Tamil script
190
+ transliterate_button = gr.Button("Convert to Tamil Script", elem_id="transliterate_btn")
191
+
192
+ # Input field for Tamil text and translate button
193
+ with gr.Row():
194
+ translate_button = gr.Button("Translate to English", elem_id="translate_btn")
195
+
196
+ # Output field for English translation
197
+ translation_output = gr.Textbox(label="Translation (English)", elem_id="translation_output")
198
+
199
+ # Output field for sentiment prediction
200
+ sentiment_output = gr.Textbox(label="Sentiment", elem_id="sentiment_output")
201
+
202
+ # Button to generate creative text
203
+ creative_text_button = gr.Button("Generate Creative Text", elem_id="creative_btn")
204
+
205
+ # Output field for creative text
206
+ creative_text_output = gr.Textbox(label="Creative Text", elem_id="creative_output")
207
+
208
+ # Button to generate image
209
+ generate_button = gr.Button("Generate Image", elem_id="generate_btn")
210
+
211
+ # Output field for image file
212
+ image_output = gr.Image(label="Generated Image")
213
+
214
+ # Define variable to hold the translated English text
215
+ translated_text_var = gr.State()
216
+
217
+ # Define button click actions
218
+ transcribe_button.click(
219
+ fn=transcribe_audio,
220
+ inputs=audio_input,
221
+ outputs=transcription_output,
222
+ )
223
+
224
+ transliterate_button.click(
225
+ fn=transliterate_to_tamil,
226
+ inputs=transcription_output,
227
+ outputs=transcription_output,
228
+ )
229
+
230
+ translate_button.click(
231
+ fn=translate_tamil_to_english,
232
+ inputs=transcription_output,
233
+ outputs=[translation_output, sentiment_output, translated_text_var],
234
+ )
235
+
236
+ creative_text_button.click(
237
+ fn=generate_creative_text,
238
+ inputs=translated_text_var,
239
+ outputs=creative_text_output,
240
+ )
241
+
242
+ generate_button.click(
243
+ fn=generate_image,
244
+ inputs=translated_text_var,
245
+ outputs=image_output,
246
+ )
247
+
248
+ # Apply custom CSS
249
+ demo.css = """
250
+ #transcribe_btn, #transliterate_btn, #translate_btn, #creative_btn, #generate_btn {
251
+ background-color: #05907B; /* Change button color */
252
+ color: white; /* Change text color */
253
+ }
254
+
255
+ #translation_output,#transcription_output, #sentiment_output, #creative_output {
256
+ background-color: #f0f8ff; /* Change background color of text areas */
257
+ }
258
+
259
+ h1 {
260
+ color: #4CAF50; /* Main heading color */
261
+ }
262
+
263
+ p {
264
+ color: #000080; /* Plain text color */
265
+ }
266
+
267
+ /* Add thick border to entire app */
268
+ .gradio-container {
269
+ border: 5px solid #05907B; /* Thick border color */
270
+ padding: 10px; /* Padding inside the border */
271
+ border-radius: 10px; /* Optional: add rounded corners */
272
+ }
273
+ """
274
+
275
+ # Launch the interface and ensure code stops afterward
276
+ demo.launch(share=True)