File size: 1,971 Bytes
fd03042 f451139 fd03042 |
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 55 56 57 58 59 60 61 62 |
from transformers import pipeline
from huggingface_hub import login
from diffusers import StableDiffusionPipeline
import gradio as gr
import torch
import transformers
# Set Hugging Face token
hf_token = "your_huggingface_token_here" # Replace this with your token
login(hf_token)
# Load Hugging Face models
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-base")
# Load Stable Diffusion model using diffusers
text_to_image = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda" if torch.cuda.is_available() else "cpu")
# Speech-to-text function
def transcribe_audio(audio_file):
try:
result = speech_to_text(audio_file)
transcription = result["text"]
return transcription
except Exception as e:
return f"Error in transcription: {str(e)}"
# Text-to-image function
def generate_image_from_text(text):
try:
image = text_to_image(text).images[0] # Generate one image
return image
except Exception as e:
return f"Error in image generation: {str(e)}"
# Combined processing function
def process_audio_and_generate_image(audio_file):
transcription = transcribe_audio(audio_file)
if "Error" in transcription:
return None, transcription
image = generate_image_from_text(transcription)
if isinstance(image, str) and "Error" in image:
return None, image
return image, transcription
# Gradio interface
iface = gr.Interface(
fn=process_audio_and_generate_image,
inputs=gr.Audio(type="filepath", label="Upload audio file (WAV/MP3)"),
outputs=[
gr.Image(label="Generated Image"),
gr.Textbox(label="Transcription")
],
title="Speech-to-Text and Image Generation",
description="Upload an audio file to transcribe speech to text, and then generate an image based on the transcription.",
)
# Launch the interface
iface.launch(share=True)
|