Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,76 @@
|
|
| 1 |
-
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
image =
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech
|
| 5 |
+
import openai
|
| 6 |
+
import cv2
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
+
# Load Stable Diffusion pipeline
|
| 10 |
+
sd_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
| 11 |
+
if torch.cuda.is_available():
|
| 12 |
+
sd_pipe.to("cuda")
|
| 13 |
+
|
| 14 |
+
# Load AI voiceover model
|
| 15 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 16 |
+
tts_model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 17 |
+
|
| 18 |
+
# OpenAI API key for voice and physics interactions
|
| 19 |
+
openai.api_key = "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"
|
| 20 |
+
|
| 21 |
+
def generate_image(prompt, emotion):
|
| 22 |
+
emotion_map = {"Happy": "smiling", "Angry": "frowning", "Sad": "crying"}
|
| 23 |
+
prompt = f"{prompt}, {emotion_map.get(emotion, '')}"
|
| 24 |
+
image = sd_pipe(prompt).images[0]
|
| 25 |
+
return image
|
| 26 |
+
|
| 27 |
+
def generate_voiceover(text):
|
| 28 |
+
inputs = processor(text, return_tensors="pt")
|
| 29 |
+
speech = tts_model.generate(**inputs)
|
| 30 |
+
return speech
|
| 31 |
+
|
| 32 |
+
def apply_physics(image, effect):
|
| 33 |
+
img_array = np.array(image)
|
| 34 |
+
if effect == "Wind":
|
| 35 |
+
kernel = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
|
| 36 |
+
img_array = cv2.filter2D(img_array, -1, kernel)
|
| 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 |
+
gr.Image(label="Generated Scene"),
|
| 72 |
+
gr.Audio(label="Voiceover")
|
| 73 |
+
]
|
| 74 |
)
|
| 75 |
|
| 76 |
+
iface.launch()
|