Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
import os
|
6 |
+
MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
|
7 |
+
|
8 |
+
from diffusers import StableDiffusionPipeline
|
9 |
+
|
10 |
+
whisper_model = whisper.load_model("small")
|
11 |
+
|
12 |
+
device="cpu"
|
13 |
+
|
14 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=MY_SECRET_TOKEN)
|
15 |
+
pipe.to(device)
|
16 |
+
|
17 |
+
def get_transcribe(audio):
|
18 |
+
audio = whisper.load_audio(audio)
|
19 |
+
audio = whisper.pad_or_trim(audio)
|
20 |
+
|
21 |
+
mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
|
22 |
+
|
23 |
+
_, probs = whisper_model.detect_language(mel)
|
24 |
+
|
25 |
+
options = whisper.DecodingOptions(fp16 = False)
|
26 |
+
result = whisper.decode(whisper_model, mel, options)
|
27 |
+
|
28 |
+
print(result.text)
|
29 |
+
return result.text
|
30 |
+
|
31 |
+
def get_images(audio):
|
32 |
+
prompt = get_transcribe(audio)
|
33 |
+
#image = pipe(prompt, init_image=init_image)["sample"][0]
|
34 |
+
images_list = pipe([prompt] * 2)
|
35 |
+
images = []
|
36 |
+
safe_image = Image.open(r"unsafe.png")
|
37 |
+
for i, image in enumerate(images_list["sample"]):
|
38 |
+
if(images_list["nsfw_content_detected"][i]):
|
39 |
+
images.append(safe_image)
|
40 |
+
else:
|
41 |
+
images.append(image)
|
42 |
+
|
43 |
+
return images
|
44 |
+
#inputs
|
45 |
+
audio = gr.Audio(label="Input Audio", show_label=False, source="microphone", type="filepath")
|
46 |
+
#outputs
|
47 |
+
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
|
48 |
+
|
49 |
+
gr.Interface(fn=get_images, inputs=audio, outputs=gallery).queue(max_size=10).launch(enable_queue=True)
|