Yaron Koresh commited on
Commit
d849b8e
·
verified ·
1 Parent(s): a00bc49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -113,9 +113,12 @@ function custom(){
113
  def run(cmd):
114
  return str(subprocess.run(cmd, shell=True, capture_output=True, env=None).stdout)
115
 
116
- def xpath(str,pattern):
117
- return fromstring(str).xpath(pattern)[0].text_content().lower().strip()
118
-
 
 
 
119
  @gpu(string(string,string),device=True,inline=True)
120
  def translate(text,lang):
121
 
@@ -144,15 +147,12 @@ def translate(text,lang):
144
  }
145
  ).content)
146
  translated = text
147
- try:
148
- src_lang = xpath(content,'//*[@class="source-language"]')
149
- trgt_lang = xpath(content,'//*[@class="target-language"]')
150
- src_text = xpath(content,'//*[@id="tw-source-text"]/*')
151
- trgt_text = xpath(content,'//*[@id="tw-target-text"]/*')
152
- if trgt_lang == lang:
153
- translated = trgt_text
154
- except:
155
- print(f'Translation Warning: Failed To Translate!')
156
  ret = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', translated)).lower().strip()
157
  print(ret)
158
  return ret
 
113
  def run(cmd):
114
  return str(subprocess.run(cmd, shell=True, capture_output=True, env=None).stdout)
115
 
116
+ def xpath_finder(str,pattern):
117
+ try:
118
+ return ""+fromstring(str).xpath(pattern)[0].text_content().lower().strip()
119
+ except:
120
+ return ""
121
+
122
  @gpu(string(string,string),device=True,inline=True)
123
  def translate(text,lang):
124
 
 
147
  }
148
  ).content)
149
  translated = text
150
+ src_lang = xpath_finder(content,'//*[@class="source-language"]')
151
+ trgt_lang = xpath_finder(content,'//*[@class="target-language"]')
152
+ src_text = xpath_finder(content,'//*[@id="tw-source-text"]/*')
153
+ trgt_text = xpath_finder(content,'//*[@id="tw-target-text"]/*')
154
+ if trgt_lang == lang:
155
+ translated = trgt_text
 
 
 
156
  ret = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', translated)).lower().strip()
157
  print(ret)
158
  return ret