Spaces:
Sleeping
Sleeping
Commit
Β·
4c0911f
1
Parent(s):
a1faa86
Added verifired translation table
Browse files
app.py
CHANGED
|
@@ -49,24 +49,63 @@ def save_to_supabase(input_text, output_text, direction):
|
|
| 49 |
except Exception as e:
|
| 50 |
logging.error("Save error: %s", e)
|
| 51 |
return "β Save error."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def get_translation_history(direction):
|
| 54 |
-
table = "translations" if direction == "en_to_ks" else "ks_to_en_translations"
|
| 55 |
headers = {
|
| 56 |
"apikey": SUPABASE_API_KEY,
|
| 57 |
"Authorization": f"Bearer {SUPABASE_API_KEY}"
|
| 58 |
}
|
| 59 |
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
-
res = requests.get(f"{SUPABASE_URL}/rest/v1/{table}?order=timestamp.desc&limit=
|
| 62 |
-
if res.status_code == 200
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
logging.error("History error: %s", e)
|
| 68 |
return "Error loading history."
|
| 69 |
|
|
|
|
| 70 |
# --- Translation with TTS integration ---
|
| 71 |
def translate(text, direction, generate_tts=False):
|
| 72 |
if not text.strip():
|
|
@@ -165,11 +204,15 @@ with gr.Blocks() as interface:
|
|
| 165 |
with gr.Row():
|
| 166 |
input_text = gr.Textbox(label="English Text", placeholder="Enter text here...", lines=2)
|
| 167 |
output_text = gr.Textbox(label="Kashmiri Translation", placeholder="Translated text...", lines=2)
|
|
|
|
|
|
|
| 168 |
|
| 169 |
with gr.Row():
|
| 170 |
translate_button = gr.Button("Translate")
|
| 171 |
save_button = gr.Button("Save Translation")
|
| 172 |
switch_button = gr.Button("Switch Direction")
|
|
|
|
|
|
|
| 173 |
|
| 174 |
save_status = gr.Textbox(label="Save Status", interactive=False)
|
| 175 |
history = gr.Textbox(label="Translation History", lines=8, interactive=False)
|
|
@@ -223,6 +266,16 @@ with gr.Blocks() as interface:
|
|
| 223 |
inputs=[stored_audio, translation_direction],
|
| 224 |
outputs=[save_status, input_text, output_text, audio_input]
|
| 225 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
interface.queue().launch(share=True)
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
logging.error("Save error: %s", e)
|
| 51 |
return "β Save error."
|
| 52 |
+
|
| 53 |
+
# --- Save verified translation ---
|
| 54 |
+
def save_verified_translation(original_text, verified_text):
|
| 55 |
+
if not original_text.strip() or not verified_text.strip():
|
| 56 |
+
return "Nothing to save."
|
| 57 |
+
|
| 58 |
+
payload = {
|
| 59 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 60 |
+
"original_translation": original_text,
|
| 61 |
+
"verified_translation": verified_text
|
| 62 |
+
}
|
| 63 |
+
headers = {
|
| 64 |
+
"apikey": SUPABASE_API_KEY,
|
| 65 |
+
"Authorization": f"Bearer {SUPABASE_API_KEY}",
|
| 66 |
+
"Content-Type": "application/json"
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
response = requests.post(f"{SUPABASE_URL}/rest/v1/verified_translations", json=payload, headers=headers)
|
| 71 |
+
return "β
Verified translation saved!" if response.status_code == 201 else "β Failed to save verified translation."
|
| 72 |
+
except Exception as e:
|
| 73 |
+
logging.error("Verified Save error: %s", e)
|
| 74 |
+
return "β Verified save error."
|
| 75 |
+
|
| 76 |
|
| 77 |
def get_translation_history(direction):
|
|
|
|
| 78 |
headers = {
|
| 79 |
"apikey": SUPABASE_API_KEY,
|
| 80 |
"Authorization": f"Bearer {SUPABASE_API_KEY}"
|
| 81 |
}
|
| 82 |
|
| 83 |
+
table = "translations" if direction == "en_to_ks" else "ks_to_en_translations"
|
| 84 |
+
|
| 85 |
try:
|
| 86 |
+
res = requests.get(f"{SUPABASE_URL}/rest/v1/{table}?order=timestamp.desc&limit=20", headers=headers)
|
| 87 |
+
normal_data = res.json() if res.status_code == 200 else []
|
| 88 |
+
|
| 89 |
+
vres = requests.get(f"{SUPABASE_URL}/rest/v1/verified_translations?order=timestamp.desc&limit=20", headers=headers)
|
| 90 |
+
verified_data = vres.json() if vres.status_code == 200 else []
|
| 91 |
+
|
| 92 |
+
normal_history = "\n".join([
|
| 93 |
+
f"π Input: {r['input_text']} β Output: {r['output_text']}"
|
| 94 |
+
for r in normal_data
|
| 95 |
+
]) or "No regular translations yet."
|
| 96 |
+
|
| 97 |
+
verified_history = "\n".join([
|
| 98 |
+
f"β
Verified: {r['original_translation']} β {r['verified_translation']}"
|
| 99 |
+
for r in verified_data
|
| 100 |
+
]) or "No verified translations yet."
|
| 101 |
+
|
| 102 |
+
return f"--- Regular Translations ---\n{normal_history}\n\n--- Verified Translations ---\n{verified_history}"
|
| 103 |
+
|
| 104 |
except Exception as e:
|
| 105 |
logging.error("History error: %s", e)
|
| 106 |
return "Error loading history."
|
| 107 |
|
| 108 |
+
|
| 109 |
# --- Translation with TTS integration ---
|
| 110 |
def translate(text, direction, generate_tts=False):
|
| 111 |
if not text.strip():
|
|
|
|
| 204 |
with gr.Row():
|
| 205 |
input_text = gr.Textbox(label="English Text", placeholder="Enter text here...", lines=2)
|
| 206 |
output_text = gr.Textbox(label="Kashmiri Translation", placeholder="Translated text...", lines=2)
|
| 207 |
+
with gr.Row():
|
| 208 |
+
verified_text = gr.Textbox(label="βοΈ Edit Translation", placeholder="Edit translation here...", lines=2)
|
| 209 |
|
| 210 |
with gr.Row():
|
| 211 |
translate_button = gr.Button("Translate")
|
| 212 |
save_button = gr.Button("Save Translation")
|
| 213 |
switch_button = gr.Button("Switch Direction")
|
| 214 |
+
verify_button = gr.Button("β
Verify & Save")
|
| 215 |
+
|
| 216 |
|
| 217 |
save_status = gr.Textbox(label="Save Status", interactive=False)
|
| 218 |
history = gr.Textbox(label="Translation History", lines=8, interactive=False)
|
|
|
|
| 266 |
inputs=[stored_audio, translation_direction],
|
| 267 |
outputs=[save_status, input_text, output_text, audio_input]
|
| 268 |
)
|
| 269 |
+
verify_button.click(
|
| 270 |
+
fn=save_verified_translation,
|
| 271 |
+
inputs=[output_text, verified_text],
|
| 272 |
+
outputs=save_status
|
| 273 |
+
).then(
|
| 274 |
+
fn=get_translation_history,
|
| 275 |
+
inputs=translation_direction,
|
| 276 |
+
outputs=history
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
|
| 280 |
if __name__ == "__main__":
|
| 281 |
interface.queue().launch(share=True)
|