kambris commited on
Commit
47cda52
Β·
verified Β·
1 Parent(s): b9278f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from gtts import gTTS
3
  import tempfile
4
  import os
5
- from googletrans import Translator
6
 
7
  # Enhanced transliteration map with proper Arabic characters
8
  translit_map = {
@@ -99,9 +99,6 @@ latin_to_ipa = {
99
  "!": "!"
100
  }
101
 
102
- # Initialize Google Translator
103
- translator = Translator()
104
-
105
  def clean_arabic_text(text):
106
  """Remove diacritics from Arabic text for translation"""
107
  if not text:
@@ -127,11 +124,12 @@ def google_translate_arabic(arabic_text):
127
  # Clean the text
128
  clean_text = clean_arabic_text(arabic_text)
129
 
130
- # Detect language first
131
- detection = translator.detect(clean_text)
132
-
133
- # Translate to English
134
- translation = translator.translate(clean_text, src='ar', dest='en')
 
135
 
136
  # Format the result
137
  result = f"🌐 **Google Translate Results:**\n\n"
@@ -171,8 +169,8 @@ def translate_multiple_words(arabic_text):
171
  for i, word in enumerate(words[:5], 1): # Limit to first 5 words
172
  if len(word.strip()) > 1: # Skip single characters
173
  try:
174
- word_translation = translator.translate(clean_arabic_text(word), src='ar', dest='en')
175
- individual_translations.append(f"**{i}. {word}** β†’ {word_translation.text}")
176
  except:
177
  individual_translations.append(f"**{i}. {word}** β†’ (translation failed)")
178
 
@@ -196,15 +194,15 @@ def reverse_translate_english(english_text):
196
 
197
  try:
198
  # Translate to Arabic
199
- translation = translator.translate(english_text, src='en', dest='ar')
200
 
201
  # Format the result
202
  result = f"🌐 **English to Arabic Translation:**\n\n"
203
  result += f"**Original English:** {english_text}\n\n"
204
- result += f"**Arabic Translation:** {translation.text}\n\n"
205
  result += f"**Source:** Google Translate"
206
 
207
- return result, translation.text
208
 
209
  except Exception as e:
210
  return f"❌ Translation error: {str(e)}", ""
 
2
  from gtts import gTTS
3
  import tempfile
4
  import os
5
+ from deep_translator import GoogleTranslator
6
 
7
  # Enhanced transliteration map with proper Arabic characters
8
  translit_map = {
 
99
  "!": "!"
100
  }
101
 
 
 
 
102
  def clean_arabic_text(text):
103
  """Remove diacritics from Arabic text for translation"""
104
  if not text:
 
124
  # Clean the text
125
  clean_text = clean_arabic_text(arabic_text)
126
 
127
+ try:
128
+ translation_text = GoogleTranslator(source='ar', target='en').translate(clean_text)
129
+ detection_lang = GoogleTranslator(source='auto', target='en').detect(clean_text)
130
+ except:
131
+ detection_lang = 'ar'
132
+ translation_text = GoogleTranslator(source='ar', target='en').translate(clean_text)
133
 
134
  # Format the result
135
  result = f"🌐 **Google Translate Results:**\n\n"
 
169
  for i, word in enumerate(words[:5], 1): # Limit to first 5 words
170
  if len(word.strip()) > 1: # Skip single characters
171
  try:
172
+ word_translation = GoogleTranslator(source='ar', target='en').translate(clean_arabic_text(word))
173
+ individual_translations.append(f"**{i}. {word}** β†’ {word_translation}")
174
  except:
175
  individual_translations.append(f"**{i}. {word}** β†’ (translation failed)")
176
 
 
194
 
195
  try:
196
  # Translate to Arabic
197
+ translation_text = GoogleTranslator(source='en', target='ar').translate(english_text)
198
 
199
  # Format the result
200
  result = f"🌐 **English to Arabic Translation:**\n\n"
201
  result += f"**Original English:** {english_text}\n\n"
202
+ result += f"**Arabic Translation:** {translation_text}\n\n"
203
  result += f"**Source:** Google Translate"
204
 
205
+ return result, translation_text
206
 
207
  except Exception as e:
208
  return f"❌ Translation error: {str(e)}", ""