Spaces:
Sleeping
Sleeping
Yaron Koresh
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -43,22 +43,26 @@ def translate(text,lang):
|
|
43 |
print(url)
|
44 |
resp = requests.get(
|
45 |
url = url,
|
46 |
-
params = {'q': f'{lang}
|
47 |
headers = {
|
48 |
'User-Agent': random.choice(user_agents)
|
49 |
}
|
50 |
)
|
51 |
content = resp.content
|
52 |
html = fromstring(content)
|
53 |
-
|
54 |
-
|
|
|
55 |
translated = text
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
except:
|
60 |
-
|
61 |
-
|
62 |
ret = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', translated)).lower().strip()
|
63 |
print(ret)
|
64 |
return ret
|
|
|
43 |
print(url)
|
44 |
resp = requests.get(
|
45 |
url = url,
|
46 |
+
params = {'q': f'{lang} translate {text}'},
|
47 |
headers = {
|
48 |
'User-Agent': random.choice(user_agents)
|
49 |
}
|
50 |
)
|
51 |
content = resp.content
|
52 |
html = fromstring(content)
|
53 |
+
|
54 |
+
#src = html.xpath('//pre[@data-placeholder="Enter text"]/textarea')[0].text.strip()
|
55 |
+
|
56 |
translated = text
|
57 |
+
|
58 |
+
try:
|
59 |
+
trgt = html.xpath('//span[@class="target-language"]')[0].text.strip()
|
60 |
+
rslt = html.xpath('//pre[@aria-label="Translated text"]/span')[0].text.strip()
|
61 |
+
if trgt.lower() == lang.lower():
|
62 |
+
translated = rslt
|
63 |
except:
|
64 |
+
print("Translation Error!")
|
65 |
+
|
66 |
ret = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', translated)).lower().strip()
|
67 |
print(ret)
|
68 |
return ret
|