Yaron Koresh commited on
Commit
5087a64
·
verified ·
1 Parent(s): cda062e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -13,9 +13,18 @@ pool = ProcessPoolExecutor(4)
13
  pool.__enter__()
14
 
15
  model_id = "runwayml/stable-diffusion-v1-5"
16
-
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
 
 
 
 
 
 
 
 
 
 
 
19
  if torch.cuda.is_available():
20
  torch.cuda.max_memory_allocated(device=device)
21
  pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
@@ -25,9 +34,13 @@ else:
25
  pipe = pipe.to(device)
26
 
27
  def translate(text,lang):
28
- html_str = requests.get( url = "http://translate.google.com", params = {"sl": "auto", "tl": lang, "op": "translate", "text": text}, headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'} ).text
 
 
 
 
29
  tree = fromstring(html_str)
30
- translated = tree.xpath('//span[@lang="'+lang+'"]/span/span/text()')[0]
31
  return translated
32
 
33
  def generate_random_string(length):
 
13
  pool.__enter__()
14
 
15
  model_id = "runwayml/stable-diffusion-v1-5"
 
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
 
18
+ user_agents = [
19
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
20
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
21
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
22
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
23
+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
24
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15'
25
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15'
26
+ ]
27
+
28
  if torch.cuda.is_available():
29
  torch.cuda.max_memory_allocated(device=device)
30
  pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
 
34
  pipe = pipe.to(device)
35
 
36
  def translate(text,lang):
37
+ html_str = requests.get(
38
+ url = "http://translate.google.com",
39
+ params = {"sl": "auto", "tl": lang, "op": "translate", "text": text},
40
+ headers = {'User-Agent': random.choice(user_agents)}
41
+ ).text
42
  tree = fromstring(html_str)
43
+ translated = tree.xpath('//span[@lang="'+lang+'"]/span/span[text()]')[0]
44
  return translated
45
 
46
  def generate_random_string(length):