Yaron Koresh commited on
Commit
b4f9b4b
·
verified ·
1 Parent(s): 6a374de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -1,14 +1,16 @@
1
- import gradio as gr
2
  import spaces
3
- from tempfile import NamedTemporaryFile
 
4
  import numpy as np
5
  import random
6
  from diffusers import StableDiffusionPipeline as DiffusionPipeline
7
  import torch
8
- from pathos.multiprocessing import ProcessingPool as ProcessPoolExecutor
 
 
9
 
10
- pool = ProcessPoolExecutor(100)
11
- pool.__enter__()
12
 
13
  model_id = "runwayml/stable-diffusion-v1-5"
14
 
@@ -22,13 +24,21 @@ else:
22
  pipe = DiffusionPipeline.from_pretrained(model_id, use_safetensors=True)
23
  pipe = pipe.to(device)
24
 
25
- @spaces.GPU(10)
 
 
 
 
 
 
 
 
 
 
26
  def infer(prompt):
27
- image = pipe(prompt).images[0]
28
- ret = None
29
- with NamedTemporaryFile("wb", suffix=".png", delete=False) as file:
30
- ret = file.name
31
- return ret
32
 
33
  css="""
34
  #col-container {
 
 
1
  import spaces
2
+ import gradio as gr
3
+ #from tempfile import NamedTemporaryFile
4
  import numpy as np
5
  import random
6
  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(100)
13
+ #pool.__enter__()
14
 
15
  model_id = "runwayml/stable-diffusion-v1-5"
16
 
 
24
  pipe = DiffusionPipeline.from_pretrained(model_id, use_safetensors=True)
25
  pipe = pipe.to(device)
26
 
27
+ def he_to_en(prompt):
28
+ html_str = requests.get( url = "https://translate.google.com", params = {"sl": "iw", "tl": "en", "op": "translate", "text": prompt} ).text()
29
+ tree = fromstring(html_str)
30
+ english = tree.xpath('span[lang="en"]/span/span/text()')[0]
31
+ return english
32
+
33
+ def generate_random_string(length):
34
+ characters = string.ascii_letters + string.digits
35
+ return ''.join(random.choice(characters) for _ in range(length))
36
+
37
+ @spaces.GPU(25)
38
  def infer(prompt):
39
+ name = generate_random_string(12)+".png"
40
+ image = pipe(he_to_en(prompt)).images[0].save(name)
41
+ return name
 
 
42
 
43
  css="""
44
  #col-container {