File size: 1,607 Bytes
3c53ea4
 
 
f2676bd
3c53ea4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17413e5
 
3c53ea4
 
 
 
89859a3
3c53ea4
45ec651
3c53ea4
 
 
 
 
 
 
 
 
 
ea0fe8d
3c53ea4
 
 
45ec651
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import time
import whisper
import torch
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler



def SpeechToText(audio):
    if audio == None : return "" 
    model = whisper.load_model("base")
    audio = whisper.load_audio(audio)
    audio = whisper.pad_or_trim(audio)

    # make log-Mel spectrogram and move to the same device as the model
    mel = whisper.log_mel_spectrogram(audio).to(model.device)

    # Detect the Max probability of language ?
    _, probs = model.detect_language(mel)
    lang = f"Language: {max(probs, key=probs.get)}"

    #  Decode audio to Text
    options = whisper.DecodingOptions(fp16 = False)
    result = whisper.decode(model, mel, options)
    return result.text

  
def img_Generation(text):
  print(text)
  #model_id = "stabilityai/stable-diffusion-2"
  model_id = "stabilityai/stable-diffusion-2-1"

  # Use the Euler scheduler here instead
  scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
  pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, revision="fp16", torch_dtype=torch.float16)
  pipe = pipe.to("cuda")
  image = pipe(text, num_inference_steps = 150).images[0]
  #image.save("img_1.png")

  return image


def transcribe(audio):
  text = SpeechToText(audio)
  image = img_Generation(text)

  return image

# gradio 
gr.Interface(
    fn=transcribe, 
    inputs=gr.Audio(source="microphone", type="filepath"), 
    outputs="image",description="A Speech to Image Generation App Using OpenAI's Whisper and Stable Diffusion V.2",title= "Whisper2IMG").launch()