Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
-
from transformers import
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Load Stable Diffusion
|
| 10 |
-
|
| 11 |
-
if torch.cuda.is_available()
|
| 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 |
-
elif effect == "Falling":
|
| 38 |
-
img_array = np.roll(img_array, 10, axis=0)
|
| 39 |
-
return img_array
|
| 40 |
-
|
| 41 |
-
def track_facial_expressions():
|
| 42 |
-
cap = cv2.VideoCapture(0)
|
| 43 |
-
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 44 |
-
while True:
|
| 45 |
-
ret, frame = cap.read()
|
| 46 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 47 |
-
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 48 |
-
for (x, y, w, h) in faces:
|
| 49 |
-
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
| 50 |
-
cv2.imshow('Face Tracking', frame)
|
| 51 |
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 52 |
-
break
|
| 53 |
-
cap.release()
|
| 54 |
-
cv2.destroyAllWindows()
|
| 55 |
-
|
| 56 |
-
def animate_scene(prompt, emotion, physics, text):
|
| 57 |
-
image = generate_image(prompt, emotion)
|
| 58 |
-
image = apply_physics(image, physics)
|
| 59 |
-
speech = generate_voiceover(text)
|
| 60 |
-
return image, speech
|
| 61 |
-
|
| 62 |
-
iface = gr.Interface(
|
| 63 |
-
fn=animate_scene,
|
| 64 |
inputs=[
|
| 65 |
gr.Textbox(label="Character Description"),
|
| 66 |
gr.Radio(["Happy", "Angry", "Sad"], label="Emotion"),
|
| 67 |
gr.Radio(["None", "Wind", "Falling"], label="Physics Effect"),
|
| 68 |
gr.Textbox(label="Dialogue")
|
| 69 |
],
|
| 70 |
-
outputs=[
|
| 71 |
-
|
| 72 |
-
gr.Audio(label="Voiceover")
|
| 73 |
-
]
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def generate_character(description, emotion, physics, dialogue):
|
| 9 |
+
# Load Stable Diffusion Model (Optimized for Speed)
|
| 10 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
| 11 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
|
| 13 |
+
# Modify description based on emotion
|
| 14 |
+
emotion_map = {"Happy": "smiling", "Angry": "furious", "Sad": "teary-eyed"}
|
| 15 |
+
description = f"{description}, {emotion_map.get(emotion, '')}"
|
| 16 |
+
|
| 17 |
+
# Generate Image
|
| 18 |
+
image = pipe(description).images[0]
|
| 19 |
+
image.save("output.png")
|
| 20 |
+
|
| 21 |
+
# Generate Voiceover
|
| 22 |
+
try:
|
| 23 |
+
tts_model = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 24 |
+
speech_output = tts_model(dialogue)
|
| 25 |
+
speech_path = "output.wav"
|
| 26 |
+
with open(speech_path, "wb") as f:
|
| 27 |
+
f.write(speech_output["audio"])
|
| 28 |
+
except Exception:
|
| 29 |
+
tts = gTTS(dialogue)
|
| 30 |
+
speech_path = "output.mp3"
|
| 31 |
+
tts.save(speech_path)
|
| 32 |
+
|
| 33 |
+
return image, speech_path
|
| 34 |
+
|
| 35 |
+
gui = gr.Interface(
|
| 36 |
+
fn=generate_character,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
inputs=[
|
| 38 |
gr.Textbox(label="Character Description"),
|
| 39 |
gr.Radio(["Happy", "Angry", "Sad"], label="Emotion"),
|
| 40 |
gr.Radio(["None", "Wind", "Falling"], label="Physics Effect"),
|
| 41 |
gr.Textbox(label="Dialogue")
|
| 42 |
],
|
| 43 |
+
outputs=[gr.Image(label="Generated Character"), gr.Audio(label="Voiceover")],
|
| 44 |
+
allow_flagging="never"
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
+
gui.launch(debug=True)
|