Afrinetwork7
commited on
Commit
•
d9b1305
1
Parent(s):
7a0eb63
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
|
3 |
import os
|
4 |
import random
|
5 |
import uuid
|
|
|
|
|
|
|
6 |
|
7 |
import gradio as gr
|
8 |
import numpy as np
|
@@ -25,6 +26,8 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
25 |
|
26 |
NUM_IMAGES_PER_PROMPT = 1
|
27 |
|
|
|
|
|
28 |
if torch.cuda.is_available():
|
29 |
pipe = DiffusionPipeline.from_pretrained(
|
30 |
"playgroundai/playground-v2.5-1024px-aesthetic",
|
@@ -36,9 +39,9 @@ if torch.cuda.is_available():
|
|
36 |
if ENABLE_CPU_OFFLOAD:
|
37 |
pipe.enable_model_cpu_offload()
|
38 |
else:
|
39 |
-
pipe.to(device)
|
40 |
print("Loaded on Device!")
|
41 |
-
|
42 |
if USE_TORCH_COMPILE:
|
43 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
44 |
print("Model Compiled!")
|
@@ -56,9 +59,28 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
56 |
return seed
|
57 |
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
@spaces.GPU(enable_queue=True)
|
60 |
def generate(
|
61 |
-
|
|
|
62 |
negative_prompt: str = "",
|
63 |
use_negative_prompt: bool = False,
|
64 |
seed: int = 0,
|
@@ -73,9 +95,14 @@ def generate(
|
|
73 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
74 |
generator = torch.Generator().manual_seed(seed)
|
75 |
|
|
|
|
|
|
|
|
|
|
|
76 |
if not use_negative_prompt:
|
77 |
negative_prompt = None # type: ignore
|
78 |
-
|
79 |
images = pipe(
|
80 |
prompt=prompt,
|
81 |
negative_prompt=negative_prompt,
|
@@ -116,6 +143,7 @@ with gr.Blocks(css=css) as demo:
|
|
116 |
)
|
117 |
with gr.Group():
|
118 |
with gr.Row():
|
|
|
119 |
prompt = gr.Text(
|
120 |
label="Prompt",
|
121 |
show_label=False,
|
@@ -168,7 +196,7 @@ with gr.Blocks(css=css) as demo:
|
|
168 |
|
169 |
gr.Examples(
|
170 |
examples=examples,
|
171 |
-
inputs=prompt,
|
172 |
outputs=[result, seed],
|
173 |
fn=generate,
|
174 |
cache_examples=CACHE_EXAMPLES,
|
@@ -190,6 +218,7 @@ with gr.Blocks(css=css) as demo:
|
|
190 |
fn=generate,
|
191 |
inputs=[
|
192 |
prompt,
|
|
|
193 |
negative_prompt,
|
194 |
use_negative_prompt,
|
195 |
seed,
|
@@ -197,10 +226,10 @@ with gr.Blocks(css=css) as demo:
|
|
197 |
height,
|
198 |
guidance_scale,
|
199 |
randomize_seed,
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
|
205 |
if __name__ == "__main__":
|
206 |
-
|
|
|
|
|
|
|
1 |
import os
|
2 |
import random
|
3 |
import uuid
|
4 |
+
from urllib.parse import quote
|
5 |
+
from requests import get
|
6 |
+
from bs4 import BeautifulSoup
|
7 |
|
8 |
import gradio as gr
|
9 |
import numpy as np
|
|
|
26 |
|
27 |
NUM_IMAGES_PER_PROMPT = 1
|
28 |
|
29 |
+
valid_languages = {'fon', 'fr', 'yo', 'en'}
|
30 |
+
|
31 |
if torch.cuda.is_available():
|
32 |
pipe = DiffusionPipeline.from_pretrained(
|
33 |
"playgroundai/playground-v2.5-1024px-aesthetic",
|
|
|
39 |
if ENABLE_CPU_OFFLOAD:
|
40 |
pipe.enable_model_cpu_offload()
|
41 |
else:
|
42 |
+
pipe.to(device)
|
43 |
print("Loaded on Device!")
|
44 |
+
|
45 |
if USE_TORCH_COMPILE:
|
46 |
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
47 |
print("Model Compiled!")
|
|
|
59 |
return seed
|
60 |
|
61 |
|
62 |
+
def translate_to_english(phrase, src_lang):
|
63 |
+
if src_lang == 'en':
|
64 |
+
return phrase
|
65 |
+
|
66 |
+
dest_lang = 'en'
|
67 |
+
encoded_phrase = quote(phrase)
|
68 |
+
url = f"https://translate.glosbe.com/{src_lang}-{dest_lang}/{encoded_phrase}"
|
69 |
+
response = get(url)
|
70 |
+
|
71 |
+
if response.status_code == 200:
|
72 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
73 |
+
translation_div = soup.find('div', class_='w-full h-full bg-gray-100 h-full border p-2 min-h-25vh sm:min-h-50vh whitespace-pre-wrap break-words')
|
74 |
+
translation = translation_div.text if translation_div else "Translation not found"
|
75 |
+
return translation
|
76 |
+
else:
|
77 |
+
return "Error: Unable to translate"
|
78 |
+
|
79 |
+
|
80 |
@spaces.GPU(enable_queue=True)
|
81 |
def generate(
|
82 |
+
phrase: str,
|
83 |
+
input_lang: str,
|
84 |
negative_prompt: str = "",
|
85 |
use_negative_prompt: bool = False,
|
86 |
seed: int = 0,
|
|
|
95 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
96 |
generator = torch.Generator().manual_seed(seed)
|
97 |
|
98 |
+
if input_lang != 'en':
|
99 |
+
prompt = translate_to_english(phrase, input_lang)
|
100 |
+
else:
|
101 |
+
prompt = phrase
|
102 |
+
|
103 |
if not use_negative_prompt:
|
104 |
negative_prompt = None # type: ignore
|
105 |
+
|
106 |
images = pipe(
|
107 |
prompt=prompt,
|
108 |
negative_prompt=negative_prompt,
|
|
|
143 |
)
|
144 |
with gr.Group():
|
145 |
with gr.Row():
|
146 |
+
input_lang = gr.Dropdown(choices=list(valid_languages), value='en', label='Input Language')
|
147 |
prompt = gr.Text(
|
148 |
label="Prompt",
|
149 |
show_label=False,
|
|
|
196 |
|
197 |
gr.Examples(
|
198 |
examples=examples,
|
199 |
+
inputs=[prompt, input_lang],
|
200 |
outputs=[result, seed],
|
201 |
fn=generate,
|
202 |
cache_examples=CACHE_EXAMPLES,
|
|
|
218 |
fn=generate,
|
219 |
inputs=[
|
220 |
prompt,
|
221 |
+
input_lang,
|
222 |
negative_prompt,
|
223 |
use_negative_prompt,
|
224 |
seed,
|
|
|
226 |
height,
|
227 |
guidance_scale,
|
228 |
randomize_seed,
|
229 |
+
],
|
230 |
+
outputs=[result, seed],
|
231 |
+
api_name="run",
|
232 |
+
)
|
233 |
|
234 |
if __name__ == "__main__":
|
235 |
+
demo.queue(max_size=20).launch()
|