File size: 11,075 Bytes
33d56ec
4133628
902cd01
7f95fc6
902cd01
 
 
7f95fc6
8c9cc75
4399d89
33d56ec
0cd73e9
902cd01
 
 
079cf4d
902cd01
 
 
 
 
85071eb
7f95fc6
8c9cc75
079cf4d
 
2a7702f
7f95fc6
079cf4d
902cd01
 
 
 
079cf4d
902cd01
 
 
 
 
 
 
 
 
 
 
 
079cf4d
7b7724d
902cd01
0cd73e9
7b7724d
4c0911f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b7724d
4c0911f
 
7b7724d
4c0911f
902cd01
079cf4d
902cd01
 
 
 
 
4c0911f
 
902cd01
4c0911f
 
 
 
 
 
 
7b7724d
4c0911f
 
 
 
7b7724d
4c0911f
 
 
 
 
902cd01
0cd73e9
902cd01
 
4c0911f
0cd73e9
 
902cd01
7304960
902cd01
 
079cf4d
 
902cd01
079cf4d
 
902cd01
 
079cf4d
 
902cd01
079cf4d
 
 
0cd73e9
 
 
 
 
 
 
902cd01
0cd73e9
7b7724d
902cd01
7304960
079cf4d
7f95fc6
079cf4d
 
 
 
7f95fc6
0cd73e9
079cf4d
7f95fc6
0cd73e9
 
079cf4d
0cd73e9
 
33d56ec
 
 
 
 
 
 
 
0cd73e9
079cf4d
0cd73e9
7b7724d
7f95fc6
33d56ec
 
 
 
 
0cd73e9
 
 
7b7724d
7304960
0cd73e9
 
33d56ec
0cd73e9
4399d89
33d56ec
85071eb
079cf4d
33d56ec
902cd01
 
 
7304960
85071eb
 
902cd01
4399d89
 
 
 
 
 
 
7f95fc6
33d56ec
902cd01
 
7304960
 
4c0911f
 
902cd01
 
 
 
079cf4d
4c0911f
 
902cd01
 
079cf4d
902cd01
7f95fc6
0cd73e9
7304960
85071eb
0cd73e9
 
 
7f95fc6
33d56ec
 
 
 
 
 
 
85071eb
7304960
aa5c3c5
 
 
 
 
 
 
7304960
aa5c3c5
7304960
 
aa5c3c5
 
 
 
 
 
 
7304960
 
aa5c3c5
7304960
 
 
 
 
 
 
 
 
 
 
 
33d56ec
7304960
 
 
 
0cd73e9
33d56ec
 
9a0f9e8
 
 
 
7304960
4c0911f
 
 
 
 
 
 
 
 
 
902cd01
 
7304960
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#app.py
import gradio as gr
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
from IndicTransToolkit.processor import IndicProcessor
import requests
from datetime import datetime
import tempfile
from gtts import gTTS
import os
import shutil


DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

# Load models
model_en_to_indic = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-en-indic-1B", trust_remote_code=True).to(DEVICE)
tokenizer_en_to_indic = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-en-indic-1B", trust_remote_code=True)
model_indic_to_en = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-indic-en-1B", trust_remote_code=True).to(DEVICE)
tokenizer_indic_to_en = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-indic-en-1B", trust_remote_code=True)

ip = IndicProcessor(inference=True)
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")

# --- Supabase settings ---
SUPABASE_URL = "https://gptmdbhzblfybdnohqnh.supabase.co"
SUPABASE_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImdwdG1kYmh6YmxmeWJkbm9ocW5oIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDc0NjY1NDgsImV4cCI6MjA2MzA0MjU0OH0.CfWArts6Kd_x7Wj0a_nAyGJfrFt8F7Wdy_MdYDj9e7U"

# --- Supabase utilities ---
def save_to_supabase(input_text, output_text, direction):
    if not input_text.strip() or not output_text.strip():
        return "Nothing to save."

    table = "translations" if direction == "en_to_ks" else "ks_to_en_translations"
    payload = {
        "timestamp": datetime.utcnow().isoformat(),
        "input_text": input_text,
        "output_text": output_text
    }
    headers = {
        "apikey": SUPABASE_API_KEY,
        "Authorization": f"Bearer {SUPABASE_API_KEY}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(f"{SUPABASE_URL}/rest/v1/{table}", json=payload, headers=headers)
        return "Saved successfully!" if response.status_code == 201 else "❌ Failed to save."
    except Exception as e:
        logging.error("Save error: %s", e)
        return "Save error."
    
# --- Save verified translation ---
def save_verified_translation(original_text, verified_text):
    if not original_text.strip() or not verified_text.strip():
        return "Nothing to save."

    payload = {
        "timestamp": datetime.utcnow().isoformat(),
        "original_translation": original_text,
        "verified_translation": verified_text
    }
    headers = {
        "apikey": SUPABASE_API_KEY,
        "Authorization": f"Bearer {SUPABASE_API_KEY}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(f"{SUPABASE_URL}/rest/v1/verified_translations", json=payload, headers=headers)
        return "Verified translation saved!" if response.status_code == 201 else "❌ Failed to save verified translation."
    except Exception as e:
        logging.error("Verified Save error: %s", e)
        return "Verified save error."


def get_translation_history(direction):
    headers = {
        "apikey": SUPABASE_API_KEY,
        "Authorization": f"Bearer {SUPABASE_API_KEY}"
    }

    table = "translations" if direction == "en_to_ks" else "ks_to_en_translations"

    try:
        res = requests.get(f"{SUPABASE_URL}/rest/v1/{table}?order=timestamp.desc&limit=20", headers=headers)
        normal_data = res.json() if res.status_code == 200 else []

        vres = requests.get(f"{SUPABASE_URL}/rest/v1/verified_translations?order=timestamp.desc&limit=20", headers=headers)
        verified_data = vres.json() if vres.status_code == 200 else []

        normal_history = "\n".join([
            f"Input: {r['input_text']} β†’ Output: {r['output_text']}"
            for r in normal_data
        ]) or "No regular translations yet."

        verified_history = "\n".join([
            f"Verified: {r['original_translation']} β†’ {r['verified_translation']}"
            for r in verified_data
        ]) or "No verified translations yet."

        return f"--- Regular Translations ---\n{normal_history}\n\n--- Verified Translations ---\n{verified_history}"

    except Exception as e:
        logging.error("History error: %s", e)
        return "Error loading history."


# --- Translation with TTS integration ---
def translate(text, direction, generate_tts=False):
    if not text.strip():
        return "Enter some text.", None

    if direction == "en_to_ks":
        src_lang, tgt_lang = "eng_Latn", "kas_Arab"
        model, tokenizer = model_en_to_indic, tokenizer_en_to_indic
    else:
        src_lang, tgt_lang = "kas_Arab", "eng_Latn"
        model, tokenizer = model_indic_to_en, tokenizer_indic_to_en

    try:
        batch = ip.preprocess_batch([text], src_lang=src_lang, tgt_lang=tgt_lang)
        tokens = tokenizer(batch, return_tensors="pt", padding=True).to(DEVICE)
        with torch.no_grad():
            output = model.generate(**tokens, max_length=256, num_beams=5)
        result = tokenizer.batch_decode(output, skip_special_tokens=True)
        final = ip.postprocess_batch(result, lang=tgt_lang)[0]
        
        # Generate TTS for KS→EN direction if requested
        audio_path = None
        if generate_tts and direction == "ks_to_en":
            audio_path = synthesize_tts(final)
            
        return final, audio_path
    except Exception as e:
        logging.error("Translation error: %s", e)
        return "Translation failed.", None

# --- TTS for English output ---
def synthesize_tts(text):
    try:
        tts = gTTS(text=text, lang="en")
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
            tts.save(f.name)
            return f.name
    except Exception as e:
        logging.error("TTS error: %s", e)
        return None

# --- STT for English audio ---
def transcribe_audio(audio_path):
    try:
        if not audio_path:
            return None, "No audio file provided"
        
        # Create a persistent copy of the audio file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
            temp_path = f.name
        shutil.copy(audio_path, temp_path)
        
        transcription = asr(temp_path)["text"]
        os.unlink(temp_path)  # Clean up temporary file
        return transcription, None
    except Exception as e:
        logging.error("STT error: %s", e)
        return None, f"Transcription failed: {str(e)}"

# --- Store audio file path ---
def store_audio(audio_path):
    """Store audio path in state and return it to keep it visible"""
    return audio_path

# --- Handle audio translation ---
def handle_audio_translation(audio_path, direction):
    if direction != "en_to_ks":
        return "Audio input is only supported for English to Kashmiri.", "", "", audio_path

    transcription, error = transcribe_audio(audio_path)
    if error:
        return error, "", "", audio_path
        
    translated, _ = translate(transcription, direction, generate_tts=False)
    return "", transcription, translated, audio_path

# --- Switch UI direction ---
def switch_direction(direction, input_text_val, output_text_val, audio_path):
    new_direction = "ks_to_en" if direction == "en_to_ks" else "en_to_ks"
    input_label = "Kashmiri Text" if new_direction == "ks_to_en" else "English Text"
    output_label = "English Translation" if new_direction == "ks_to_en" else "Kashmiri Translation"
    return new_direction, gr.update(value=output_text_val, label=input_label), gr.update(value=input_text_val, label=output_label), None

# === Gradio Interface ===
with gr.Blocks() as interface:
    gr.HTML("""
<div style="display: flex; justify-content: space-between; align-items: center; padding: 10px;">
    <img src="https://raw.githubusercontent.com/BurhaanRasheedZargar/Images/211321a234613a9c3dd944fe9367cf13d1386239/assets/left_logo.png" style="height:150px; width:auto;">
    <h2 style="margin: 0; text-align: center;">English ↔ Kashmiri Translator</h2>
    <img src="https://raw.githubusercontent.com/BurhaanRasheedZargar/Images/77797f7f7cbee328fa0f9d31cf3e290441e04cd3/assets/right_logo.png">
</div>
""")
    translation_direction = gr.State(value="en_to_ks")
    stored_audio = gr.State()

    with gr.Row():
        input_text = gr.Textbox(label="English Text", placeholder="Enter text here...", lines=2)
        output_text = gr.Textbox(label="Kashmiri Translation", placeholder="Translated text...", lines=2)
    with gr.Row():
        verified_text = gr.Textbox(label="✏️ Edit Translation", placeholder="Edit translation here...", lines=2)

    with gr.Row():
        translate_button = gr.Button("Translate")
        save_button = gr.Button("Save Translation")
        switch_button = gr.Button("Switch Direction")
        verify_button = gr.Button("βœ… Verify & Save")


    save_status = gr.Textbox(label="Save Status", interactive=False)
    history = gr.Textbox(label="Translation History", lines=8, interactive=False)

    with gr.Row():
        audio_input = gr.Audio(type="filepath", label="πŸŽ™οΈ Record English audio", sources=["microphone"])
        audio_output = gr.Audio(label="πŸ”Š English TTS", interactive=False)

    with gr.Row():
        stt_button = gr.Button("🎀 Transcribe & Translate (EN β†’ KS)")
        tts_button = gr.Button("πŸ”Š Translate & Speak (KS β†’ EN)")

    # Store audio when recorded
    audio_input.change(
        fn=store_audio,
        inputs=audio_input,
        outputs=stored_audio
    )

    # Events
    translate_button.click(
    fn=translate,
    inputs=[input_text, translation_direction, gr.State(False)],
    outputs=[output_text, audio_output]
    ).then(
    fn=lambda txt: txt,
    inputs=output_text,
    outputs=verified_text
    )

    
    tts_button.click(
    fn=translate,
    inputs=[input_text, translation_direction, gr.State(True)],
    outputs=[output_text, audio_output]
    ).then(
    fn=lambda txt: txt,
    inputs=output_text,
    outputs=verified_text
    )


    save_button.click(
        fn=save_to_supabase,
        inputs=[input_text, output_text, translation_direction],
        outputs=save_status
    ).then(
        fn=get_translation_history,
        inputs=translation_direction,
        outputs=history
    )

    switch_button.click(
        fn=switch_direction,
        inputs=[translation_direction, input_text, output_text, stored_audio],
        outputs=[translation_direction, input_text, output_text, audio_output]
    )

    stt_button.click(
        fn=handle_audio_translation,
        inputs=[stored_audio, translation_direction],
        outputs=[save_status, input_text, output_text, audio_input]
    ).then(
    fn=lambda txt: txt,
    inputs=output_text,
    outputs=verified_text
    )
    verify_button.click(
        fn=save_verified_translation,
        inputs=[output_text, verified_text],
        outputs=save_status
    ).then(
        fn=get_translation_history,
        inputs=translation_direction,
        outputs=history
    )


if __name__ == "__main__":
    interface.queue().launch(share=True)