Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,6 @@ from PIL import Image
|
|
11 |
import spaces
|
12 |
import torch
|
13 |
from diffusers import DiffusionPipeline
|
14 |
-
from huggingface_hub import Client
|
15 |
|
16 |
DESCRIPTION = """# Playground v2.5"""
|
17 |
if not torch.cuda.is_available():
|
@@ -27,14 +26,7 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
27 |
|
28 |
NUM_IMAGES_PER_PROMPT = 1
|
29 |
|
30 |
-
|
31 |
-
"fon": "fon (Fon)",
|
32 |
-
"yo": "yo (Yoruba)",
|
33 |
-
"fr": "fra (French)",
|
34 |
-
"en": "eng (English)"
|
35 |
-
}
|
36 |
-
|
37 |
-
valid_languages = set(languages.keys())
|
38 |
|
39 |
if torch.cuda.is_available():
|
40 |
pipe = DiffusionPipeline.from_pretrained(
|
@@ -87,7 +79,7 @@ def translate_to_english(phrase, src_lang):
|
|
87 |
|
88 |
@spaces.GPU(enable_queue=True)
|
89 |
def generate(
|
90 |
-
|
91 |
input_lang: str,
|
92 |
negative_prompt: str = "",
|
93 |
use_negative_prompt: bool = False,
|
@@ -103,20 +95,10 @@ def generate(
|
|
103 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
104 |
generator = torch.Generator().manual_seed(seed)
|
105 |
|
106 |
-
client = Client("https://afrinetwork-speak.hf.space/")
|
107 |
-
language_name = languages[input_lang]
|
108 |
-
result = client.predict(
|
109 |
-
"Record from Mic",
|
110 |
-
audio,
|
111 |
-
language_name,
|
112 |
-
api_name="/predict"
|
113 |
-
)
|
114 |
-
transcript = result["transcription"]
|
115 |
-
|
116 |
if input_lang != 'en':
|
117 |
-
prompt = translate_to_english(
|
118 |
else:
|
119 |
-
prompt =
|
120 |
|
121 |
if not use_negative_prompt:
|
122 |
negative_prompt = None # type: ignore
|
@@ -161,8 +143,14 @@ with gr.Blocks(css=css) as demo:
|
|
161 |
)
|
162 |
with gr.Group():
|
163 |
with gr.Row():
|
164 |
-
input_lang = gr.Dropdown(choices=list(
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
run_button = gr.Button("Run", scale=0)
|
167 |
result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT, show_label=False)
|
168 |
with gr.Accordion("Advanced options", open=False):
|
@@ -208,11 +196,12 @@ with gr.Blocks(css=css) as demo:
|
|
208 |
|
209 |
gr.Examples(
|
210 |
examples=examples,
|
211 |
-
inputs=[
|
212 |
outputs=[result, seed],
|
213 |
fn=generate,
|
214 |
cache_examples=CACHE_EXAMPLES,
|
215 |
)
|
|
|
216 |
use_negative_prompt.change(
|
217 |
fn=lambda x: gr.update(visible=x),
|
218 |
inputs=use_negative_prompt,
|
@@ -222,13 +211,13 @@ with gr.Blocks(css=css) as demo:
|
|
222 |
|
223 |
gr.on(
|
224 |
triggers=[
|
225 |
-
|
226 |
negative_prompt.submit,
|
227 |
run_button.click,
|
228 |
],
|
229 |
fn=generate,
|
230 |
inputs=[
|
231 |
-
|
232 |
input_lang,
|
233 |
negative_prompt,
|
234 |
use_negative_prompt,
|
@@ -237,10 +226,10 @@ with gr.Blocks(css=css) as demo:
|
|
237 |
height,
|
238 |
guidance_scale,
|
239 |
randomize_seed,
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
|
245 |
if __name__ == "__main__":
|
246 |
-
|
|
|
11 |
import spaces
|
12 |
import torch
|
13 |
from diffusers import DiffusionPipeline
|
|
|
14 |
|
15 |
DESCRIPTION = """# Playground v2.5"""
|
16 |
if not torch.cuda.is_available():
|
|
|
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(
|
|
|
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,
|
|
|
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
|
|
|
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,
|
150 |
+
max_lines=1,
|
151 |
+
placeholder="Enter your prompt",
|
152 |
+
container=False,
|
153 |
+
)
|
154 |
run_button = gr.Button("Run", scale=0)
|
155 |
result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT, show_label=False)
|
156 |
with gr.Accordion("Advanced options", open=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,
|
203 |
)
|
204 |
+
|
205 |
use_negative_prompt.change(
|
206 |
fn=lambda x: gr.update(visible=x),
|
207 |
inputs=use_negative_prompt,
|
|
|
211 |
|
212 |
gr.on(
|
213 |
triggers=[
|
214 |
+
prompt.submit,
|
215 |
negative_prompt.submit,
|
216 |
run_button.click,
|
217 |
],
|
218 |
fn=generate,
|
219 |
inputs=[
|
220 |
+
prompt,
|
221 |
input_lang,
|
222 |
negative_prompt,
|
223 |
use_negative_prompt,
|
|
|
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()
|