Afrinetwork7 commited on
Commit
ad3f3da
1 Parent(s): 2107ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -50
app.py CHANGED
@@ -11,6 +11,7 @@ from PIL import Image
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,7 +27,14 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
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,7 +87,7 @@ def translate_to_english(phrase, src_lang):
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,13 +103,23 @@ def generate(
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
105
 
106
  images = pipe(
107
  prompt=prompt,
@@ -122,12 +140,12 @@ def generate(
122
 
123
 
124
  examples = [
125
- ["neon holography crystal cat", "en"],
126
- ["a cat eating a piece of cheese", "en"],
127
- ["an astronaut riding a horse in space", "en"],
128
- ["a cartoon of a boy playing with a tiger", "en"],
129
- ["a cute robot artist painting on an easel, concept art", "en"],
130
- ["a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the shoulder pose, brown skin-tone", "en"]
131
  ]
132
 
133
  css = '''
@@ -143,14 +161,8 @@ with gr.Blocks(css=css) as demo:
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):
@@ -160,7 +172,7 @@ with gr.Blocks(css=css) as demo:
160
  label="Negative prompt",
161
  max_lines=1,
162
  placeholder="Enter a negative prompt",
163
- visible=False,
164
  )
165
  seed = gr.Slider(
166
  label="Seed",
@@ -196,40 +208,39 @@ with gr.Blocks(css=css) as demo:
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,
208
- outputs=negative_prompt,
209
- api_name=False,
210
- )
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,
224
- seed,
225
- width,
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()
 
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
 
28
  NUM_IMAGES_PER_PROMPT = 1
29
 
30
+ languages = {
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
 
88
  @spaces.GPU(enable_queue=True)
89
  def generate(
90
+ audio,
91
  input_lang: str,
92
  negative_prompt: str = "",
93
  use_negative_prompt: bool = False,
 
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(transcript, input_lang)
118
  else:
119
+ prompt = transcript
120
 
121
  if not use_negative_prompt:
122
+ negative_prompt = None # type: ignore
123
 
124
  images = pipe(
125
  prompt=prompt,
 
140
 
141
 
142
  examples = [
143
+ "neon holography crystal cat",
144
+ "a cat eating a piece of cheese",
145
+ "an astronaut riding a horse in space",
146
+ "a cartoon of a boy playing with a tiger",
147
+ "a cute robot artist painting on an easel, concept art",
148
+ "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
149
  ]
150
 
151
  css = '''
 
161
  )
162
  with gr.Group():
163
  with gr.Row():
164
+ input_lang = gr.Dropdown(choices=list(languages.values()), value='eng (English)', label='Input Language')
165
+ audio = gr.Audio(source="microphone", type="filepath", label="Audio Input")
 
 
 
 
 
 
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):
 
172
  label="Negative prompt",
173
  max_lines=1,
174
  placeholder="Enter a negative prompt",
175
+ visible=True,
176
  )
177
  seed = gr.Slider(
178
  label="Seed",
 
208
 
209
  gr.Examples(
210
  examples=examples,
211
+ inputs=[audio, input_lang],
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,
219
+ outputs=negative_prompt,
220
+ api_name=False,
221
+ )
222
+
223
+ gr.on(
224
+ triggers=[
225
+ audio.upload,
226
+ negative_prompt.submit,
227
+ run_button.click,
228
+ ],
229
+ fn=generate,
230
+ inputs=[
231
+ audio,
232
+ input_lang,
233
+ negative_prompt,
234
+ use_negative_prompt,
235
+ seed,
236
+ width,
237
+ height,
238
+ guidance_scale,
239
+ randomize_seed,
240
+ ],
241
+ outputs=[result, seed],
242
+ api_name="run",
243
+ )
244
 
245
  if __name__ == "__main__":
246
+ demo.queue(max_size=20).launch()