File size: 10,084 Bytes
625a47c
 
ff88345
625a47c
 
 
 
 
 
 
 
 
0724936
625a47c
 
 
0724936
625a47c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff88345
0724936
625a47c
 
 
0724936
625a47c
 
ff88345
0724936
625a47c
 
 
0724936
625a47c
 
 
 
 
ff88345
625a47c
0724936
625a47c
44c460e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import os
import io
import requests
import gradio as gr
from groq import Groq
from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
from deep_translator import GoogleTranslator
from PIL import Image, ImageDraw
import joblib
import time
from indic_transliteration import sanscript
from indic_transliteration.sanscript import transliterate
import openai
import torch
import warnings
from huggingface_hub import InferenceApi

# Detect if GPU is available
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Set up Groq API key
api_key = os.getenv("GROQ_API_KEY")
client = Groq(api_key=api_key)

# Set your Hugging Face API key
os.environ['HF_API_KEY']
api_key = os.getenv('HF_API_KEY')
if api_key is None:
    raise ValueError("Hugging Face API key is not set. Please set it in your environment.")

# Set OpenAI API key for text generation
openai.api_key = os.getenv('OPENAI_API_KEY')

headers = {"Authorization": f"Bearer {api_key}"}

# Load GPT-Neo for creative text generation
text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
text_generation_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name).to(device)
text_generation_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)

# Add padding token to GPT-Neo tokenizer if not present
if text_generation_tokenizer.pad_token is None:
    text_generation_tokenizer.add_special_tokens({'pad_token': '[PAD]'})

# Define the API URL for image generation
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"

# Load the trained sentiment analysis model and preprocessing steps
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    model = joblib.load('model.pkl')

# Function to query Hugging Face API
def query(payload, max_retries=5):
    for attempt in range(max_retries):
        response = requests.post(API_URL, headers=headers, json=payload)

        if response.status_code == 503:
            print(f"Model is still loading, retrying... Attempt {attempt + 1}/{max_retries}")
            estimated_time = min(response.json().get("estimated_time", 60), 60)
            time.sleep(estimated_time)
            continue

        if response.status_code != 200:
            print(f"Error: Received status code {response.status_code}")
            print(f"Response: {response.text}")
            return None

        return response.content

    print(f"Failed to generate image after {max_retries} attempts.")
    return None

# Function to generate image
def generate_image(prompt):
    image_bytes = query({"inputs": prompt})

    if image_bytes is None:
        error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
        d = ImageDraw.Draw(error_img)
        d.text((10, 150), "Image Generation Failed", fill=(255, 255, 255))
        return error_img

    try:
        image = Image.open(io.BytesIO(image_bytes))
        return image
    except Exception as e:
        print(f"Error: {e}")
        error_img = Image.new('RGB', (300, 300), color=(255, 0, 0))
        d = ImageDraw.Draw(error_img)
        d.text((10, 150), "Invalid Image Data", fill=(255, 255, 255))
        return error_img

# Tamil Audio to Tamil text
def transcribe_audio(audio_path):
    if audio_path is None:
        return "Please upload an audio file."
    try:
        with open(audio_path, "rb") as file:
            transcription = client.audio.transcriptions.create(
                file=(os.path.basename(audio_path), file.read()),
                model="whisper-large-v3",
                response_format="verbose_json",
            )
        return transcription.text
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Transliterate Romanized Tamil (in English letters) to Tamil script
def transliterate_to_tamil(romanized_text):
    try:
        # Step 1: Normalize the input for better transliteration results
        romanized_text = romanized_text.strip().lower()  # Remove extra spaces and convert to lowercase

        # Step 2: Handle common punctuation that might interrupt transliteration
        romanized_text = romanized_text.replace(".", " ").replace(",", " ").replace("?", " ").replace("!", " ")

        # Step 3: Apply ITRANS transliteration
        tamil_text = transliterate(romanized_text, sanscript.ITRANS, sanscript.TAMIL)

        return tamil_text
    except Exception as e:
        return f"An error occurred during transliteration: {str(e)}"

# Function to translate Tamil text to English using deep-translator
def translate_tamil_to_english(tamil_text):
    if not tamil_text:
        return "Please provide text to translate."
    try:
        translator = GoogleTranslator(source='ta', target='en')
        translated_text = translator.translate(tamil_text)

        # Predict sentiment from translated text
        sentiment_result = predict_sentiment(translated_text)

        return translated_text, sentiment_result, translated_text
    except Exception as e:
        return f"An error occurred during translation: {str(e)}", None, None

# Function to predict sentiment from English text
def predict_sentiment(english_text):
    if not english_text:
        return "No text provided for sentiment analysis."
    try:
        sentiment = model.predict([english_text])[0]
        return f"Sentiment: {sentiment}"
    except Exception as e:
        return f"An error occurred during sentiment prediction: {str(e)}"

# Generate creative text based on the translated English text
def generate_creative_text(english_text):
    if not english_text:
        return "Please provide text to generate creative content."

    try:
        inputs = text_generation_tokenizer(english_text, return_tensors="pt", padding=True, truncation=True).to(device)

        # Set parameters to control the output and avoid repetition
        generated_tokens = text_generation_model.generate(
            **inputs,
            max_length=60,
            num_return_sequences=1,
            no_repeat_ngram_size=3,
            temperature=0.7,
            top_p=0.9,
            do_sample=True,
            early_stopping=True
        )

        creative_text = text_generation_tokenizer.decode(generated_tokens[0], skip_special_tokens=True).strip()
        return creative_text

    except Exception as e:
        return f"An error occurred during text generation: {str(e)}"

# Create Gradio interface
with gr.Blocks() as demo:
    gr.Markdown(
        """
        <h1 style='color: #4CAF50;'>🎙️ Tamil Audio Transcription, Translation, Sentiment Prediction, Creative Text Generation, and Image Generation</h1>
        <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>
        """
    )

    # Input for audio file
    with gr.Row():
        audio_input = gr.Audio(type="filepath", label="Upload Audio File")
        transcribe_button = gr.Button("Transcribe Audio", elem_id="transcribe_btn")

    # Output field for Tamil transcription with ability to edit or type Romanized Tamil
    transcription_output = gr.Textbox(label="Transcription (Tamil or Romanized Tamil)", interactive=True ,elem_id="transcription_output")

    # Button for transliterating Romanized Tamil to Tamil script
    transliterate_button = gr.Button("Convert to Tamil Script", elem_id="transliterate_btn")

    # Input field for Tamil text and translate button
    with gr.Row():
        translate_button = gr.Button("Translate to English", elem_id="translate_btn")

    # Output field for English translation
    translation_output = gr.Textbox(label="Translation (English)", elem_id="translation_output")

    # Output field for sentiment prediction
    sentiment_output = gr.Textbox(label="Sentiment", elem_id="sentiment_output")

    # Button to generate creative text
    creative_text_button = gr.Button("Generate Creative Text", elem_id="creative_btn")

    # Output field for creative text
    creative_text_output = gr.Textbox(label="Creative Text", elem_id="creative_output")

    # Button to generate image
    generate_button = gr.Button("Generate Image", elem_id="generate_btn")

    # Output field for image file
    image_output = gr.Image(label="Generated Image")

    # Define variable to hold the translated English text
    translated_text_var = gr.State()

    # Define button click actions
    transcribe_button.click(
        fn=transcribe_audio,
        inputs=audio_input,
        outputs=transcription_output,
    )

    transliterate_button.click(
        fn=transliterate_to_tamil,
        inputs=transcription_output,
        outputs=transcription_output,
    )

    translate_button.click(
        fn=translate_tamil_to_english,
        inputs=transcription_output,
        outputs=[translation_output, sentiment_output, translated_text_var],
    )

    creative_text_button.click(
        fn=generate_creative_text,
        inputs=translated_text_var,
        outputs=creative_text_output,
    )

    generate_button.click(
        fn=generate_image,
        inputs=translated_text_var,
        outputs=image_output,
    )

# Apply custom CSS
demo.css = """
#transcribe_btn, #transliterate_btn, #translate_btn, #creative_btn, #generate_btn {
    background-color: #05907B; /* Change button color */
    color: white; /* Change text color */
}

#translation_output,#transcription_output, #sentiment_output, #creative_output {
    background-color: #f0f8ff; /* Change background color of text areas */
}

h1 {
    color: #4CAF50; /* Main heading color */
}

p {
    color: #000080; /* Plain text color */
}

/* Add thick border to entire app */
.gradio-container {
    border: 5px solid #05907B; /* Thick border color */
    padding: 10px; /* Padding inside the border */
    border-radius: 10px; /* Optional: add rounded corners */
}
"""

# Launch the interface and ensure code stops afterward
demo.launch(share=True)