import gradio as gr import torch import whisper from PIL import Image import os MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD') from diffusers import StableDiffusionPipeline ### ———————————————————————————————————————— title="Whisper to Stable Diffusion" ### ———————————————————————————————————————— whisper_model = whisper.load_model("small") device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=MY_SECRET_TOKEN) pipe.to(device) ### ———————————————————————————————————————— def magic_whisper_to_sd(audio, guidance_scale, nb_iterations, seed): whisper_results = translate(audio) prompt = whisper_results[2] images = diffuse(prompt, guidance_scale, nb_iterations, seed) return whisper_results[0], whisper_results[1], whisper_results[2], images def diffuse(prompt, guidance_scale, nb_iterations, seed): generator = torch.Generator(device=device).manual_seed(int(seed)) print(""" — Sending prompt to Stable Diffusion ... — """) print("prompt: " + prompt) print("guidance scale: " + str(guidance_scale)) print("inference steps: " + str(nb_iterations)) print("seed: " + str(seed)) images_list = pipe( [prompt] * 2, guidance_scale=guidance_scale, num_inference_steps=nb_iterations, generator=generator ) images = [] safe_image = Image.open(r"unsafe.png") for i, image in enumerate(images_list["sample"]): if(images_list["nsfw_content_detected"][i]): images.append(safe_image) else: images.append(image) print("Stable Diffusion has finished") print("———————————————————————————————————————————") return images def translate(audio): print(""" — Sending audio to Whisper ... — """) audio = whisper.load_audio(audio) audio = whisper.pad_or_trim(audio) mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device) _, probs = whisper_model.detect_language(mel) transcript_options = whisper.DecodingOptions(task="transcribe", fp16 = False) translate_options = whisper.DecodingOptions(task="translate", fp16 = False) transcription = whisper.decode(whisper_model, mel, transcript_options) translation = whisper.decode(whisper_model, mel, translate_options) print("language spoken: " + transcription.language) print("transcript: " + transcription.text) print("———————————————————————————————————————————") print("translated: " + translation.text) return transcription.language, transcription.text, translation.text ### ———————————————————————————————————————— css = """ .container { max-width: 1280px; margin: auto; padding-top: 1.5rem; } a { text-decoration: underline; } h1 { font-weight: 900; margin-bottom: 7px; text-align: center; font-size: 2em; margin-bottom: 1em; } #w2sd_container{ margin-top: 20px; } .footer { margin-bottom: 45px; margin-top: 35px; text-align: center; border-bottom: 1px solid #e5e5e5; } .footer>p { font-size: .8rem; display: inline-block; padding: 0 10px; transform: translateY(10px); background: white; } .dark .footer { border-color: #303030; } .dark .footer>p { background: #0b0f19; } .tabitem { border-bottom-left-radius: 10px; border-bottom-right-radius: 10px; } #record_tab, #upload_tab { font-size: 1.2em; } #record_btn{ } audio { margin-bottom: 10px; } div#record_btn > .mt-6{ margin-top: 0!important; } div#record_btn > .mt-6 button { font-size: 2em; width: 100%; padding: 20px; height: 160px; } div#upload_area { height: 11.1rem; } div#upload_area > div.w-full > div { min-height: 9rem; } #check_btn_1, #check_btn_2{ --tw-gradient-from: #4caf50; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #4caf50; } #magic_btn_1, #magic_btn_2{ --tw-gradient-from: #f44336; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #ff9800; } div#spoken_lang textarea { font-size: 4em; line-height: 1em; text-align: center; } div#transcripted { flex: 4; } div#translated textarea { font-size: 1.5em; line-height: 1.25em; } #sd_settings { margin-bottom: 20px; } #diffuse_btn { font-size: 1em; margin-bottom: 20px; --tw-gradient-from: #4caf50; --tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to); --tw-gradient-to: #4caf50; } #notice { padding: 20px 14px 10px; display: flex; align-content: space-evenly; gap: 20px; line-height: 1em; font-size: .8em; border: 1px solid #374151; border-radius: 10px; } #about { padding: 20px; } #notice > div { flex: 1; } """ ### ———————————————————————————————————————— with gr.Blocks(css=css) as demo: gr.HTML('''
Ask stable diffusion for images by speaking (or singing 🤗) in your native language ! Try it in French 😉
This demo is running on 🐢 CPU • Offered by Sylvain @fffiloni •
—
Whisper is a general-purpose speech recognition model.
It is trained on a large dataset of diverse audio and is also a multi-task model that can perform multilingual speech recognition as well as speech translation and language identification.
—
Stable Diffusion is a state of the art text-to-image model that generates images from text.
The model is licensed with a CreativeML Open RAIL-M license.
The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license.
The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups.
For the full list of restrictions please read the license.
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence.
The model was trained on the LAION-5B dataset, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes.
You can read more in the model card.