File size: 1,558 Bytes
7727288 cab0672 d450bfe cab0672 d450bfe 7727288 d450bfe | 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 | import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
from transformers import pipeline
from gtts import gTTS
import os
def generate_character(description, emotion, physics, dialogue):
# Load Stable Diffusion Model (Optimized for Speed)
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
# Modify description based on emotion
emotion_map = {"Happy": "smiling", "Angry": "furious", "Sad": "teary-eyed"}
description = f"{description}, {emotion_map.get(emotion, '')}"
# Generate Image
image = pipe(description).images[0]
image.save("output.png")
# Generate Voiceover
try:
tts_model = pipeline("text-to-speech", model="facebook/mms-tts-eng")
speech_output = tts_model(dialogue)
speech_path = "output.wav"
with open(speech_path, "wb") as f:
f.write(speech_output["audio"])
except Exception:
tts = gTTS(dialogue)
speech_path = "output.mp3"
tts.save(speech_path)
return image, speech_path
gui = gr.Interface(
fn=generate_character,
inputs=[
gr.Textbox(label="Character Description"),
gr.Radio(["Happy", "Angry", "Sad"], label="Emotion"),
gr.Radio(["None", "Wind", "Falling"], label="Physics Effect"),
gr.Textbox(label="Dialogue")
],
outputs=[gr.Image(label="Generated Character"), gr.Audio(label="Voiceover")],
allow_flagging="never"
)
gui.launch(debug=True)
|