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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -7,7 +7,7 @@ from diffusers import StableDiffusionPipeline as DiffusionPipeline
7
  import torch
8
  from pathos.multiprocessing import ProcessingPool as ProcessPoolExecutor
9
  import requests
10
- from lxml.html.soupparser import fromstring
11
 
12
  pool = ProcessPoolExecutor(4)
13
  pool.__enter__()
@@ -15,16 +15,6 @@ pool.__enter__()
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,13 +24,22 @@ else:
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):
@@ -49,7 +48,9 @@ def generate_random_string(length):
49
 
50
  def infer(prompt):
51
  name = generate_random_string(12)+".png"
52
- image = pipe(translate(prompt,"en")).images[0].save(name)
 
 
53
  return name
54
 
55
  css="""
 
7
  import torch
8
  from pathos.multiprocessing import ProcessingPool as ProcessPoolExecutor
9
  import requests
10
+ from lxml import etree
11
 
12
  pool = ProcessPoolExecutor(4)
13
  pool.__enter__()
 
15
  model_id = "runwayml/stable-diffusion-v1-5"
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
 
 
 
 
 
 
 
 
 
 
 
18
  if torch.cuda.is_available():
19
  torch.cuda.max_memory_allocated(device=device)
20
  pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
 
24
  pipe = pipe.to(device)
25
 
26
  def translate(text,lang):
27
+ user_agents = [
28
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
29
+ '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'
30
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
31
+ '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'
32
+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
33
+ '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'
34
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15'
35
+ ]
36
  html_str = requests.get(
37
  url = "http://translate.google.com",
38
  params = {"sl": "auto", "tl": lang, "op": "translate", "text": text},
39
  headers = {'User-Agent': random.choice(user_agents)}
40
  ).text
41
+ tree = etree.HTML(html_str)
42
+ translated = tree.xpath(f'//span[@lang="{lang}"]/span/span[text()]')
43
  return translated
44
 
45
  def generate_random_string(length):
 
48
 
49
  def infer(prompt):
50
  name = generate_random_string(12)+".png"
51
+ english_prompt = translate(prompt,"en")
52
+ print(f'Final prompt: {english_prompt}')
53
+ image = pipe(english_prompt).images[0].save(name)
54
  return name
55
 
56
  css="""