Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
|
|
|
| 5 |
|
| 6 |
# Enhanced transliteration mapping (English phonetics to Hebrew letters)
|
| 7 |
transliteration_map = {
|
|
@@ -52,34 +53,17 @@ def transliterate(text):
|
|
| 52 |
return ''.join(result)
|
| 53 |
|
| 54 |
def translate_text(text):
|
| 55 |
-
"""Translate English text to Hebrew using
|
| 56 |
if not text or not text.strip():
|
| 57 |
return ""
|
| 58 |
|
| 59 |
try:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
"source": "en",
|
| 64 |
-
"target": "he",
|
| 65 |
-
"format": "text"
|
| 66 |
-
}
|
| 67 |
|
| 68 |
-
headers = {"Content-Type": "application/json"}
|
| 69 |
-
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
| 70 |
-
|
| 71 |
-
if response.status_code == 200:
|
| 72 |
-
result = response.json()
|
| 73 |
-
return result.get('translatedText', 'Translation failed')
|
| 74 |
-
else:
|
| 75 |
-
return f"Error {response.status_code}: Unable to translate"
|
| 76 |
-
|
| 77 |
-
except requests.exceptions.Timeout:
|
| 78 |
-
return "Translation timed out. Check your connection."
|
| 79 |
-
except requests.exceptions.RequestException:
|
| 80 |
-
return "Network error: Unable to reach translation service."
|
| 81 |
except Exception as e:
|
| 82 |
-
return f"
|
| 83 |
|
| 84 |
def translate_multiple_words(hebrew_text):
|
| 85 |
"""Translate multiple Hebrew words - both individually and as a phrase"""
|
|
@@ -88,16 +72,7 @@ def translate_multiple_words(hebrew_text):
|
|
| 88 |
|
| 89 |
try:
|
| 90 |
# Translate the full phrase
|
| 91 |
-
|
| 92 |
-
payload = {"q": hebrew_text, "source": "he", "target": "en", "format": "text"}
|
| 93 |
-
headers = {"Content-Type": "application/json"}
|
| 94 |
-
|
| 95 |
-
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
| 96 |
-
|
| 97 |
-
if response.status_code == 200:
|
| 98 |
-
full_translation = response.json().get('translatedText', '')
|
| 99 |
-
else:
|
| 100 |
-
full_translation = "Translation failed"
|
| 101 |
|
| 102 |
# Translate individual words if multiple
|
| 103 |
words = hebrew_text.strip().split()
|
|
@@ -107,12 +82,7 @@ def translate_multiple_words(hebrew_text):
|
|
| 107 |
for i, word in enumerate(words[:5], 1):
|
| 108 |
if len(word.strip()) > 1:
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
response = requests.post(url, json=payload, headers=headers, timeout=5)
|
| 112 |
-
if response.status_code == 200:
|
| 113 |
-
word_trans = response.json().get('translatedText', '(failed)')
|
| 114 |
-
else:
|
| 115 |
-
word_trans = "(failed)"
|
| 116 |
individual_translations.append(f"**{i}. {word}** → {word_trans}")
|
| 117 |
except:
|
| 118 |
individual_translations.append(f"**{i}. {word}** → (failed)")
|
|
@@ -134,16 +104,8 @@ def reverse_translate(hebrew_text):
|
|
| 134 |
return "No text provided"
|
| 135 |
|
| 136 |
try:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
headers = {"Content-Type": "application/json"}
|
| 140 |
-
|
| 141 |
-
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
| 142 |
-
|
| 143 |
-
if response.status_code == 200:
|
| 144 |
-
return response.json().get('translatedText', 'Translation failed')
|
| 145 |
-
else:
|
| 146 |
-
return f"Error {response.status_code}"
|
| 147 |
except Exception as e:
|
| 148 |
return f"Error: {str(e)}"
|
| 149 |
|
|
|
|
| 2 |
import requests
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
| 5 |
+
from deep_translator import GoogleTranslator
|
| 6 |
|
| 7 |
# Enhanced transliteration mapping (English phonetics to Hebrew letters)
|
| 8 |
transliteration_map = {
|
|
|
|
| 53 |
return ''.join(result)
|
| 54 |
|
| 55 |
def translate_text(text):
|
| 56 |
+
"""Translate English text to Hebrew using Google Translate"""
|
| 57 |
if not text or not text.strip():
|
| 58 |
return ""
|
| 59 |
|
| 60 |
try:
|
| 61 |
+
# Use Google Translate via deep_translator
|
| 62 |
+
translation = GoogleTranslator(source='en', target='he').translate(text)
|
| 63 |
+
return translation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
+
return f"Translation error: {str(e)}"
|
| 67 |
|
| 68 |
def translate_multiple_words(hebrew_text):
|
| 69 |
"""Translate multiple Hebrew words - both individually and as a phrase"""
|
|
|
|
| 72 |
|
| 73 |
try:
|
| 74 |
# Translate the full phrase
|
| 75 |
+
full_translation = GoogleTranslator(source='he', target='en').translate(hebrew_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Translate individual words if multiple
|
| 78 |
words = hebrew_text.strip().split()
|
|
|
|
| 82 |
for i, word in enumerate(words[:5], 1):
|
| 83 |
if len(word.strip()) > 1:
|
| 84 |
try:
|
| 85 |
+
word_trans = GoogleTranslator(source='he', target='en').translate(word)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
individual_translations.append(f"**{i}. {word}** → {word_trans}")
|
| 87 |
except:
|
| 88 |
individual_translations.append(f"**{i}. {word}** → (failed)")
|
|
|
|
| 104 |
return "No text provided"
|
| 105 |
|
| 106 |
try:
|
| 107 |
+
translation = GoogleTranslator(source='he', target='en').translate(hebrew_text)
|
| 108 |
+
return translation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
return f"Error: {str(e)}"
|
| 111 |
|